Convert Uint64 to slice []Uint8

(YoungGopher) #1

Hi all
I’m using package satori/uuid to get uuid values
But in my case after getting new uuid i need convert it to simple int type
I found some solution but can’t understand how after work with int value reconvert it to uuid format again
How to do it effective and simple?

For example

newValue, _ := uuid.NewV4()
log.Printf("%T", newValue)
//convert to slice []uint8
b, _ := newValue.MarshalBinary()
log.Printf("%T, %v", b, b)

//convert to uint64
data := binary.BigEndian.Uint64(b)
log.Printf("%T, %v", data, data)


[]uint8, [238 57 123 50 142 161 70 253 153 220 163 192 193 38 123 135]
uint64, 17165886911770871549

But how convert uint64 -> []uint8 -> uuid ?
Best variant convert in uuid -> int and int -> uuid
without uint’s format - but i don’t understand how(
Using runes or else?

(Tim Heckman) #2

You can’t directly convert a UUID to a simple integer type. UUIDs are 128 bits, and the largest integer type in Go is only 64 bits. This means to store the UUID as integers, you’d need two integer values.

You also can’t convert a uint64 to a UUID, because a UUID is 128 bits and would require two uint64 to correctly represent it. So if you go from a uint64 to a UUID it means you’ve drastically reduced the uniqueness of the ID, because only 64 of 128 bits have been set.

Why do you need to convert it to a simple integer type? What are you looking to use the data for?

To give you a code snippet at least, here is how I’d convert a uint64 to a []byte:

I should add that there is the math/big package which has the big.Int type. You can use this to represent integers larger than 64 bit in size, although I wouldn’t consider this a simple integer.

(YoungGopher) #3

Hi - thanks for description
When i create every object (small generator - but it can be thousands objects (and concurrency work))
i need unique (id) for every object
After then the new object i want represented to json format (with package google/jsonapi where need ID int)
for sending to the next step with http
And i think theat using uuid this the most unique desicion
Maybe i’ll wrong
and maybe i can use reflect or something
for loop from uuid.String() to convert runes -> int
… i think

(Tim Heckman) #4

@YongGopher If you want a unique identifier that can fit within a 64-bit integer, you can’t use UUIDs. The problem is that a UUID is too large to fit within an integer no matter what kind of manipulation you do. It’d be the same as trying to fit two liters of water in to a one liter bottle, it’s not physically possible.

If you want a unique ID, you could try generating random uint64 numbers and using those. That would grant you, at most, 18,446,744,073,709,551,615 unique values. Whereas the 128-bit random value has approximately 340,282,366,920,938,463,463,374,607,431,768,211,455 values. The chances of a collision are dramatically less.

Another thing to consider is whether the system you’re using JSON with supports 64-bit numerical values. JavaScript, for example, can only have 53-bits of precision for integer values. So you’ll need to be mindful of the languages you’re interfacing with, and whether it supports integers as large as you need.

(Norbert Melzer) #5

If you want to have UUID in JSON, the safest way is to simply use its string representation.

The UUID package you use has a proper implementation of the Stringer interface, such that you can put it into JSON easily:

In general you should avoid numbers in JSON (at least if you need them exact), as JSON does not differ between float and integer, it does know only “number”, and if a given integral literal can’t be represented in the readers integral types, its allowed to the reader to coerce silently into a float. In theory, the reader does not even need to try to fit it into an int. It is allowed to read 1 as 1.0 right from the beginning.

(YoungGopher) #6

Yes I know about String() method to UUID

(system) #7

This topic was automatically closed 90 days after the last reply. New replies are no longer allowed.