Converting from an integer to its binary representation

前端 未结 7 979
野的像风
野的像风 2020-12-24 00:09

Has anyone got an idea if there is any inbuilt functionality in Go for converting from any one of the numeric types to its binary number form.

For example, if

7条回答
  •  生来不讨喜
    2020-12-24 00:41

    This code works on big integers *big.Int :

    x := big.NewInt(123)
    s := fmt.Sprintf("%b", x)
    // s == "1111011"
    

    because *big.Int implements the fmt.Formatter interface.

    Taken from https://stackoverflow.com/a/23317788/871134

提交回复
热议问题