简体   繁体   English

Java:有整数,需要在0x00格式的字节中

[英]Java: have integer, need it in byte in 0x00 format

I have an integer used to seed my for loop: 我有一个用于为我的for循环播种的整数:

for(int i = 0; i < value; i++)

Within my for loop I am seeding a byte array with byte content values that increment +1. 在我的for循环中,我播种了一个字节数组,其字节内容值增加+1。 For instance new byte[]{0x00}; 例如new byte[]{0x00}; But the 0x00 needs to be 0x01 on the next iteration, how can I convert my value of integer i into a value of byte in the 0x00 format? 但是0x00在下一次迭代时需要为0x01 ,如何将整数i的值转换为0x00格式的byte值?

I tried things like Byte.valueOf(Integer.toHexString(i)) but this just gives me a value that looks like 0 instead of 0x00 . 我尝试过像Byte.valueOf(Integer.toHexString(i))这样的东西,但这只给了我一个看起来像0而不是0x00

new byte[]{0x00}

is actually equivalent to 实际上相当于

new byte[]{0}

The 0x00 notation is just an alternative way to write integer constants, and if the integer constant is in the range -128 to 127, then it can be used as a byte. 0x00表示法只是写整数常量的另一种方法,如果整数常量在-128到127范围内,那么它可以用作一个字节。

If you have an existing integer variable that you want to use, and its value is in the range -128 to 127, then you just have to cast it: 如果您有一个想要使用的现有整数变量,并且其值在-128到127之间,那么您只需要将其强制转换:

int i = 1;
new byte[]{(byte)i};

I think the real problem is that you are confused about number representations and text renderings of numbers. 我认为真正的问题是你对数字表示和数字文本渲染感到困惑。 Here are some key facts that you need to understand: 以下是您需要了解的一些重要事实:

  • The byte type is the set of integral values from -128 to +127. byte类型是从-128到+127的整数值集。

  • All integral types use the same representation (2's complement). 所有整数类型使用相同的表示(2的补码)。 The difference between the different types is their ranges. 不同类型之间的差异是它们的范围。

  • When you "see" a number, what you are seeing is a rendering of the number into a sequence of characters. 当您“看到”一个数字时,您看到的是将数字呈现为一系列字符。 There are MANY possible renderings; 有很多可能的渲染; eg the number represented in memory as 00101010 (42) can be rendered as "42" or "+42" or "0x2a" or ... "forty two" . 例如,存储器中表示为00101010 (42)的数字可以表示为"42""+42""0x2a"或...... "forty two"

  • The default format for rendering a byte , short , int and long is the same; 渲染byteshortintlong的默认格式是相同的; ie an optional minus sign followed by 1 or more decimal digits (with no zero padding). 即一个可选的减号,后跟一个或多个十进制数字(没有零填充)。 If you want to see your numbers formatted differently, then you need to do the formatting explicitly; 如果您希望以不同方式查看数字格式,则需要明确地进行格式化; eg using String.format(...) . 例如,使用String.format(...)

So to pull this together, if you want the bytes to look like 0x00 and 0x01 when you output or display them, you need to format them appropriately as you output / display them. 因此,要将它们组合在一起,如果您希望在输出或显示它们时字节看起来像0x000x01 ,则需要在输出/显示它们时对它们进行适当格式化。 In your example code, I doubt that there is anything wrong with the numbers themselves, or with the loop you are using to populate the array. 在您的示例代码中,我怀疑数字本身有什么问题,或者用于填充数组的循环。

You are confusing the string representation of the value with the value itself. 您将值的字符串表示与值本身混淆。 A value can be represented as binary, decimal, or hex, but it is still the same value. 值可以表示为二进制,十进制或十六进制,但它仍然是相同的值。

If you want to use your integer to initialise a byte array, you just need to cast your integer value to a byte value as follows: 如果要使用整数初始化字节数组,只需将整数值转换为字节值,如下所示:

arr[i] = (byte) i;

I would just like to note that 0 is NOT the same thing as 0x00. 我只想指出0与0x00不同。 If i were to use: 如果我使用:

ColorChooserOutputText.append(Integer.toHexString(list[i].getRed()));                                           
ColorChooserOutputText.append(Integer.toHexString(list[i].getGreen()));                                      
ColorChooserOutputText.append(Integer.toHexString(list[i].getBlue()));

and wanted to return the color, Purple it would return: ff0cc Which would be fine if I were just using Java. 并希望返回颜色,紫色它会返回:ff0cc如果我只是使用Java,那会没问题。 But if you are going between Java and something that had format specific needs ff0cc would not produce purple.. ff00cc is actually purple. 但是如果你在Java和具有格式特定需求的东西之间进行ff0cc就不会产生紫色...... ff00cc实际上是紫色的。

    //Declare some variables.
    Color HexColor = JButton1.getBackground();

    String MyRValue = null;
    String MyGValue = null;
    String MyBValue = null;

    //Get Hex Value
    MyRValue = Integer.toHexString(HexColor.getRed());
    MyGValue = Integer.toHexString(HexColor.getGreen());
    MyBValue = Integer.toHexString(HexColor.getBlue());

    //Make sure to keep both 0's                    
    MyRValue = ("00"+MyRValue).substring(MyRValue.length());
    MyGValue = ("00"+MyGValue).substring(MyGValue.length());
    MyBValue = ("00"+MyBValue).substring(MyBValue.length());

    //Format your HexColor to #00ff00, #000000, #ff00ee
    JTextArea1.append("#");
    JTextArea1.append(MyRValue+MyGValue+MyBValue);
    JTextArea1.append(", ");

You want 你要

new byte[]{(byte)i}

How you print this array is another matter. 如何打印此阵列是另一回事。 Look up printf in the API reference. 在API参考中查找printf。

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

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