school/cs142/smcquay/go/lab02/binary/binary.go

27 lines
387 B
Go
Raw Normal View History

2016-04-06 20:45:34 -07:00
package main
import (
"fmt"
)
func itob(i int) string {
var buf [64]byte
j := len(buf)
b := 2
for i > 0 {
j--
buf[j] = "0123456789abcdefghipqrstuvwxyz"[i%b]
i /= b
}
return string(buf[j:])
}
func main() {
var i int
fmt.Print("please enter a number: ")
fmt.Scan(&i)
fmt.Printf("%d as a binary: %s\n", i, itob(i))
}