我正在尝试将一个数字从整数转换为另一个整数,如果以十六进制打印,则看起来与原始整数相同.
例如:
将20转换为32(即0x20)
将54转换为84(即0x54)
我写了一个java程序,应该将小数从1转换为256到十六进制,但问题出现在我尝试小数超过256之后,我开始得到不正确的结果.这是我的代码:
public class Conversion {
public static void main(String[] args) {
System.out.printf("%s%14s", "Decimal", "Hexadecimal");
for(int i = 1; i <= 300; i++) {
System.out.printf("%7d ", i);
decimalToHex(i);
System.out.println();
}
}
private static void decimalToHex(int decimal) {
int count;
if(decimal >= 256) {
count = 2;
} else {
count = 1;
}
for (int i = 1; i <= count; i++) {
if(decimal >= 256) {
returnHex(decimal / 256);
decimal %= 256;
}
if(decimal >= 16) {
returnHex(decimal / 16); …Run Code Online (Sandbox Code Playgroud) 我在下面有Java代码演示,它给了我一些问题.
这是一个例子:
public class MyTest
{
public static void main(String as[])
{
String ColorHex="#4EB3A2";
int RedColor = Integer.parseInt(ColorHex.substring(1,3), 16);
int GreenColor = Integer.parseInt(ColorHex.substring(3,5), 16);
int BlueColor = Integer.parseInt(ColorHex.substring(5,7), 16);
int finalColorValue = 65536 * RedColor + 256*GreenColor + BlueColor;
int ColorDecimal=finalColorValue;
int red = ColorDecimal % 256;
ColorDecimal = ( ColorDecimal - red ) / 256;
int green = ColorDecimal % 256;
ColorDecimal = ( ColorDecimal - green ) / 256;
int blue = ColorDecimal % 256;
ColorDecimal = ( …Run Code Online (Sandbox Code Playgroud)