How to change ASCI to Hex

hi everyone:
I want to change ASCI to Hex,which i receive in the UDPSocket,but it
just report error

  1. my code is:
    server = UDPSocket.open
    server.bind(‘192.168.203.108’, $port)
    s= server.recvfrom(164)
    s=s[0]
    s=Integer(s)
    p s

  2. the error report:
    in `Integer’: string contains null byte (ArgumentError)

Zqd Z. wrote:

hi everyone:
I want to change ASCI to Hex,which i receive in the UDPSocket,but it
just report error

  1. my code is:
    server = UDPSocket.open
    server.bind(‘192.168.203.108’, $port)
    s= server.recvfrom(164)

I think this is what you’re asking for:

  s.unpack("C")[0]

--------------------------------- Joel VanderWerf
[email protected]
removed_email_addr[email protected], [email protected] (ruby-talk ML)
how to change ASCI to Hex

I think this is what you’re asking for:

  s.unpack("C")[0]


vjoel : Joel VanderWerf : path berkeley edu : 510 665 3407

Joel, In Ruby 1.9 you’re right, but with Ruby 1.8 no need to unpack,
because str[n] returns just one character at the index points

Then,
server = UDPSocket.open
server.bind(‘192.168.203.108’, $port)
s= server.recvfrom(164)
s=s[0]
p s

--------------------------------- Joel VanderWerf
[email protected]
[email protected], [email protected] (ruby-talk ML)
how to change ASCI to Hex

server = UDPSocket.open
because str[n] returns just one character at the index points

Anyway, I’m guessing the next question will be “how do I get a long from
positions 3…7?”, and so might as well get started using unpack…

you may see the version invoking ruby with -v option
ie)
c:>ruby -v
ruby 1.9.0 (2008-08-26 revision 18849) [i386-mswin32]

I’m guessing the next question will be “how do I get a long from
positions 3…7?”, and so might as well get started using unpack…
Um, you are correct. I respect you.

arton wrote:

server.bind(‘192.168.203.108’, $port)

Then,
server = UDPSocket.open
server.bind(‘192.168.203.108’, $port)
s= server.recvfrom(164)
s=s[0]
p s

True, but that’s what the OP tried and got an error. Perhaps OP’s ruby
is 1.9?

Anyway, I’m guessing the next question will be “how do I get a long from
positions 3…7?”, and so might as well get started using unpack…

thanks for answering

my ruby version is 1.8.6

well,when i receive and print,it it
““[email protected]\002\001\000\004\006public\2443\006\006+\006\001\004\001\[email protected]\004\300\250\313\031\002\001\006\002\001*C\002090\0310\027\006\b+\006\001\002\001\001\005\000\004\vSystem
Name\n””

how can i print it in Hex?

arton wrote:

--------------------------------- Joel VanderWerf
[email protected]
[email protected], [email protected] (ruby-talk ML)
how to change ASCI to Hex

server = UDPSocket.open
because str[n] returns just one character at the index points

Anyway, I’m guessing the next question will be “how do I get a long from
positions 3…7?”, and so might as well get started using unpack…

you may see the version invoking ruby with -v option
ie)
c:>ruby -v
ruby 1.9.0 (2008-08-26 revision 18849) [i386-mswin32]

I’m guessing the next question will be “how do I get a long from
positions 3…7?”, and so might as well get started using unpack…
Um, you are correct. I respect you.

2008/9/7 Zqd Z. [email protected]:

thanks for answering

my ruby version is 1.8.6

well,when i receive and print,it it
““[email protected]\002\001\000\004\006public\2443\006\006+\006\001\004\001\[email protected]\004\300\250\313\031\002\001\006\002\001*C\002090\0310\027\006\b+\006\001\002\001\001\005\000\004\vSystem
Name\n””

how can i print it in Hex?

s.unpack(‘H*’)
Or
s.unpack(‘C*’).map{|x| ’ %02x’ % x}.join

Regards,

Park H.

thanks for all~~~~~~~

i have resolve with your help:

line=line.unpack(“H*”)

it work,3ks!

--------------------------------- Zqd Z. [email protected]
[email protected], [email protected] (ruby-talk ML)
how to change ASCI to Hex

thanks for answering

my ruby version is 1.8.6

well,when i receive and print,it it
““[email protected]\002\001\000\004\006public\2443\006\006+\006\001\004\001\[email protected]\004\300\250\313\031\002\001\006\002\001*C\002090\0310\027\006\b+\006\001\002\001\001\005\000\004\vSystem
Name\n””

how can i print it in Hex?

p data.unpack(‘H*’)[0]

unpack specifier H measn per 4 bit decode (into 0-F)
* means whole size

This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.

| Privacy Policy | Terms of Service | Remote Ruby Jobs