简体   繁体   English

使用Java将较大值的十六进制字符串转换为字节

[英]Convert larger value hex string into bytes using java

I am trying to convert a larger value of hex string into bytes. 我正在尝试将十六进制字符串的较大值转换为字节。 The information which is get about converting signed bytes is the max is 0x7F equals to 127 only. 有关转换带符号字节的信息最大为0x7F,仅等于127。 But now mine I want to convert hexa value C6 into bytes which i should receive 198. Are there any ways to do this? 但是现在我想将六值C6转换为我应该接收198的字节。有什么方法可以做到这一点?

Currently I have tested my code using this methods: 目前,我已经使用以下方法测试了我的代码:

  1. static byte[] bytearray = {0x02, 0x08, 0x16, 0x00, 0x00, 0x33, (byte)(Integer.parseInt("C6",16) & 0xff), 0x1B};
  2. static byte[] bytearray = {0x02, 0x08, 0x16, 0x00, 0x00, 0x33, (byte)0xC6, 0x1B};

All of this method giving me the same value -58 only. 所有这些方法仅给我相同的值-58。

I appreciate if someone can help me with this. 如果有人可以帮助我,我将不胜感激。 Thank you 谢谢

Use method 2 and don't worry about the negative values in your byte array. 使用方法2,不必担心字节数组中的负值。 Bytes are signed in java, so if you want to process your bytes as 0 to 255 instead of -128 to 127, AND each byte against 0xFF. 字节是用Java签名的,因此,如果您想将字节处理为0到255而不是-128到127,并且将每个字节与0xFF相对。 This will promote the byte to an integer and will be a value from 0 - 255. 这会将字节提升为整数,并且将是0到255之间的值。

UPDATE 更新

Seeing a comment about how you're going to send this through a serial port, there is nothing wrong with your byte array. 看到有关如何通过串行端口发送此消息的注释,字节数组没有任何问题。 The receiver (if it's another Java program) will have to process the bytes by ANDing them against 0xFF. 接收方(如果是另一个Java程序)将必须通过将字节与0xFF进行“与”运算来处理这些字节。 Another serial program (in C# for example) would receive bytes (0x00 - 0xFF) 另一个串行程序(例如,在C#中)将接收字节(0x00-0xFF)

public static void main(String[] args) throws Exception {
    byte[] bytearray = {0x02, 0x08, 0x16, 0x00, 0x00, 0x33, (byte)0xC6, 0x1B};
    for (byte b : bytearray) {
        System.out.printf("%d ", b);
    }
    System.out.println();

    for (byte b : bytearray) {
        System.out.printf("%d ", b & 0xFF);
    }
}

Output: 输出:

2 8 22 0 0 51 -58 27 
2 8 22 0 0 51 198 27 

OLD

public static void main(String[] args) throws Exception {
    System.out.println(Byte.MIN_VALUE);
    System.out.println(Byte.MAX_VALUE);

    System.out.println(Byte.MIN_VALUE & 0xFF);
    System.out.println((byte)-1 & 0xFF);
    System.out.println((byte)-10 & 0xFF);
    System.out.println((byte)-58 & 0xFF);
}

Output: 输出:

-128
127
128
255
246
198

It's correct because bytes are signed in java. 这是正确的,因为字节是在Java中签名的。 If you need it between 0-255 instead of -128 to 127, you nede to use a short or an int (or a long, I suppose.) 如果您需要在0-255之间而不是-128到127之间,则需要使用short或int(我想是long)。

That being said, you can still send it across in that format, but when you use it, you'll need to make use of it as an int. 话虽如此,您仍然可以通过该格式发送它,但是当您使用它时,您将需要将它用作int。 Otherwise you'll get negative numbers forever. 否则,您将永远得到负数。

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

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