1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
| /* 二进制转换 */ func convert2binary(n int) { result := "" for ; n > 0; n /= 2 { // 每次除于二 // 求余数 lsb := n % 2 // 每次求到的余数添加到结果的前面 result = strconv.Itoa(lsb) + result } fmt.Println(result) }
func main() { convert2binary(100) }
|