Solved

Hexa to float, how to?

Posted on 1997-05-02
2
306 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
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

Netscaler Common Configuration How To guides

If you use NetScaler you will want to see these guides. The NetScaler How To Guides show administrators how to get NetScaler up and configured by providing instructions for common scenarios and some not so common ones.

Question has a verified solution.

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

An Outlet in Cocoa is a persistent reference to a GUI control; it connects a property (a variable) to a control.  For example, it is common to create an Outlet for the text field GUI control and change the text that appears in this field via that Ou…
Windows programmers of the C/C++ variety, how many of you realise that since Window 9x Microsoft has been lying to you about what constitutes Unicode (http://en.wikipedia.org/wiki/Unicode)? They will have you believe that Unicode requires you to use…
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.
The goal of this video is to provide viewers with basic examples to understand opening and reading files in the C programming language.

778 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