Question about Hex => Signed Int

T

Tim Conner

Hello,
Is there a ruby function to convert the following hexadecimal numbers
into the corresponding signed integers or will i need to write something
to do the conversion? Sorry if this question is stupid but i'm pretty
new to all this.

0xFF467A7B => -12158341
0x38CFEF => 3723247

Thanks
 
G

Gary Wright

Hello,
Is there a ruby function to convert the following hexadecimal numbers
into the corresponding signed integers or will i need to write
something
to do the conversion? Sorry if this question is stupid but i'm pretty
new to all this.

0xFF467A7B => -12158341
0x38CFEF => 3723247

The tricky part is dealing with the twos compliment notation but
String#to_i will do the hex/decimal conversion:
=> -12158341
=> 3723247

Gary Wright
 
T

Tim Conner

Gary said:
The tricky part is dealing with the twos compliment notation but
String#to_i will do the hex/decimal conversion:

=> -12158341

=> 3723247

Gary Wright

Thanks for that. Is there a ruby function which I can pass a hex number
and it works out if it is +/- and then returns the correct decimal
value?
 
J

Joel VanderWerf

Tim said:
Thanks for that. Is there a ruby function which I can pass a hex number
and it works out if it is +/- and then returns the correct decimal
value?

Convert to unsigned first, like above, then do something like this:

length = 32 # in bits; you have to choose this

mid = 2**(length-1)
max_unsigned = 2**length
to_signed = proc {|n| (n>=mid) ? n - max_unsigned : n}

p to_signed["0xFF467A7B".to_i(16)]
p to_signed["0x38CFEF".to_i(16)]

See http://en.wikipedia.org/wiki/Twos_complement.
 
K

Ken Bloom

Tim said:
Thanks for that. Is there a ruby function which I can pass a hex
number and it works out if it is +/- and then returns the correct
decimal value?

Convert to unsigned first, like above, then do something like this:

length = 32 # in bits; you have to choose this

mid = 2**(length-1)
max_unsigned = 2**length
to_signed = proc {|n| (n>=mid) ? n - max_unsigned : n}

p to_signed["0xFF467A7B".to_i(16)]
p to_signed["0x38CFEF".to_i(16)]

See http://en.wikipedia.org/wiki/Twos_complement.

Why go through all the trouble to make this a proc?

--Ken
 
J

Joel VanderWerf

Ken said:
Tim said:
Thanks for that. Is there a ruby function which I can pass a hex
number and it works out if it is +/- and then returns the correct
decimal value?
Convert to unsigned first, like above, then do something like this:

length = 32 # in bits; you have to choose this

mid = 2**(length-1)
max_unsigned = 2**length
to_signed = proc {|n| (n>=mid) ? n - max_unsigned : n}

p to_signed["0xFF467A7B".to_i(16)]
p to_signed["0x38CFEF".to_i(16)]

See http://en.wikipedia.org/wiki/Twos_complement.

Why go through all the trouble to make this a proc?

Cut and paste from some code that was using #define_method with this
proc, and thereby saved performing the exponentiations on each call.
That's all.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,744
Messages
2,569,484
Members
44,903
Latest member
orderPeak8CBDGummies

Latest Threads

Top