From: Lars Haugseth Date: 2012-06-30T18:06:14+09:00 Subject: Re: Newb on String to Hex for UDP packet 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] -- Lars Haugseth