LinuxQuestions.org
Review your favorite Linux distribution.
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices

Reply
 
Search this Thread
Old 01-15-2007, 09:09 PM   #1
carcassonne
Member
 
Registered: Jul 2005
Distribution: Fedora6 x86_64
Posts: 118

Rep: Reputation: 15
C: a float from 2 bytes of a 4-byte array


Hi,

I'd like to obtain a float that I multiply by 0.5 eg. like this:

int value;
float result;
value = 0x3039;
result = value * 0.5;

... except that the value comes from the first two bytes of a 4-byte array instead:

float result;
char array[]= {0x39, 0x30, 0x40, 0x49};

How can I take, in C, the first two bytes and either put them in an integer to do the above math or directly them to perform the calculation ? I tried the following, typecasting the beginning of the array so that the compiler might think it's an integer, but it does not work:

result = (int) array * 0.5;

Any suggestion greatly appreciated, thanks !
Al
 
Old 01-15-2007, 09:34 PM   #2
theNbomr
LQ 5k Club
 
Registered: Aug 2005
Distribution: OpenSuse, Fedora, Redhat, Debian
Posts: 5,395
Blog Entries: 2

Rep: Reputation: 903Reputation: 903Reputation: 903Reputation: 903Reputation: 903Reputation: 903Reputation: 903Reputation: 903
You must first convert the array to an integer:

Code:
value = array[0] + 256 * array[1];
If you contrive your array order correctly, you may be able to do it with a union, but this would be dependent upon byte order for the particular architecture.

Code:
union arrayToInt {
char array[4];
int  value;
} convertor;

convertor.array[0] = 39;
convertor.array[1] = 30;
result = 0.5 * convertor.value;
--- rod.
 
Old 01-15-2007, 11:04 PM   #3
arabindav
LQ Newbie
 
Registered: Oct 2006
Posts: 9

Rep: Reputation: 0
Hello carcassonne,

You can do as follows:

char array[] = {0x39, 0x30, 0x40, 0x49};
unsigned int* value;
/* or int* value; */

value = &array;
printf("%x\n", *value);

The above will give you the something like "49403039". However, as suggested by theNbomr, the value of "value" depends upon byte order. You can AND (&) and shift (right/left) to meet your requirement.

Have fun.
 
Old 01-16-2007, 03:16 AM   #4
grumpf
Member
 
Registered: Dec 2005
Distribution: just replace windows with linux
Posts: 38

Rep: Reputation: 15
hi carcassonne,
please note that you will have different results on big/little endian boxes.
If our are interessted in obtaining the mantisse/exponent part of the fp-number you may
prefer: frexp()

see: man frexp() for details
 
Old 01-16-2007, 06:22 AM   #5
Vasek
LQ Newbie
 
Registered: Nov 2006
Location: Prague, Czech Republic
Distribution: Fedora
Posts: 4

Rep: Reputation: 0
But for converting only the first two bytes, unsigned short would be more reliable on x86:

char array[] = {0x39, 0x30, 0x40, 0x49};
int value;
value = *(unsigned short*)array;

This results in 0x3039.
 
Old 01-16-2007, 01:06 PM   #6
KenJennings
LQ Newbie
 
Registered: Mar 2005
Location: FL, USA
Distribution: SuSE
Posts: 28

Rep: Reputation: 15
You should not try to exploit the order of values as they are stored in the array, since this will be completely non-portable. Simply get the components of your desired integer value and merge them together:

int highbyte = 0x30;
int lowbyte = 0x39;

For int value, either method below works and is reliably portable (assuming only that bytes are 8-bits wide and an int is large enough to store two bytes)

value = ( highbyte * 256 ) + lowbyte ;

OR

value = ( highbyte << 8 ) | lowbyte ;

Then you can work with the integer result of "value" however you like.
 
Old 01-16-2007, 06:31 PM   #7
gzickert
LQ Newbie
 
Registered: Feb 2004
Location: Orcutt, CA
Distribution: Fedora Core 10
Posts: 9

Rep: Reputation: 0
char array[] = {0x39, 0x30, 0x40, 0x49}; // These are all numeric characters '0'..'9'
int value = 0;

for (int i = 0; i < 4; ++i)
{
value = value * 10 + (array[i] - '0'); // process each digit from most significant to least.
}
 
Old 01-16-2007, 09:29 PM   #8
KenJennings
LQ Newbie
 
Registered: Mar 2005
Location: FL, USA
Distribution: SuSE
Posts: 28

Rep: Reputation: 15
Quote:
Originally Posted by gzickert
Code:
char array[] = {0x39, 0x30, 0x40, 0x49}; // These are all numeric characters '0'..'9'
...
You lost me. What numeric character(s) are 0x40 or 0x49 ?

Here the command:
Code:
echo "0123456789" | hex
reports:
Code:
0000  30 31 32 33 34 35 36 37 38 39 0a                 01234567 89.
 
Old 01-17-2007, 06:23 PM   #9
gzickert
LQ Newbie
 
Registered: Feb 2004
Location: Orcutt, CA
Distribution: Fedora Core 10
Posts: 9

Rep: Reputation: 0
char array to int

Sorry for the confusion. The example was for an array of 4 numeric chars and I copied the example array w/o ensuring that all of the chars were numeric. That said, if you are only interested in the first 2 chars then change the loop control test to i < 2 so only the first 2 characters are processed.
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Best Python float array library? zero79 Programming 0 08-31-2005 05:25 PM
count digits of a float || convert float to string nadroj Programming 6 07-11-2005 04:52 PM
how to convert long integer value to byte array appas Programming 11 11-23-2004 01:56 PM
Java byte array problem Mohsen Programming 2 03-11-2004 01:37 PM
how to transform an object into an array of bytes on Java! poeta_boy Programming 3 02-15-2004 07:28 PM


All times are GMT -5. The time now is 01:54 PM.

Main Menu
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration