Hello,
I need to decode a utf-8 string to hexadecimal. After a lot of search I
found the unpack method of the string class, it can be used like:
“f”.unpack(‘U’)[0].to_s(16)
=> “66”
This method seems to decode only one character and returns the decoded
value
as an array. The problem is that I need to decode long strings and this
method seems inefficient when dealing with long strings.
Do you guys have any idea? I don’t think I should convert my string
characters into arrays, convert them one by one, then join them as an
array
of converted characters then convert them to a string again !!!
You can still use unpack to do this as it’s argument will translate as
many characters from the string as there are items to correspond in the
argument. This is ugly when applying to a litteral, but to a variable,
not quite so:
Hi,
It seems to get uglier now ! What if I want to exclude numbers from
being
converted, I’ve been trying to do this all the day but I failed, I tried
different approaches, this is the last one:
number = /\d/
s = “hello5”
s.unpack(‘U’*s.length).collect {|x|
if x !=~ number
x.to_s 16
end
}
I tried more complex methods but they all failed, the result of the
above
code is:
=> [“68”, “65”, “6c”, “6c”, “6f”, “35”]
While I need to reach the following:
=> [“68”, “65”, “6c”, “6c”, “6f”, “5”]
??
I’ve tested the regular expression in a separate code and it looks to
work,
but when using it here … I don’t know what’s the problem.
So this will check the value of x against the regex, if the value of x
is a
digit no change is made else it will be translated into hex, is that
right?
I have a question, what does split(//) do?
So this will check the value of x against the regex, if the value of x
is a
digit no change is made else it will be translated into hex, is that
right?
Yes, that’s right.
I have a question, what does split(//) do?
#split will split a string into an array of sub-strings at places which
match the argument. If the argument is an empty regular expression,
then it splits at each letter. See the documentation for more details: