Skip to content

Instantly share code, notes, and snippets.

@luojiyin1987
Created August 28, 2017 15:40
Show Gist options
  • Save luojiyin1987/5275d50537c9fdb94eec759713e8f260 to your computer and use it in GitHub Desktop.
Save luojiyin1987/5275d50537c9fdb94eec759713e8f260 to your computer and use it in GitHub Desktop.
Base 7
func convertToBase7(num int) string {
if num == 0 {
return "0"
}
var positive bool
var result string
if num < 0 {
num = -num
positive = true
}
for num !=0 {
result = strconv.Itoa(num%7) + result
num /= 7
}
if positive {
result = "-" + result
}
return result
}
//----------------------------
func convertToBase7(num int) string {
if num == 0 {
return "0"
}
var neg bool
if num < 0 {
neg = true
num *= -1
}
res := ""
for ; num != 0; num /= 7 {
res = strconv.Itoa(num%7) + res
}
if neg {
res = "-" + res
}
return res
}
//----------------------
func convertToBase7(num int) string {
if num == 0 {
return "0"
}
var res, sign string
if num<0 {
sign = "-"
num=-num
}
for ;num>0; num/=7 {
res = string((byte(num%7)+'0'))+res
}
return sign+res
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment