解密哈希后,方法decrypt(hash[field])回傳:
"100\xE2\x80\x90111\xE2\x80\x9010333\xE2\x80\x900012"
但使用puts decrypt(hash[field])給出正確的結果
'100‐111‐10333‐0012'
如何解決這個問題?
uj5u.com熱心網友回復:
您的decrypt方法回傳二進制資料,而不是您期望的 UTF-8 編碼字串。位元組"\xE2\x80\x90"是‐:
puts '‐'.bytes.map { |b| b.to_s(16) }
# e2
# 80
# 90
或者:
puts '‐'.force_encoding('binary').inspect
# "\xE2\x80\x90"
如果您確定收到的是 UTF-8 字串,請修復編碼:
# 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
轉載請註明出處,本文鏈接:https://www.uj5u.com/caozuo/487396.html
上一篇:docker 安裝 oracle
