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

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/7781461/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-30 21:26:12  来源:igfitidea点击:

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

java

提问by CQM

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 instance new byte[]{0x00};But the 0x00needs to be 0x01on the next iteration, how can I convert my value of integer iinto a value of byte in the 0x00 format?

在我的 for 循环中,我正在播种一个字节数组,其中的字节内容值递增 +1。例如new byte[]{0x00};但是0x00需要0x01在下一次迭代中,如何将整数值i转换为 0x00 格式的字节值?

I tried things like Byte.valueOf(Integer.toHexString(i))but this just gives me a value that looks like 0instead of 0x00.

我尝试过类似的事情,Byte.valueOf(Integer.toHexString(i))但这只是给了我一个看起来像0而不是0x00.

回答by ruakh

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};

回答by Stephen C

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 bytetype is the set of integral values from -128 to +127.

  • All integral types use the same representation (2's complement). 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; e.g. the number represented in memory as 00101010(42) can be rendered as "42"or "+42"or "0x2a"or ... "forty two".

  • The default format for rendering a byte, short, intand longis the same; i.e. 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; e.g. using String.format(...).

  • byte类型是一组的整数值的从-128到+127。

  • 所有整数类型都使用相同的表示(2 的补码)。不同类型之间的区别在于它们的范围。

  • 当您“看到”一个数字时,您所看到的是将数字呈现为一系列字符。有许多可能的渲染;例如,在内存中表示为00101010(42) 的数字可以呈现为"42"or"+42""0x2a"or ... "forty two"

  • 为渲染的默认格式byteshortint并且long是相同的; 即一个可选的减号,后跟 1 个或多个十进制数字(没有零填充)。如果你想看到你的数字格式不同,那么你需要明确地进行格式化;例如使用String.format(...).

So to pull this together, if you want the bytes to look like 0x00and 0x01when you output or display them, you need to format them appropriately as you output / display them. 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.

因此,要将这些组合在一起,如果您希望字节看起来像0x00并且0x01在输出或显示它们时,您需要在输出/显示它们时适当地格式化它们。在您的示例代码中,我怀疑数字本身或用于填充数组的循环有什么问题。

回答by Grodriguez

You are confusing the string representationof 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;

回答by Ingo

You want

你要

new byte[]{(byte)i}

How you print this array is another matter. Look up printf in the API reference.

如何打印这个数组是另一回事。在 API 参考中查找 printf。

回答by Peter Black

I would just like to note that 0 is NOT the same thing as 0x00. If i were to use:

我只想指出 0 与 0x00 不同。如果我要使用:

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. But if you are going between Java and something that had format specific needs ff0cc would not produce purple.. ff00cc is actually purple.

并且想要返回颜色,Purple 它会返回:ff0cc 如果我只是使用 Java,这会很好。但是,如果您在 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(", ");