Solved

Hexa to float, how to?

Posted on 1997-05-02
2
309 Views
Last Modified: 2011-10-03
I need to convert some hexadecimal code to float or double, how can I achive this?
ex:
I have in the code   2A ED 00 F2
what is the equivalent in a float number?
or double?
Is there any functiuon in C that performs this action?
0
Comment
Question by:Antonio050297
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
2 Comments
 
LVL 4

Accepted Solution

by:
emmons earned 10 total points
ID: 1250230
Assuming that the input characters are all upper case HEX digits, this should do the job of getting each byte into an int.
Then you can merge them together by just multiplies of 256.

float f;
f = byte2int( "2A");
f = 256 * f + byte2int( "ED");
f = 256 * f + byte2int( "00");
f = 256 * f + byte2int( "F2");

There is a tacit assumption here that the number that you are passing in is not stored in an odd way. If it is stored in memory as a float, than this will not work. Also, the above code might be exactly opposite of what you want, depending on the byte ordering of your architecture. Try it on a number that you know to verify the ordering.
int byte2int( char in[3]) /* 2 bytes and a null */
 {
int i;
i = 16 * (( in[0] > '9') ? in[0] - 'A' : in[0] - '0');
i += (( in[1] > '9') ? in[1] - 'A' : in[1] - '0');
return( i);
}
0
 

Author Comment

by:Antonio050297
ID: 1250231
Thanks very much for your support mr.emmons!
0

Featured Post

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Have you thought about creating an iPhone application (app), but didn't even know where to get started? Here's how: ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ Important pre-programming comments: I’ve never tri…
This is a short and sweet, but (hopefully) to the point article. There seems to be some fundamental misunderstanding about the function prototype for the "main" function in C and C++, more specifically what type this function should return. I see so…
The goal of this video is to provide viewers with basic examples to understand how to use strings and some functions related to them in the C programming language.
Video by: Grant
The goal of this video is to provide viewers with basic examples to understand and use while-loops in the C programming language.

697 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question