The Go Programming Language

Source file src/pkg/strconv/itoa.go

     1	// Copyright 2009 The Go Authors. All rights reserved.
     2	// Use of this source code is governed by a BSD-style
     3	// license that can be found in the LICENSE file.
     4	
     5	package strconv
     6	
     7	// Uitob64 returns the string representation of i in the given base.
     8	func Uitob64(u uint64, base uint) string {
     9		if base < 2 || 36 < base {
    10			panic("invalid base " + Uitoa(base))
    11		}
    12		if u == 0 {
    13			return "0"
    14		}
    15	
    16		// Assemble decimal in reverse order.
    17		var buf [64]byte
    18		j := len(buf)
    19		b := uint64(base)
    20		for u > 0 {
    21			j--
    22			buf[j] = "0123456789abcdefghijklmnopqrstuvwxyz"[u%b]
    23			u /= b
    24		}
    25	
    26		return string(buf[j:])
    27	}
    28	
    29	// Itob64 returns the string representation of i in the given base.
    30	func Itob64(i int64, base uint) string {
    31		if i == 0 {
    32			return "0"
    33		}
    34	
    35		if i < 0 {
    36			return "-" + Uitob64(-uint64(i), base)
    37		}
    38		return Uitob64(uint64(i), base)
    39	}
    40	
    41	// Itoa64 returns the decimal string representation of i.
    42	func Itoa64(i int64) string { return Itob64(i, 10) }
    43	
    44	// Uitoa64 returns the decimal string representation of i.
    45	func Uitoa64(i uint64) string { return Uitob64(i, 10) }
    46	
    47	// Uitob returns the string representation of i in the given base.
    48	func Uitob(i uint, base uint) string { return Uitob64(uint64(i), base) }
    49	
    50	// Itob returns the string representation of i in the given base.
    51	func Itob(i int, base uint) string { return Itob64(int64(i), base) }
    52	
    53	// Itoa returns the decimal string representation of i.
    54	func Itoa(i int) string { return Itob64(int64(i), 10) }
    55	
    56	// Uitoa returns the decimal string representation of i.
    57	func Uitoa(i uint) string { return Uitob64(uint64(i), 10) }

release.r60.3. Except as noted, this content is licensed under a Creative Commons Attribution 3.0 License.