Newb on String to Hex for UDP packet

Guys im a total newb to ruby and i have been battling with something
that should be very simple, help!

the situation is this: i need to feed a program via user input an int or
worst case a hex string…

eg: Please enter last octect of IP address: user enters:123 or 7B

i then want to include that as a hex byte in a UDP packet and send it.

Eg: UDP = “/x00/x12/x23/(insert value in hex here)/x00/x00”

I managed to include the value in there but the resulting UDP packet is
sending the value in asccii,
the packet looks like: 00:12:23:31:32:33:00:00

i have tried a bunch of variations and nothing…

any help is appreciated.

On 06/29/2012 04:55 PM, ricky quirch wrote:

Guys im a total newb to ruby and i have been battling with something
that should be very simple, help!

the situation is this: i need to feed a program via user input an int or
worst case a hex string…

eg: Please enter last octect of IP address: user enters:123 or 7B

How do you determine whether a given number (like 10) is hexadecimal or
decimal without a prefix?

i then want to include that as a hex byte in a UDP packet and send it.

Eg: UDP = “/x00/x12/x23/(insert value in hex here)/x00/x00”

I managed to include the value in there but the resulting UDP packet is
sending the value in asccii,
the packet looks like: 00:12:23:31:32:33:00:00

You need to use backslashes for character escapes in the string, not
forward slashes.

octet = 0x7b

packet = “\x00\x12\x23#{octet.chr}\x00\x00”

packet.bytes.to_a
=> [0, 18, 35, 123, 0, 0]

ricky quirch wrote in post #1066641:

eg: Please enter last octect of IP address: user enters:123 or 7B

i then want to include that as a hex byte in a UDP packet and send it.

Eg: UDP = “/x00/x12/x23/(insert value in hex here)/x00/x00”

I managed to include the value in there but the resulting UDP packet is
sending the value in asccii,
the packet looks like: 00:12:23:31:32:33:00:00

You might want to look at Array#pack

[123].pack(“C”)
=> “{”

[255].pack(“C”)
=> “\377”

[192,168,1,4].pack(“C*”)
=> “\300\250\001\004”

[0x7f000001].pack(“N”)
=> “\177\000\000\001”