github.com/lovishpuri/go-40569/src@v0.0.0-20230519171745-f8623e7c56cf/os/str.go (about) 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 // Simple conversions to avoid depending on strconv. 6 7 package os 8 9 // itox converts val (an int) to a hexadecimal string. 10 func itox(val int) string { 11 if val < 0 { 12 return "-" + uitox(uint(-val)) 13 } 14 return uitox(uint(val)) 15 } 16 17 const hex = "0123456789abcdef" 18 19 // uitox converts val (a uint) to a hexadecimal string. 20 func uitox(val uint) string { 21 if val == 0 { // avoid string allocation 22 return "0x0" 23 } 24 var buf [20]byte // big enough for 64bit value base 16 + 0x 25 i := len(buf) - 1 26 for val >= 16 { 27 q := val / 16 28 buf[i] = hex[val%16] 29 i-- 30 val = q 31 } 32 // val < 16 33 buf[i] = hex[val%16] 34 i-- 35 buf[i] = 'x' 36 i-- 37 buf[i] = '0' 38 return string(buf[i:]) 39 }