hex representation for integers in Golang

37 Views Asked by At

Can't understand how Go converts integers to hex

when I run this code

package main

import (
    "fmt"
)

func main() {
    d := -12345
    hex := fmt.Sprintf("%X", d)
    fmt.Println(hex)
}

I see result -3039, but I expect to see cfc7, because go uses Two’s-Complement representation And how can I get a right answer in the output?

1

There are 1 best solutions below

0
Burak Serdar On

You are passing in an int, which is signed. Pass an unsigned in:

    hex := fmt.Sprintf("%X", d)

This will print FFFFFFFFFFFFCFC7