Find centralized, trusted content and collaborate around the technologies you use most.
Teams
Q&A for work
Connect and share knowledge within a single location that is structured and easy to search.
我知道我需要使用[5].pack("n")例如。这会产生输出"\x00\x05"
[5].pack("n")
"\x00\x05"
但现在我想打包一个字符串的长度。如果我使用:
"Hello".length.pack("n")
我得到错误
undefined method `pack' for 5:Fixnum (NoMethodError).
我怎样才能解决这个问题?我已经尝试将其转换"Hello".length为整数,"Hello".length.to_i但是我得到了同样的错误。有没有什么办法解决这一问题?
"Hello".length
"Hello".length.to_i
您需要使用数组,而不是整数("Hello".length已经是一个)。
["Hello".length].pack("n") # => "\x00\x05"