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?
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?
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?
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”.
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
(scanf for Ruby, v.1.1.1), though the more general
solution of Integer might be better if you’re also doing binary.
(Hmmm… binary in scanf… hmmm…)
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.
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.
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.
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…)