string(42)
converts integer constant 42
to an array of bytes, of length 1 where first element of array has 00101010
package main
import "fmt"
func main() {
s := string(42)
fmt.Printf("%d\n", len(s)) // 1
fmt.Printf("%b\n", s[0]) // 101010 looks good
}
But,
Below code is taking the valid integer constant,
package main
import "fmt"
func main() {
s := string(1024)
fmt.Printf("%d\n", len(s)) // 2
fmt.Printf("%b %b\n", s[0], s[1]) // 11010000 10000000 this looks wrong representation, it should be 00000100 00000000
}
Below code is taking the valid integer constant,
package main
import "fmt"
func main() {
s := string(4254353345467546745674564564567445674658647567567853467867568756756785786785676858878978978978978907978977896789676786789655289890980889098835432453455544)
fmt.Printf("%d\n", len(s))
fmt.Printf("%d %d %d", s[0], s[1], s[2])
}
and converting it to array of bytes, of size 3. 239 191 189
but this is the not the right representation of this integer constant. It should be more than 3 bytes.
How to retrieve the bytes for the given integer constant?