概述
2019独角兽企业重金招聘Python工程师标准>>>
https://stackoverflow.com/questions/17623668/what-does-string-packh-mean
It interprets the string as hex numbers, two characters per byte, and converts it to a string with the characters with the corresponding ASCII code:
["464F4F"].pack('H*')
# =>
"FOO", 0x46 is the code for 'F', 0x4F the code for 'O'
把这个字符串当成是16进制,而且每两个字符当成是一个数,正好好一个字节,按照ASCII编码。
For the opposite conversion, use unpack
:
'FOO'.unpack('H*')
# => ["464f4f"]
It is a little bit more difficult for non-ASCII-8BIT encodings:
"á".encoding
# => #<Encoding:UTF-8>
"á".unpack('H*')
# => ["c3a1"]
['c3a1'].pack('H*')
# => "xC3xA1"
['c3a1'].pack('H*').encoding
# => #<Encoding:ASCII-8BIT>
['c3a1'].pack('H*').force_encoding('UTF-8') # => "á"
转载于:https://my.oschina.net/u/855913/blog/1614719
最后
以上就是鲤鱼豆芽为你收集整理的What does [“string”].pack('H*') mean?的全部内容,希望文章能够帮你解决What does [“string”].pack('H*') mean?所遇到的程序开发问题。
如果觉得靠谱客网站的内容还不错,欢迎将靠谱客网站推荐给程序员好友。
本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
发表评论 取消回复