Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

ruby unpack for 64-bit integer

Tags:

ruby

binary

I need to convert big-endian, 8-bytes, uint64 into an integer.

I can only find Q in the documentation for 64-bit unsigned, native endian (uint64_t) and nothing for 64-bit big-endian uint64.

So I just want to make sure that Q is the right one to use since Q does not specify that it is for big-endian

What I'm using: uid = io.read(8).unpack("Q")[0]

Or should I break it down into 2 32-bit integers then convert to string then concatenate like ...

uid = (io.read(4).unpack("N")[0].to_s + io.read(4).unpack("N")[0].to_s).to_i

like image 213
monty_lennie Avatar asked Aug 15 '26 12:08

monty_lennie


1 Answers

From the same documentation page you linked:

S> L> Q>  | Integer | same as the directives without ">" except
s> l> q>  |         | big endian
like image 138
Amadan Avatar answered Aug 17 '26 03:08

Amadan



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!