如何在java中将十进制值转换为十六进制?

时间:2021-09-17 16:33:36

How to convert Decimal Value (Temperature) to 16 bit hexadecimal in java?

如何在java中将十进制值(温度)转换为16位十六进制?

input : -54.9

输入:-54.9

Expected Result : 0x8225

预期结果:0x8225

I have reverse code for it where i am converting 16 byte hexadecimal to Decimal Value (Temperature).

我有反向代码,我将16字节十六进制转换为十进制值(温度)。

private static double hexDataToTemperature(String tempHexData) {

    String tempMSBstr = tempHexData.substring(0, 2);
    String tempLSBstr = tempHexData.substring(2, 4);

    int tempMSB = Integer.parseInt(tempMSBstr, 16);
    int tempLSB = Integer.parseInt(tempLSBstr, 16);
    int sign = 1;

    if (tempMSB >= 128) {
        tempMSB = tempMSB - 128;
        sign = -1;
    }

    Float f = (float) (sign * ((float) ((tempMSB * 256) + tempLSB) / 10));

    return Double.parseDouble("" + f);

}

2 个解决方案

#1


0  

To represent temperatures in tenths of a degree as a signed short (16 bit) value represented in hexadecimal:

以十六进制表示温度作为以十六进制表示的带符号的短(16位)值:

static String toHex( float t ){
    short it = (short)Math.round(t*10);
    return String.format( "%04x", it );
}

You can add "0x" to the format string if you want that. - The reverse conversion:

如果需要,可以在格式字符串中添加“0x”。 - 反向转换:

static float toDec( String s ){
    int it = Integer.parseInt( s, 16 );
    if( it > 32767 ) it -= 65536;
    return it/10.0F;
}

This represents the integer in two's complement, so the result for -54.9 will not be 0x8225 but 0xfddb. Using the most significant bit as a sign bit and representing the absolute value in the remaining 15 bit ("signed magnitude") is highly unusual especially with Java.

这表示二进制补码中的整数,因此-54.9的结果不是0x8225而是0xfddb。使用最高有效位作为符号位并表示剩余15位(“有符号幅度”)中的绝对值是非常不寻常的,特别是对于Java。

If you do want to use signed magnitude:

如果您确实想使用带符号的幅度:

static String toHex( float t ){
    int sign = 0;
    if( t < 0 ){
        sign = 0x8000;
        t = -t;
    }
    short it = (short)(Math.round(t*10) + sign);
    return String.format( "%04x", it );
}

static float toDec( String s ){
    int it = Integer.parseInt( s, 16 );
    if( it > 32767 ){
        it = -(it - 0x8000);
    }
    return it/10.0F;
}

#2


-1  

try Idea from this code " please notice toHexString()"

尝试从这段代码的想法“请注意toHexString()”

import java.util.Scanner;
    class DecimalToHex
    {
        public static void main(String args[])
        {
          Scanner input = new Scanner( System.in );
          System.out.print(" decimal number : ");
          int num =input.nextInt();

          // calling method toHexString()
          String str = Integer.toHexString(num);
          System.out.println("Decimal to hexadecimal: "+str);
        }
    }

#1


0  

To represent temperatures in tenths of a degree as a signed short (16 bit) value represented in hexadecimal:

以十六进制表示温度作为以十六进制表示的带符号的短(16位)值:

static String toHex( float t ){
    short it = (short)Math.round(t*10);
    return String.format( "%04x", it );
}

You can add "0x" to the format string if you want that. - The reverse conversion:

如果需要,可以在格式字符串中添加“0x”。 - 反向转换:

static float toDec( String s ){
    int it = Integer.parseInt( s, 16 );
    if( it > 32767 ) it -= 65536;
    return it/10.0F;
}

This represents the integer in two's complement, so the result for -54.9 will not be 0x8225 but 0xfddb. Using the most significant bit as a sign bit and representing the absolute value in the remaining 15 bit ("signed magnitude") is highly unusual especially with Java.

这表示二进制补码中的整数,因此-54.9的结果不是0x8225而是0xfddb。使用最高有效位作为符号位并表示剩余15位(“有符号幅度”)中的绝对值是非常不寻常的,特别是对于Java。

If you do want to use signed magnitude:

如果您确实想使用带符号的幅度:

static String toHex( float t ){
    int sign = 0;
    if( t < 0 ){
        sign = 0x8000;
        t = -t;
    }
    short it = (short)(Math.round(t*10) + sign);
    return String.format( "%04x", it );
}

static float toDec( String s ){
    int it = Integer.parseInt( s, 16 );
    if( it > 32767 ){
        it = -(it - 0x8000);
    }
    return it/10.0F;
}

#2


-1  

try Idea from this code " please notice toHexString()"

尝试从这段代码的想法“请注意toHexString()”

import java.util.Scanner;
    class DecimalToHex
    {
        public static void main(String args[])
        {
          Scanner input = new Scanner( System.in );
          System.out.print(" decimal number : ");
          int num =input.nextInt();

          // calling method toHexString()
          String str = Integer.toHexString(num);
          System.out.println("Decimal to hexadecimal: "+str);
        }
    }