Reading in Hex numbers

Hi all,

I want to read in a file that has hex numbers, which are stored with
the “0x” hex prefix (i.e. 0x12345678). Is there a built in function
that will do the conversion, similiar to “to_i”, for hex numbers?

Also… is there something also for binary? Octal?

Thanks,

  • Khurram

I want to read in a file that has hex numbers, which are stored with
the "0x" hex prefix (i.e. 0x12345678). Is there a built in function
that will do the conversion, similiar to "to_i", for hex numbers?

You can try with Integer

pigeon% ruby -e 'p Integer("0x12345678")'
305419896
pigeon%

pigeon% ruby -e 'p Integer("0b0001110")'
14
pigeon%

pigeon% ruby -e 'p Integer("01234567")'
342391
pigeon%

pigeon% ruby -e 'p Integer("1234567")'
1234567
pigeon%

Guy Decoux

You can use Integer on a string e.g.

irb(main):001:0> Integer(‘0xE84C’)
59468
irb(main):002:0> Integer(‘040’)
32
irb(main):003:0> Integer(‘0b1011’)
11

Hope this helps,

Mike

···

In article 9ba6a8da.0209090511.35a87842@posting.google.com, Khurram wrote:

Hi all,

I want to read in a file that has hex numbers, which are stored with
the “0x” hex prefix (i.e. 0x12345678). Is there a built in function
that will do the conversion, similiar to “to_i”, for hex numbers?

Also… is there something also for binary? Octal?


mike@stok.co.uk | The “`Stok’ disclaimers” apply.
http://www.stok.co.uk/~mike/ | GPG PGP Key 1024D/059913DA
mike@exegenix.com | Fingerprint 0570 71CD 6790 7C28 3D60
http://www.exegenix.com/ | 75D2 9EC4 C1C0 0599 13DA

Khurram wrote:

I want to read in a file that has hex numbers, which are stored with
the “0x” hex prefix (i.e. 0x12345678). Is there a built in function
that will do the conversion, similiar to “to_i”, for hex numbers?

Yes, String#hex does this with or without the “0x” prefix; e.g.

"0x123".hex -> yields 291
"123".hex -> ditto

Also… is there something also for binary? Octal?

For octal numbers, it’s String#oct:

"123".oct -> yields 83

I don’t think there is a built-in for converting from binary numbers,
but there is a sample implementation in Hal Fulton’s excellent book,
“The Ruby Way”.

Hope this helps,

Lyle

Hi –

···

On Mon, 9 Sep 2002, Khurram wrote:

Hi all,

I want to read in a file that has hex numbers, which are stored with
the “0x” hex prefix (i.e. 0x12345678). Is there a built in function
that will do the conversion, similiar to “to_i”, for hex numbers?

Also… is there something also for binary? Octal?

For hex and octal you could use scanf
(http://www.rubyhacker.com/code/scanf), though the more general
solution of Integer might be better if you’re also doing binary.
(Hmmm… binary in scanf… hmmm…)

David


David Alan Black | Register for RubyConf 2002!
home: dblack@candle.superlink.net | November 1-3
work: blackdav@shu.edu | Seattle, WA, USA
Web: http://pirate.shu.edu/~blackdav | http://www.rubyconf.com

I want to read in a file that has hex numbers, which are stored with
the “0x” hex prefix (i.e. 0x12345678). Is there a built in function
that will do the conversion, similiar to “to_i”, for hex numbers?

Also… is there something also for binary? Octal?

Integer() is probably your best bet, but there
is also scanf (see the RAA). scanf will work
with hex and octal, though not binary.

Hal

···

----- Original Message -----
From: “Khurram” khabibiuf@hotmail.com
Newsgroups: comp.lang.ruby
To: “ruby-talk ML” ruby-talk@ruby-lang.org
Sent: Monday, September 09, 2002 8:31 AM
Subject: Reading in Hex numbers.

Hi,

I want to read in a file that has hex numbers, which are stored with
the “0x” hex prefix (i.e. 0x12345678). Is there a built in function
that will do the conversion, similiar to “to_i”, for hex numbers?

Also… is there something also for binary? Octal?

In 1.7, String#to_i takes optional base argument, e.g.

“111”.to_i => 111
"111".to_i(2) => 7
"111".to_i(8) => 73
"111".to_i(10) => 111
"111".to_i(16) => 273

						matz.
···

In message “Reading in Hex numbers.” on 02/09/09, Khurram khabibiuf@hotmail.com writes:

:slight_smile: :slight_smile:

I knew you would pick up on that, David.

I think adding binary to scanf is fine as long as
we don’t run out of alphabet. I.e., assuming %b is
the logical choice and it doesn’t already mean
something that we’re likely to implement in the
future.

Hal

···

----- Original Message -----
From: dblack@candle.superlink.net
To: “ruby-talk ML” ruby-talk@ruby-lang.org
Sent: Monday, September 09, 2002 10:55 AM
Subject: Re: Reading in Hex numbers.

I want to read in a file that has hex numbers, which are stored with
the “0x” hex prefix (i.e. 0x12345678). Is there a built in function
that will do the conversion, similiar to “to_i”, for hex numbers?

Also… is there something also for binary? Octal?

For hex and octal you could use scanf
(http://www.rubyhacker.com/code/scanf), though the more general
solution of Integer might be better if you’re also doing binary.
(Hmmm… binary in scanf… hmmm…)