[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

comp.lang.ruby

character to hex/binary/etc...

John Joyce

8/16/2007 7:47:00 PM

Hmm... I can turn a number into a character with .chr
I can convert my numbers with .to_s(2) ((where 2 is a radix, or base))
But how do I convert numbers to characters??

15 Answers

Wolfgang Nádasi-donner

8/16/2007 7:58:00 PM

0

John Joyce wrote:
> Hmm... I can turn a number into a character with .chr
> I can convert my numbers with .to_s(2) ((where 2 is a radix, or base))
> But how do I convert numbers to characters??

I don't understand what you mean - "to_s(b)" builds a string. What do
you mean by "convert numbers to characters" (example).

Wolfgang Nádasi-Donner
--
Posted via http://www.ruby-....

John Joyce

8/16/2007 8:17:00 PM

0


On Aug 16, 2007, at 2:57 PM, Wolfgang Nádasi-Donner wrote:

> John Joyce wrote:
>> Hmm... I can turn a number into a character with .chr
>> I can convert my numbers with .to_s(2) ((where 2 is a radix, or
>> base))
>> But how do I convert numbers to characters??
>
> I don't understand what you mean - "to_s(b)" builds a string. What do
> you mean by "convert numbers to characters" (example).
>
> Wolfgang Nádasi-Donner
> --
> Posted via http://www.ruby-....
>
yes it builds a string representation of a number.
I want to take a hex or binary number though and return a character.
0x49 for example is "I"


Wolfgang Nádasi-donner

8/16/2007 8:21:00 PM

0

John Joyce wrote:
> On Aug 16, 2007, at 2:57 PM, Wolfgang N�dasi-Donner wrote:
>
>> --
>> Posted via http://www.ruby-....
>>
> yes it builds a string representation of a number.
> I want to take a hex or binary number though and return a character.
> 0x49 for example is "I"

irb(main):002:0> 0x49.chr
=> "I"

It works!
--
Posted via http://www.ruby-....

Stefano Crocco

8/16/2007 8:23:00 PM

0

Alle giovedì 16 agosto 2007, John Joyce ha scritto:
> On Aug 16, 2007, at 2:57 PM, Wolfgang Nádasi-Donner wrote:
> > John Joyce wrote:
> >> Hmm... I can turn a number into a character with .chr
> >> I can convert my numbers with .to_s(2) ((where 2 is a radix, or
> >> base))
> >> But how do I convert numbers to characters??
> >
> > I don't understand what you mean - "to_s(b)" builds a string. What do
> > you mean by "convert numbers to characters" (example).
> >
> > Wolfgang Nádasi-Donner
> > --
> > Posted via http://www.ruby-....
>
> yes it builds a string representation of a number.
> I want to take a hex or binary number though and return a character.
> 0x49 for example is "I"

I think you need Integer#chr:

0x49.chr
=> 'I'

Stefano

Tom Werner

8/16/2007 8:28:00 PM

0

John Joyce wrote:
> Hmm... I can turn a number into a character with .chr
> I can convert my numbers with .to_s(2) ((where 2 is a radix, or base))
> But how do I convert numbers to characters??
>
>

You may be looking for Array#pack. It's useful for converting an array
from one encoding to another. One of these possibilities is to convert
an array of numbers into a string:

n = [65, 66, 67]
n.pack("c*")
#=> "ABC"


Pack (and unpack) can do tons of useful and surprising things.

Tom

--
* Libraries:
Chronic (chronic.rubyforge.org)
God (god.rubyforge.org)
* Site:
rubyisawesome.com


John Joyce

8/16/2007 8:28:00 PM

0


On Aug 16, 2007, at 3:22 PM, Stefano Crocco wrote:

> Alle giovedì 16 agosto 2007, John Joyce ha scritto:
>> On Aug 16, 2007, at 2:57 PM, Wolfgang Nádasi-Donner wrote:
>>> John Joyce wrote:
>>>> Hmm... I can turn a number into a character with .chr
>>>> I can convert my numbers with .to_s(2) ((where 2 is a radix, or
>>>> base))
>>>> But how do I convert numbers to characters??
>>>
>>> I don't understand what you mean - "to_s(b)" builds a string.
>>> What do
>>> you mean by "convert numbers to characters" (example).
>>>
>>> Wolfgang Nádasi-Donner
>>> --
>>> Posted via http://www.ruby-....
>>
>> yes it builds a string representation of a number.
>> I want to take a hex or binary number though and return a character.
>> 0x49 for example is "I"
>
> I think you need Integer#chr:
>
> 0x49.chr
> => 'I'
>
> Stefano
>
Oops, no, that's not what I need. I can do that.
I need to take "I" or "J" or whatever character and convert to hex!
or binary
(sorry, I'm a little sleepy now)

Adam Shelly

8/16/2007 8:36:00 PM

0

On 8/16/07, John Joyce <dangerwillrobinsondanger@gmail.com> wrote:
> Oops, no, that's not what I need. I can do that.
> I need to take "I" or "J" or whatever character and convert to hex!
> or binary
> (sorry, I'm a little sleepy now)
>
like this?


irb(main):011:0> s = "A"
=> "A"
irb(main):012:0> s[0]
=> 65
irb(main):013:0> c = ?A
=> 65
irb(main):014:0> s[0].to_s(16)
=> "41"
irb(main):015:0> c.to_s(16)
=> "41"
irb(main):016:0> c.to_s(2)
=> "1000001"

Tom Werner

8/16/2007 8:40:00 PM

0

John Joyce wrote:
>
> On Aug 16, 2007, at 3:22 PM, Stefano Crocco wrote:
>
>> Alle giovedì 16 agosto 2007, John Joyce ha scritto:
>>> On Aug 16, 2007, at 2:57 PM, Wolfgang Nádasi-Donner wrote:
>>>> John Joyce wrote:
>>>>> Hmm... I can turn a number into a character with .chr
>>>>> I can convert my numbers with .to_s(2) ((where 2 is a radix, or
>>>>> base))
>>>>> But how do I convert numbers to characters??
>>>>
>>>> I don't understand what you mean - "to_s(b)" builds a string. What do
>>>> you mean by "convert numbers to characters" (example).
>>>>
>>>> Wolfgang Nádasi-Donner
>>>> --
>>>> Posted via http://www.ruby-....
>>>
>>> yes it builds a string representation of a number.
>>> I want to take a hex or binary number though and return a character.
>>> 0x49 for example is "I"
>>
>> I think you need Integer#chr:
>>
>> 0x49.chr
>> => 'I'
>>
>> Stefano
>>
> Oops, no, that's not what I need. I can do that.
> I need to take "I" or "J" or whatever character and convert to hex! or
> binary
> (sorry, I'm a little sleepy now)
>

Ah well in that case:

?I
# => 73

?I.to_s(2)
# => "1001001"

?I.to_s(16)
# => "49"

Tom Preston-Werner

--
* Libraries:
Chronic (chronic.rubyforge.org)
God (god.rubyforge.org)
* Site:
rubyisawesome.com


John Joyce

8/16/2007 8:50:00 PM

0


On Aug 16, 2007, at 3:36 PM, Adam Shelly wrote:

> On 8/16/07, John Joyce <dangerwillrobinsondanger@gmail.com> wrote:
>> Oops, no, that's not what I need. I can do that.
>> I need to take "I" or "J" or whatever character and convert to hex!
>> or binary
>> (sorry, I'm a little sleepy now)
>>
> like this?
>
>
> irb(main):011:0> s = "A"
> => "A"
> irb(main):012:0> s[0]
> => 65
> irb(main):013:0> c = ?A
> => 65
> irb(main):014:0> s[0].to_s(16)
> => "41"
> irb(main):015:0> c.to_s(16)
> => "41"
> irb(main):016:0> c.to_s(2)
> => "1000001"
>
Hmm... that first technique is useful! Returning elements of a string.

John Joyce

8/16/2007 9:29:00 PM

0


On Aug 16, 2007, at 3:27 PM, Tom Werner wrote:

> John Joyce wrote:
>> Hmm... I can turn a number into a character with .chr
>> I can convert my numbers with .to_s(2) ((where 2 is a radix, or
>> base))
>> But how do I convert numbers to characters??
>>
>>
>
> You may be looking for Array#pack. It's useful for converting an
> array from one encoding to another. One of these possibilities is
> to convert an array of numbers into a string:
>
> n = [65, 66, 67]
> n.pack("c*")
> #=> "ABC"
>
>
> Pack (and unpack) can do tons of useful and surprising things.
>
> Tom
>
The creator of god! I must listen carefully.

I after some reading, I'm starting to think about using the files
signatures used by DROID / PRONOM
Anyone know much about that stuff? Any Ruby implementations/bindings?