1

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

input : -54.9

Expected Result : 0x8225

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

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

}
BalusC
  • 1,082,665
  • 372
  • 3,610
  • 3,555

2 Answers2

0

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

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:

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.

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;
}
laune
  • 31,114
  • 3
  • 29
  • 42
-1

try Idea from this code " please notice 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);
        }
    }
JBALI
  • 89
  • 6
  • but where I should pass the value that i need to convert? –  Jan 15 '15 at 11:15
  • and input is float or double – laune Jan 15 '15 at 11:15
  • by using scanner the console will ask u to enter a value then u can specify what type it will take if double then double num =input.nextDouble(); [see link](http://www.java-made-easy.com/java-scanner.html). – JBALI Jan 15 '15 at 11:22
  • @user3624028 And then the Integer.toHexString( num ) will do... what? – laune Jan 15 '15 at 11:33
  • Integer.toHexString( num ) will method returns a hexadecimal string representation of int, you can also assign it to double type, for example : `Double d = new Double("4.0"); String str = d.toHexString(1.0); System.out.println("Hex String = " + str);` – JBALI Jan 15 '15 at 11:40