I am trying to implement the following C++ code in Ruby:
sprintf (tmp, "|%02x", Payload[i] & 0xFF);
Basically, all bytes in the payload data whose value is not a code of letters or numbers in the US-ASCII code table, are encoded as three bytes:
| X1 X2
where '|' is a byte with the value of code of character '|' in the US-ASCII code table (0x7C), 'X1' is a first hexadecimal digit of the code of the byte, and 'X2' is a second hexadecimal digit of the code of the byte.
Up until now, I have coded this:
payload.each_char do |char|
if char.match(/^[[:alnum:]]$/)
encoded_string << char && next
end
encoded_string << sprintf("|%02x", char.hex);
end
Problem is, this does not work correctly for special symbols like '*' and others.
Any ideas are much appreciated.