I am using Ruby 1.8.7 (and upgrading isn't an option). I would like to create a string of all UTF-8 code points from 0 to 127, written as "\uXXXX".
My problem is that this is being interpreted as (for example): 'u0008'. If I try to use '\u0008', the string becomes "\u0008" which IS NOT what I want.
I have tried many different ways, but it seems impossible to create a string that is exactly just "\uXXXX" ie. "\u000B". it always is either "\u000B" or "u000B"
开发者_如何学GoEscaping the '\' isn't an option. I need to send a string to a server, such that the server will receive '\u000B' for example. It is so that other server can test its parsing of the \uXXXX syntax. This seems impossible to do in Ruby however.
Happy if someone can prove me wrong :)
Use Integer #
chr
to get the character. Here's a clean version:
(1..127).each do |i|
value << "U+#{i} = #{i.chr}, hex = \\x#{"%02x" % i}; "
end
The "%02x" % i
is the equal to sprintf("%02x", i)
. It returns the integer as a 2-digit hexadecimal number.
Escaped output (see comments):
(1..127).each do |i|
value << "U+#{i} = \\u#{"%04x" % i}, hex = \\x#{"%02x" % i}; "
end
精彩评论