How to Get the String in Specified Base in Golang?


When working with numbers in programming, it's often necessary to convert between different number bases, such as decimal, binary, and hexadecimal. In Go, you can easily convert a number to a string representation in a specified base using the strconv package. In this article, we'll explore how to get the string in a specified base in Golang.

Step 1: Convert the Number to a String

The first step to getting the string in a specified base in Go is to convert the number to a string using the strconv package. The strconv.Itoa() function can be used to convert an integer to a string.

num := 42
str := strconv.Itoa(num)

In this example, we're converting the integer 42 to a string using the strconv.Itoa() function.

Step 2: Convert the String to the Desired Base

Once you have the number in string format, you can convert it to the desired base using the strconv package. The strconv.ParseInt() function can be used to convert a string to an integer in a specified base.

numStr := "101010"
num, err := strconv.ParseInt(numStr, 2, 64)

In this example, we're converting the string "101010" to an integer in binary format using the strconv.ParseInt() function. The second argument to the function specifies the base of the input string, which is 2 for binary. The third argument specifies the number of bits in the resulting integer, which is 64 in this case.

Step 3: Convert the Integer to a String in the Desired Base

Finally, you can convert the integer to a string representation in the desired base using the strconv.FormatInt() function.

base := 16
str := strconv.FormatInt(num, base)

In this example, we're converting the integer num to a string in hexadecimal format using the strconv.FormatInt() function. The second argument to the function specifies the base of the output string, which is 16 for hexadecimal.

Step 4: Print the Result

You can print the result of the conversion to the console using the fmt package.

fmt.Println(str)

By following these simple steps, you can convert a number to a string representation in a specified base in Go.

Example

package main

import (
   "fmt"
   "strconv"
)

func main() {
   num := 42
   str := strconv.Itoa(num)
   fmt.Println("Integer to string:", str)

   numStr := "101010"
   num64, err := strconv.ParseInt(numStr, 2, 64)
   if err != nil {
      fmt.Println(err)
      return
   }
   fmt.Println("String to integer:", num64)
   
   base := 16
   str = strconv.FormatInt(num64, base)
   fmt.Println("Integer to string in hexadecimal:", str)
}

Output

Integer to string: 42
String to integer: 42
Integer to string in hexadecimal: 2a

Conclusion

Converting a number to a string representation in a specified base is a common task in programming that can be accomplished easily in Go using the strconv package. By following the steps outlined in this article, you can convert a number to a string in any desired base in your Go program.

Updated on: 08-May-2023

185 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements