To convert a number from base 2 to base 8 in go, we can use the strconv
package which provides functions to convert strings to different numeric formats. Here's an example code that converts a binary number to an octal:
main.go274 chars18 lines
The strconv.ParseInt
function takes three arguments - the input string, the base to convert from (in this case 2 for binary) and the bit size (in this case 64). It returns the decimal equivalent of the input string.
The strconv.FormatInt
function takes two arguments - the decimal number to convert and the base to convert to (in this case 8 for octal). It returns a string representing the decimal number in the desired base.
The output of the above code would be:
main.go28 chars3 lines
gistlibby LogSnag