简体   繁体   English

如何在Java中将字符串转换为32位对齐的字节数组?

[英]How to convert string to a 32-bit aligned byte array in java?

The title itself might not be self-explaining. 标题本身可能无法解释。 Let me show an example: 让我举一个例子:

_test is 5f 74 65 73 74 (totally 5 bytes in ascii) _test5f 74 65 73 74 (ASCII中总共5个字节)

However for some reason I need to pack it into a byte array like: 但是由于某种原因,我需要将其打包成一个字节数组,例如:

5f 74 65 73 74 00 00 00

(the last 3 bytes of 0 are padding bytes to do the alignment) (0的最后3个字节是填充字节以进行对齐)

In other words, I need to calculate the number of padding bytes before I use write() to write those bytes one by one. 换句话说,在使用write()一一写入这些字节之前,我需要计算填充字节的数量。

I can use this way to get the number I want: (4 - str.length() % 4) % 4 but I'm curious if there is another efficient way to get that (eg bit-wise operation). 我可以使用这种方法来获取所需的数字: (4 - str.length() % 4) % 4但我很好奇是否还有另一种有效的方法来获取该数字(例如按位运算)。

是的,尽管当使用定点CPU时效率更高,而定点CPU会在软件中实现/%等除运算: ((str.length()-1)|3)+1

To figure out the amount of padding to get to a multiple of 4 all we need to look at is the last 2 bits of the length. 为了弄清楚填充量达到4的倍数,我们需要查看的是长度的最后2位。

Last 2 bits   Padding   Padding
 of Length    Needed    in binary
    00          0          00
    01          3          11
    10          2          10
    11          1          01

This suggests a pattern. 这暗示了一种模式。 If the length is even (last bit 0), the padding is equal to the last 2 bits of the length; 如果长度为偶数(最后一位0),则填充等于长度的最后2位;否则为0。 otherwise it's the last 2 bits with the first bit flipped. 否则,它是最后2位,第一位翻转。

This can be expressed as 这可以表示为

(len & 0x03) ^ ((len & 0x01) << 1)

Ie take the last 2 bits of the length and xor with the last bit shifted 1 bit to the left. 即,取长度的最后2位,然后将最后一位向左移动1位,即xor。

NOTE: If you do this, be sure to comment it well in the code so the next person who has to read this doesn't have to spend 10 minutes figuring it out. 注意:如果执行此操作,请确保在代码中对其进行注释,这样,下一个必须阅读此内容的人员就不必花费10分钟的时间来弄清楚它。

str.length() is not the same as the number of bytes. str.length()与字节数不同。 This is only true if you use ASCII strings. 仅当您使用ASCII字符串时才如此。 If oyu have non-ASCII string (German, French, ...), then the correct way to determine the number of bytes is following: 如果oyu具有非ASCII字符串(德语,法语,...),则确定字节数的正确方法如下:

int nBytes = str.getBytes("UTF8").length();

Determining the 32-bit aligned length is trivial. 确定32位对齐长度是微不足道的。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM