字节数组已经是一组位的二进制表示(就像一个短数组一样)。位数组的唯一区别是您将始终以 8 个为一组获得位,并且如果不进行一些按位操作,您将无法单独寻址每个位。
通常,a String
(一段文本)中的位表示不在卡片上执行。它是在比特传输到终端上的应用程序之后执行的。您可能已经注意到 Java Card classic 上没有该String
类型。
根据要求,一些方法可以将 bitjes(荷兰语中的位)转换为 Java Card 的字符。谢谢你的谜题。
/**
* Converts an array of bytes, used as a container for bits, into an ASCII
* representation of those bits.
*
* @param in
* the input buffer
* @param inOffset
* the offset of the bits in the input buffer
* @param bitSize
* the number of bits in the input buffer
* @param out
* the output buffer that will contain the ASCII string
* @param outOffset
* the offset of the first digit in the output buffer
* @return the offset directly after the last ASCII digit
*/
public static final short toBitjes(final byte[] in, short inOffset,
final short bitSize, final byte[] out, short outOffset) {
for (short i = 0; i < bitSize; i++) {
final byte currentByte = in[inOffset + i / BYTE_SIZE];
out[outOffset++] = (byte) (0x30 + ( (currentByte >> (7 - (i % BYTE_SIZE))) & 1 ) );
}
return outOffset;
}
当然,随意重命名“bitjes”。
用法:
// buffer containing 9 bits valued 10011001.1 in middle of buffer
final byte[] inBuffer = new byte[] { 0x00, (byte) 0x99, (byte) 0x80, 0x00 };
final short bitSize = 9;
// bit array starting at byte 1
final short inOffset = 1;
// or use JCSystem#makeTransientByteArray()
final byte[] outBuffer = new byte[40];
// ASCII start at offset 2
final short outOffset = 2;
// newOffset will be 2 + 9 = 11
short newOffset = toBitjes(inBuffer, inOffset, bitSize, outBuffer, outOffset);