0

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.

4

1 に答える 1

0

テストする1.8.7はありませんが、これは1.9.3で機能し、使用されるすべてのメソッドは1.8.7である必要があります。

payload.each_byte do |byte|
  if byte.chr.match(/^[[:alnum:]]$/) 
    encoded_string << byte.chr && next 
  end
  encoded_string << sprintf("|%02x", byte);  
end

結果"hello world***"

hello|20world|2a|2a|2a
于 2012-12-18T17:21:05.030 に答える