• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Convert hexadecimal value to double treating it hex see desc.

 
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi
Is there a way that I can convert the following to double. The scenario is if my string starts with 16# and ends with # I have to take the value in between 16# and # as hexadecimal and then convert it to double e.g. the string value is :
String s = "16#1234#";
Now if I convert this to long I am doing this
if (s.startsWith("16#") && s.endsWith("#"))
{
extractedValue = s.substring( 3, s.length()-1 );
longValue = Long.parseLong( extractedValue, 16 );
}
I am using the radix as 16 because of the condition defined above. Luckily there is method

parseLong(String s, int radix)
in java.lang.Long. But there is no such method that takes radix also in java.lang.Double
we only have a parseDouble(String).
So is there a way I convert the above string to double treating it as a hexadecimal just like done with parseLong. If I extract the values in between 16# and # I get 1234 and now say do this
double d = Double.parseDouble(1234);
I get different result because it treats 1234 as double though it's hexadecimal value. The same applies to octal and others. Is there a way or work arround this.
I really appreciate for any help.
Thanks
 
Wanderer
Posts: 18671
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Well, there's more than one way to represent floating-point numbers as bits - it will depend how the hex strings were generated in the first place. Internally, Java uses the IEEE 754 standard, described here in the JLS. If that's also what was used to generate the hex strings, you may find the Double.longBitsToDouble(long) method useful. Use parseLong() to convert String to long, and then longBitsToDouble() to convert it to a double.
If that doesn't work, you'll have to find out how exactly the hex strings are being generated. Find some known double values, and see how they're represented in hex. Maybe post examples here if it's not clear how the encoding works. Good luck...
 
hallian
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Here is small program that I am using now notice in the main method I am passing the value of the string to be
String s = "16#123.FFFFFFFFFFFF#";
and I get the final result value as
291.00000000000006
though converting the left side after decimal point gives me
5.3290705182007514E-14
and the right side gives
291
and adding these doesn't show all the digits on the left.
Now if I add another F in my String value at the top the final output only shows 291.0 and doesn't add the left side. Any idea what might be the problem or why it's not doing. I have the sample code as follows:

##################################################

public class Conv
{
public static double convertRightSide(String rhsValue, int radix) throws NumberFormatException
{
double mult = 1.0;
int parsedNumber = 0;
double finalResult = 0;

try
{
for(int i = 0; i <= rhsValue.length()-1; i++)
{
char ch = rhsValue.charAt(i);
//Character c= new Character(ch);
//String s = c.toString();
mult = mult/radix;
parsedNumber = Integer.parseInt(""+ch, radix);
finalResult = mult * parsedNumber;
}
}
catch(NumberFormatException nfe)
{
throw nfe;
}
catch(Exception exception)
{
throw new NumberFormatException();
}
return finalResult;
}
private static double getDoubleValue(String extractedString, int radix) throws NumberFormatException
{
String leftHandSideString = null;
String rightHandSideString = null;
double fullDoubleResult = 0.0;
double wholePortion = 0;

try
{
//if there is a decimal point in the number then do this
if(extractedString.indexOf('.') > 0)
{
leftHandSideString = extractedString.substring( 0, extractedString.indexOf('.') );
rightHandSideString = extractedString.substring( extractedString.indexOf('.')+1, extractedString.length() );

wholePortion = (double) Long.parseLong(leftHandSideString, radix);

if(extractedString.charAt(0) == '-')
{
System.out.println("In the Minus " + wholePortion);
fullDoubleResult = wholePortion - convertRightSide(rightHandSideString, radix);
}
else
fullDoubleResult = wholePortion + convertRightSide(rightHandSideString, radix);
}
else
{
fullDoubleResult = Long.parseLong(extractedString, radix);
}
}
catch(NumberFormatException nfe)
{
throw nfe;
}
catch(Exception exception)
{
throw new NumberFormatException();
}

return fullDoubleResult;
}
public static double parseDouble(String s) throws NumberFormatException
{
String extractedValue = null;
double doubleValue = 0.0;

try
{
doubleValue = getDoubleValue(s, 10);
}
catch (NumberFormatException nfe)
{
if (s.startsWith("16#") && s.endsWith("#"))
{
extractedValue = s.substring( 3, s.length()-1 );
doubleValue = getDoubleValue(extractedValue, 16);
System.out.println("Final " + doubleValue);
}
else if (s.startsWith("h"))
{
extractedValue = s.substring( 1 );
doubleValue = getDoubleValue(extractedValue, 16);
}
else if (s.startsWith("8#") && s.endsWith("#"))
{
extractedValue = s.substring( 2, s.length()-1 );
doubleValue = getDoubleValue(extractedValue, 8);
}
else if (s.startsWith("o"))
{
extractedValue = s.substring( 1 );
doubleValue = getDoubleValue(extractedValue, 8);
}
else if (s.startsWith("2#") && s.endsWith("#"))
{
extractedValue = s.substring( 2, s.length()-1 );
doubleValue = getDoubleValue(extractedValue, 2);
}
else if (s.startsWith("b"))
{
extractedValue = s.substring( 1 );
doubleValue = getDoubleValue(extractedValue, 2);
}
else
{
throw nfe;
}
}

return doubleValue;
}
public static void main (String[] args)
{
Conv con = new Conv ();
try
{
String s = "16#123.FFFFFFFFFFFF#";
double d = con.parseDouble(s);
}
catch(Exception except)
{
System.out.println(except);
}
}
}

##################################################
Thanks for any help.
 
Sheriff
Posts: 7023
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
prog,
Welcome to JavaRanch!
We ain't got many rules 'round these parts, but we do got one. Please change your display name to comply with The JavaRanch Naming Policy.
Thanks Pardner! Hope to see you 'round the Ranch!
 
Don't get me started about those stupid light bulbs.
reply
    Bookmark Topic Watch Topic
  • New Topic