[lnkForumImage]
TotalShareware - Download Free Software

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


 

Forums >

comp.lang.ruby

constructing a string from hex value

Srdjan Marinovic

9/6/2006 2:05:00 PM

hi,

How do I construct a string out of hex values?
In other words I want to give exact hex value for each byte in the
string. So I would have soemthing like

0x2a, 0x7e, 0x0a, 0x7e

If I go through Fixnum I get my string to contain numbers 48, 115, 10,
115 but I do not want that but rather a string of ASCII(48),
ACII(115),...

Thanks

srdjan

5 Answers

Florian Frank

9/6/2006 2:24:00 PM

0

Srdjan Marinovic wrote:
> 0x2a, 0x7e, 0x0a, 0x7e

[ 0x2a, 0x7e, 0x0a, 0x7e ].pack 'C*'

--
Florian Frank

Tim Hunter

9/6/2006 2:30:00 PM

0

Srdjan Marinovic wrote:
> hi,
>
> How do I construct a string out of hex values?
> In other words I want to give exact hex value for each byte in the
> string. So I would have soemthing like
>
> 0x2a, 0x7e, 0x0a, 0x7e
>
> If I go through Fixnum I get my string to contain numbers 48, 115, 10,
> 115 but I do not want that but rather a string of ASCII(48),
> ACII(115),...
>
> Thanks
>
> srdjan

irb(main):001:0> p = "\x2a\x7e\x0a\x7e"
=> "*~\n~"
irb(main):002:0>

--
Posted via http://www.ruby-....

Daniel Martin

9/6/2006 2:38:00 PM

0

"Srdjan Marinovic" <srdjan.marinovic@gmail.com> writes:

> How do I construct a string out of hex values?
> In other words I want to give exact hex value for each byte in the
> string. So I would have soemthing like
>
> 0x2a, 0x7e, 0x0a, 0x7e
>
> If I go through Fixnum I get my string to contain numbers 48, 115, 10,
> 115 but I do not want that but rather a string of ASCII(48),
> ACII(115),...

irb(main):001:0> a = [ 0x2a, 0x7e, 0x0a, 0x7e ]
=> [42, 126, 10, 126]
irb(main):002:0> a.map{|x|x.chr}.join
=> "*~\n~"

If all you have is the string representation of the hex values (say,
if you're reading hex values from a file), you have to do just a tiny
bit more to first convert the hex values into integers:

irb(main):001:0> a = %w[ 2a 7e 0a 7e ]
=> ["2a", "7e", "0a", "7e"]
irb(main):002:0> a.map{|x|x.hex.chr}.join
=> "*~\n~"

But only just barely more.

--
s=%q( Daniel Martin -- martin@snowplow.org
puts "s=%q(#{s})",s.map{|i|i}[1] )
puts "s=%q(#{s})",s.map{|i|i}[1]

Srdjan Marinovic

9/6/2006 3:02:00 PM

0

thanks a lot for you help

On 9/6/06, Florian Frank <flori@nixe.ping.de> wrote:
> Srdjan Marinovic wrote:
> > 0x2a, 0x7e, 0x0a, 0x7e
>
> [ 0x2a, 0x7e, 0x0a, 0x7e ].pack 'C*'
>
> --
> Florian Frank
>
>

Ara.T.Howard

9/6/2006 3:11:00 PM

0