after decrypting a hash, method decrypt(hash[field])
is returning:
"100\xE2\x80\x90111\xE2\x80\x9010333\xE2\x80\x900012"
but using puts decrypt(hash[field])
gives correct result as
'100‐111‐10333‐0012'
how to resolve this issue?
CodePudding user response:
Your decrypt
method is returning binary data, not the UTF-8 encoded string you're expecting. The bytes "\xE2\x80\x90"
are ‐
:
puts '‐'.bytes.map { |b| b.to_s(16) }
# e2
# 80
# 90
or:
puts '‐'.force_encoding('binary').inspect
# "\xE2\x80\x90"
If you're sure you're getting a UTF-8 string, then fix the encoding:
# UTF-8 is the default so we force binary to start
bytes = "100\xE2\x80\x90111\xE2\x80\x9010333\xE2\x80\x900012".force_encoding('binary')
# "100\xE2\x80\x90111\xE2\x80\x9010333\xE2\x80\x900012"
puts bytes.force_encoding('utf-8')
# 100‐111‐10333‐0012