What you are asking (to store 18,446,744,073,709,551,615 as an int64 value) is impossible.
A unit64 stores positive integers and has 64 bits available to hold information. It can therefore store any positive integer between 0 and 18,446,744,073,709,551,615 (2^64-1).
An int64 uses one bit to hold the sign, leaving 63 bits to hold information about the number. It can store any value between -9,223,372,036,854,775,808 and +9,223,372,036,854,775,807 (-2^63 and 2^63-1).
Both types can hold 18,446,744,073,709,551,616 unique integers, it is just that the uint64 range starts at zero, where as the int64 values straddle zero.
To hold 18,446,744,073,709,551,615 as a signed integer would require 65 bits.
In your conversion, no information from the underlying bytes is lost. The difference in the integer values returned is due to how the the two types interpret and display the values.
uint64 will display a raw integer value, whereas int64 will use two's complement.
var x uint64 = 18446744073709551615
var y int64 = int64(x)
fmt.Printf("uint64: %v = %#[1]x, int64: %v = %#x\n", x, y, uint64(y))
// uint64: 18446744073709551615 = 0xffffffffffffffff
// int64: -1 = 0xffffffffffffffff
x -= 100
y -= 100
fmt.Printf("uint64: %v = %#[1]x, int64: %v = %#x\n", x, y, uint64(y))
// uint64: 18446744073709551515 = 0xffffffffffffff9b
// int64: -101 = 0xffffffffffffff9b
https://play.golang.com/p/hlWqhnC9Dh
Answer from PassKit on Stack OverflowYou convert them with a type "conversion"
var a int
var b int64
int64(a) < b
When comparing values, you always want to convert the smaller type to the larger. Converting the other way will possibly truncate the value:
var x int32 = 0
var y int64 = math.MaxInt32 + 1 // y == 2147483648
if x < int32(y) {
// this evaluates to false, because int32(y) is -2147483648
Or in your case to convert the maxInt int64 value to an int, you could use
for a := 2; a < int(maxInt); a++ {
which would fail to execute correctly if maxInt overflows the max value of the int type on your system.
I came here because of the title, "How to convert an int64 to int in Go?". The answer is,
int(int64Var)
Converting int32 to uint64 and back to int32
go - When casting an int64 to uint64, is the sign retained? - Stack Overflow
go - How to change int into int64? - Stack Overflow
Convert Uint64 to slice []Uint8
I am a bit concerned about a piece of code I wrote. It seems to work but I did not quite expect it to...
How does type conversion exactly work in Go when I convert a int32 to uint64 and then back to int32. I was somewhat scared that funny business might happen.
To give practical context of what I am doing. I have data like:
type data struct {
a uint16
b uint8
c uint8
d int32
}
Instead of using a data struct I want to package all the values in a single uint64 like:
data := uint64(a) | uint64(b)<<16 | uint64(c)<<24 | uint64(d)<<32
and then unpack it like (shifting back and applying a bit mask if necessary):
d := int32(data>>32)
my worries were around:
data = uint64(int32(d))
I was worried that it would try to do some conversion from int32 with negative values. But running some tests all it seems to be doing is padding or truncating bits and interpreting the two's compliment int values as uint.
Is this all safe and correct or am I about to run into nightmarish bugs?
Edit: In essence what I am asking with go type conversions does this hold for every value of d of type int32 :
d == int32(uint64(d))
Parsing string into int64 example:
// Use the max value for signed 64 integer. http://golang.org/pkg/builtin/#int64
var s string = "9223372036854775807"
i, err := strconv.ParseInt(s, 10, 64)
if err != nil {
panic(err)
}
fmt.Printf("Hello, %v with type %s!\n", i, reflect.TypeOf(i))
output:
Hello, 9223372036854775807 with type int64!
https://play.golang.org/p/XOKkE6WWer
No, there's no Atoi64. You should also pass in the 64 as the last parameter to ParseInt, or it might not produce the expected value on a 32-bit system.
var s string = "9223372036854775807"
i, _ := strconv.ParseInt(s, 10, 64)
fmt.Printf("val: %v ; type: %[1]T\n", i)
https://play.golang.org/p/FUC8QO0-lYn