How to store Hex string in an Integer variable in android???

懵懂的女人 提交于 2019-12-11 03:12:17

问题


In the following code intArray[i] stores RGB values of pixels in hex format(eg:0xffff0000) .... The method hsvToRgb() gives bak an integer value of RGB (eg:15777252) but i need back the rgb value in the original hex format after changes. The second line gives me that but its a string ....What do i do to store this string value back into the array? ... please help me.

       int disco = hsvToRgb(hsv);

       hexColor = String.format("0x%06X", (0xffffff & disco));
       intArray[i] = Integer.valueOf(String.valueOf(disco), 16);

回答1:


There's no such thing as a "hex format" integer versus a "decimal format" integer. The bit/byte representation of the value is the same. For example, the decimal value 15,777,252 is the hex value 0xF0BDE4. (You can use Google to convert: search "15777252 in hex").

You can use the disco value directly. If you want to print it out in a hex representation, use Integer.toHexString().

Regarding the format. Think of it like this ... The computer represents the value as a series of bits. By way of example, let's pick a random number and represent it using 8 bits: 01110101. Using a bit string to represent bigger numbers would get very long very quickly, so hexadecimal is often used. The hex equivalent is: 65. By convention, we usually precede the value by 0x when it's in hex. That gives us 0x65. Non-programmers tend to deal more naturally in base 10 however (rather than base 16). The same number in base 10 is 101.

You can see this with some code:

final int value = 0x65;                            // we can declare it in hex
final int sameValue = 101;                         // or in decimal

System.out.println(value);                         // output in base 10; prints "101"
System.out.println(Integer.toHexString(value));    // output in base 16; prints "65"
System.out.println(Integer.toBinaryString(value)); // output in base 2; prints "1100101"

System.out.println(""+(value == sameValue));       // prints "true"


来源:https://stackoverflow.com/questions/10490767/how-to-store-hex-string-in-an-integer-variable-in-android

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!