如何在Ruby中将字符串转换为字节?

时间:2021-09-25 21:44:28

How do I extend the String class, and attach a method named to_bytes?

如何扩展String类,并附加一个名为to_bytes的方法?

3 个解决方案

#1


23  

Ruby already has a String#each_byte method which is aliased to String#bytes.

Ruby已经有一个字符串#each_byte方法,它被别名为字符串#bytes。

Prior to Ruby 1.9 strings were equivalent to byte arrays, i.e. a character was assumed to be a single byte. That's fine for ASCII text and various text encodings like Win-1252 and ISO-8859-1 but fails badly with Unicode, which we see more and more often on the web. Ruby 1.9+ is Unicode aware, and strings are no longer considered to be made up of bytes, but instead consist of characters, which can be multiple bytes long.

在Ruby 1.9字符串之前,字符串相当于字节数组,即假定字符为单个字节。对于ASCII文本和各种文本编码(如Win-1252和ISO-8859-1)来说,这是没问题的,但在Unicode(我们在web上看到的越来越多)上,它却很糟糕。Ruby 1.9+支持Unicode,字符串不再被认为是由字节组成的,而是由字符组成,字符可以是多个字节长。

So, if you are trying to manipulate text as single bytes, you'll need to ensure your input is ASCII, or at least a single-byte-based character set. If you might have multi-byte characters you should use String#each_char or String.split(//) or String.unpack with the U flag.

因此,如果您试图以单个字节来操作文本,那么您需要确保您的输入是ASCII,或者至少是一个基于单字节的字符集。如果您可能有多字节字符,那么您应该使用String#each_char或String.split(//)或String。打开美国国旗。


What does // mean in String.split(//)

//在字符串中是什么意思?

// is the same as using ''. Either tells split to return characters. You can also usually use chars.

//和use是一样的。要么告诉split返回字符。你也可以使用chars。

#2


36  

String#bytes returns enumerator through string bytes. .to_a can convert it to an array.

字符串#bytes通过字符串字节返回枚举器。.to_a可以将其转换为数组。

"asd".bytes.to_a
=> [97, 115, 100]

#3


0  

With the help of unpack we can convert the string into any format:- byte, bite(MSB,LSB), ASCII or hex. please go through this link:- http://blog.bigbinary.com/2011/07/20/ruby-pack-unpack.html. To convert string into bytes:-

在解包的帮助下,我们可以将字符串转换成任何格式:- byte, bite(MSB,LSB), ASCII或十六进制。请通过这个链接:- http://blog.bigbinary.com/2011/07/20/ruby-pack-unpack.html。将字符串转换成字节:-

"abcde".unpack('c*')  
=> [97, 98, 99, 100, 101]

#1


23  

Ruby already has a String#each_byte method which is aliased to String#bytes.

Ruby已经有一个字符串#each_byte方法,它被别名为字符串#bytes。

Prior to Ruby 1.9 strings were equivalent to byte arrays, i.e. a character was assumed to be a single byte. That's fine for ASCII text and various text encodings like Win-1252 and ISO-8859-1 but fails badly with Unicode, which we see more and more often on the web. Ruby 1.9+ is Unicode aware, and strings are no longer considered to be made up of bytes, but instead consist of characters, which can be multiple bytes long.

在Ruby 1.9字符串之前,字符串相当于字节数组,即假定字符为单个字节。对于ASCII文本和各种文本编码(如Win-1252和ISO-8859-1)来说,这是没问题的,但在Unicode(我们在web上看到的越来越多)上,它却很糟糕。Ruby 1.9+支持Unicode,字符串不再被认为是由字节组成的,而是由字符组成,字符可以是多个字节长。

So, if you are trying to manipulate text as single bytes, you'll need to ensure your input is ASCII, or at least a single-byte-based character set. If you might have multi-byte characters you should use String#each_char or String.split(//) or String.unpack with the U flag.

因此,如果您试图以单个字节来操作文本,那么您需要确保您的输入是ASCII,或者至少是一个基于单字节的字符集。如果您可能有多字节字符,那么您应该使用String#each_char或String.split(//)或String。打开美国国旗。


What does // mean in String.split(//)

//在字符串中是什么意思?

// is the same as using ''. Either tells split to return characters. You can also usually use chars.

//和use是一样的。要么告诉split返回字符。你也可以使用chars。

#2


36  

String#bytes returns enumerator through string bytes. .to_a can convert it to an array.

字符串#bytes通过字符串字节返回枚举器。.to_a可以将其转换为数组。

"asd".bytes.to_a
=> [97, 115, 100]

#3


0  

With the help of unpack we can convert the string into any format:- byte, bite(MSB,LSB), ASCII or hex. please go through this link:- http://blog.bigbinary.com/2011/07/20/ruby-pack-unpack.html. To convert string into bytes:-

在解包的帮助下,我们可以将字符串转换成任何格式:- byte, bite(MSB,LSB), ASCII或十六进制。请通过这个链接:- http://blog.bigbinary.com/2011/07/20/ruby-pack-unpack.html。将字符串转换成字节:-

"abcde".unpack('c*')  
=> [97, 98, 99, 100, 101]