java中整数到字节十六进制的转换
我正在尝试开发一个java卡应用程序。将有一个动态输入字符串,其中包含字节数组中的电话号码,如:java中整数到字节十六进制的转换,java,arrays,javacard,Java,Arrays,Javacard,我正在尝试开发一个java卡应用程序。将有一个动态输入字符串,其中包含字节数组中的电话号码,如: byte[] number =new byte[] {1,2,3,4,5,6,7,8,9,5}; 我希望将此数组更改为以下数组: byte[] changed num = {(byte)0x0C, (byte)0x91, (byte)0x19, (byte)0x21, (byte)0x43, (byte)0x65, (byte)0x87, (byte)0x59} 其中,前三个字节将始终相同,其
byte[] number =new byte[] {1,2,3,4,5,6,7,8,9,5};
我希望将此数组更改为以下数组:
byte[] changed num = {(byte)0x0C, (byte)0x91, (byte)0x19, (byte)0x21, (byte)0x43, (byte)0x65, (byte)0x87, (byte)0x59}
其中,前三个字节将始终相同,其余5个字节将从传入阵列更新
我尝试了以下方法:
public static void main(String args[]){
byte[] number =new byte[] {1,2,3,4,5,6,7,8,9,5};
byte[] changednum = new byte[8];
changednum[0] = (byte)0x0C;
changednum[1] = (byte)0x91;
changednum[2] = (byte)0x19;
changednum[3] = (byte)0x(number[0] + number[1]*10);
changednum[4] = (byte)0x(number[2] + number[3]*10);
changednum[5] = (byte)0x(number[4] + number[5]*10);
changednum[6] = (byte)0x(number[6] + number[7]*10);
changednum[7] = (byte)0x(number[8] + number[9]*10);
System.out.println(Arrays.toString(changednum));
}
}
但最后5个值未转换为字节值
s、 您可以使用Integer.toHexString方法进行转换,
您需要仔细注意,将显式整数转换为-128到127之间的字节
final int f = -2211;
System.out.println(f);
System.out.println((byte) f);
System.out.println(Integer.toHexString((byte) f));
这条线
changednum[3] = (byte)0x(number[0] + number[1]*10);
可以通过一组复杂的字符串操作来完成,简单的数学可以实现你想要的
changednum[3] = (byte)(number[0] + number[1]*16);
需要*16,因为您似乎假设数字是十六进制的
你可以使用一个循环
for (int i = 0; i < 4; i++)
changednum[i+3] = (byte)(number[i*2] + number[i*2+1]*16);
或者使用+=来避免强制转换
for (int i = 0; i < 4; i++)
changednum[i+3] += number[i*2] + number[i*2+1] * 16;
或者你可以写
for (int i = 0; i < 4; i++)
changednum[i+3] += number[i*2] + (number[i*2+1] << 4);
在这种情况下,虽然在Java的标准版中,性能不是很重要,但在Java卡中,性能通常是至关重要的。由于采用了位运算符,此解决方案比公认的答案略快,并且它是一个没有32位整数的有效Java卡代码:
for (short i = 3, j = 0; i < 7; i++, j += 2) {
changednum[i] = (byte) ((number[j+1] << 4) | (number[j] & 0x0F));
}
在我看来,其他答案缺乏可读性 此解决方案使用单独的方法和ByteBuffer使这些方法在不通过偏移和其他管道的情况下工作。它有一些奇怪的东西,比如常量和经过深思熟虑的标识符、异常、JavaDoc和《可维护性》一书中其他可怕的概念
package nl.owlstead.stackoverflow;
import java.nio.BufferOverflowException;
import java.nio.ByteBuffer;
/**
* Helper class to create telephone numbers with headers for Java card using
* reverse packed BCD format.
*
* @param buffer
* a buffer with enough space for the 3 byte header
*/
public final class OverlyDesignedTelephoneNumberHelper {
private static final int TELEPHONE_NUMBER_SIZE = 10;
private static final int NIBBLE_SIZE = 4;
private static final byte[] HEADER = { (byte) 0x0C, (byte) 0x91,
(byte) 0x19 };
/**
* Adds the header for the telephone number to the given buffer.
*
* @param buffer
* a buffer with enough space for the 3 byte header
* @throws NullPointerException
* if the buffer is null
* @throws BufferOverflowException
* if the buffer doesn't have enough space for the header
*/
private static void addTelephoneNumberHeader(final ByteBuffer buffer) {
buffer.put(HEADER);
}
/**
* Adds the telephone number to the given buffer.
*
* @param buffer
* a buffer with enough space for the 3 byte header
* @param number
* the number in BCD format, should be 10 bytes in size
* @throws IllegalArgumentException
* if the number is null or doesn't contain 10 BCD digits
* @throws NullPointerException
* if the buffer is null
* @throws BufferOverflowException
* if the buffer doesn't have enough space for the telephone
* number
*/
private static void addTelephoneNumber(final ByteBuffer buffer,
final byte[] number) {
if (number == null || number.length != TELEPHONE_NUMBER_SIZE) {
throw new IllegalArgumentException("Expecting 10 digit number");
}
for (int i = 0; i < number.length; i += 2) {
final byte lowDigit = number[i];
validateUnpackedBCDDigit(lowDigit);
final byte highDigit = number[i + 1];
validateUnpackedBCDDigit(highDigit);
buffer.put((byte) ((highDigit << NIBBLE_SIZE) | lowDigit));
}
}
/**
* Tests if the given unpacked BCD digit is within range.
*
* @param b
* the byte to test
* @throws IllegalArgumentException
* if it isn't
*/
private static void validateUnpackedBCDDigit(final byte b) {
if (b < 0 || b > 9) {
throw new IllegalArgumentException(
"Telefonenumber isn't all bytes representing digits in BCD");
}
}
public static void main(final String... args) {
final byte[] number = new byte[] { 1, 2, 3, 4, 5, 6, 7, 8, 9, 5 };
final ByteBuffer buf = ByteBuffer.allocate(HEADER.length
+ TELEPHONE_NUMBER_SIZE);
addTelephoneNumberHeader(buf);
addTelephoneNumber(buf, number);
buf.flip();
while (buf.hasRemaining()) {
System.out.printf("%02X", buf.get());
}
System.out.println();
}
private OverlyDesignedTelephoneNumberHelper() {
// avoid instantiation
}
}
一些解释会很好。是0xnumber[0]+number[1]*10;语法真的有效吗?不是。但这正是我想要实现的。Yuk,你先把数字分成对,然后用BCD编码,同时把字节放在小尾端模式,用半字节作为基类型?为什么???您想编写java卡代码,您使用的是公共静态void mainString args[],这显然不是java卡…@AdityaParsai不清楚代码是否需要在java卡中,或者是否需要在发送到java卡之前进行转换,即在java SE中。你能澄清一下吗?在这种情况下,我更喜欢@MaartenBodewes,因为结果是一样的,但是shift的优先级比乘法低,所以你需要更多的括号。请注意,在JavaCard中,整数支持是可选的,如果我没记错的话,至少在2.2.2之前是可选的。除了字节已经签名,所以根据你的兴趣,你应该尝试使用短裤,因为情况下数字大于0x80.@ jLaZa,不管你认为该值是签名还是未签名,都是一个你如何读取值的问题。b&0xFF是无符号的。@PeterLawrey它是无符号的,但如果您这样做,例如字节a=字节0x80&0xFF,然后检查b<0,则为真。这就是我的意思,@AdityaParsai应该意识到这一点。抱歉误会了。小心,这是给java卡的@vlp我把它理解为:我正在尝试开发一个java卡应用程序。将有一个动态输入字符串,该字符串将在字节数组中包含电话号码,如:。所以我假设输入需要在终端端更改,而不是在Java卡本身中。还要注意问题标题,它表示Java,而不是Java卡。这也解释了问题中的主要方法。最后请注意,被接受的andwer也不能直接与Java卡兼容。你可能是对的-我为垃圾邮件感到抱歉you@vlp我也可以骗我。我不得不把这个问题再读几遍以确定答案。再次向我发送垃圾邮件: