Data Conversion Byte to Integer
-
Hello, I am working on some financial data .I am picking up data from the dump file in a byte type variable . I have encountered a statement that is confusing me a lot a simple example or statement might be helpful : "empsalary are two-byte integers, high order first, and negatives are signed magnitude. Users may have to swap the bytes and/or convert negatives to the complement they use. This can be done by putting the low order byte first, then turning off bit 15 (the high order bit), then multiplying by -1. For positive numbers, only the bytes are switched." what does the above statement really means??
-
Hello, I am working on some financial data .I am picking up data from the dump file in a byte type variable . I have encountered a statement that is confusing me a lot a simple example or statement might be helpful : "empsalary are two-byte integers, high order first, and negatives are signed magnitude. Users may have to swap the bytes and/or convert negatives to the complement they use. This can be done by putting the low order byte first, then turning off bit 15 (the high order bit), then multiplying by -1. For positive numbers, only the bytes are switched." what does the above statement really means??
You are about to learn about endian issues. You can read all about it on the wikipedia[^] This is an long standing issue -- and is based on the processor on your computer. For all of us Windows, we operate as "little endian" -- whereas mac and java folks operate in "big endian" format. Endianness is just how the processor stores numbers in byte format. There's some handy functions used for socket programming (but also available generally) -- see ntohs()[^]
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ Peter Weyzen Staff Engineer [SoonR Inc -- PC Power delivered to your phone](http://www.soonr.com)
-
Hello, I am working on some financial data .I am picking up data from the dump file in a byte type variable . I have encountered a statement that is confusing me a lot a simple example or statement might be helpful : "empsalary are two-byte integers, high order first, and negatives are signed magnitude. Users may have to swap the bytes and/or convert negatives to the complement they use. This can be done by putting the low order byte first, then turning off bit 15 (the high order bit), then multiplying by -1. For positive numbers, only the bytes are switched." what does the above statement really means??
IMHO the above statement is clear: values are stored inside the file as two bytes integers, with most significative byte first, for instance
1000
(that is03E8h
in hexadecimal notation) is stored as follows:first byte = 3 (03h); second byte = 232 (E8h)
that is:
3 * 256 + 232 = 768 + 232 = 1000.
On the other hand, negative numbers are stored using Sign and magnitude representation (see [^]), hence
-1000
will become83E8h
in hexadecimal notation (8h=1000binary
, i.e. sign bitON
). Whenever you need to retrieve values you have to always check the sign bit. I give a C code snippet (error checking is left as an exercise to the reader...:)):unsigned char a,b;
int value;/* read first byte */
fread(&a, sizeof(a), 1, fd);
/* read second byte */
fread(&b, sizeof(b), 1, fd);
/* get magnitude, i.e. absolute value */
value = ((a & 0x7F) << 8 ) | b;
/* check sign */
if ( a & 0x80)
{
value = -value;
}
/* now value contains the (possibly negative?!? :-D) employee's salary!*/Hope that helps :)
If the Lord God Almighty had consulted me before embarking upon the Creation, I would have recommended something simpler. -- Alfonso the Wise, 13th Century King of Castile.
-
IMHO the above statement is clear: values are stored inside the file as two bytes integers, with most significative byte first, for instance
1000
(that is03E8h
in hexadecimal notation) is stored as follows:first byte = 3 (03h); second byte = 232 (E8h)
that is:
3 * 256 + 232 = 768 + 232 = 1000.
On the other hand, negative numbers are stored using Sign and magnitude representation (see [^]), hence
-1000
will become83E8h
in hexadecimal notation (8h=1000binary
, i.e. sign bitON
). Whenever you need to retrieve values you have to always check the sign bit. I give a C code snippet (error checking is left as an exercise to the reader...:)):unsigned char a,b;
int value;/* read first byte */
fread(&a, sizeof(a), 1, fd);
/* read second byte */
fread(&b, sizeof(b), 1, fd);
/* get magnitude, i.e. absolute value */
value = ((a & 0x7F) << 8 ) | b;
/* check sign */
if ( a & 0x80)
{
value = -value;
}
/* now value contains the (possibly negative?!? :-D) employee's salary!*/Hope that helps :)
If the Lord God Almighty had consulted me before embarking upon the Creation, I would have recommended something simpler. -- Alfonso the Wise, 13th Century King of Castile.
Thanks a Lot CPallini , by the way your mentioned code can go in error state ???
-
Thanks a Lot CPallini , by the way your mentioned code can go in error state ???
-
You are about to learn about endian issues. You can read all about it on the wikipedia[^] This is an long standing issue -- and is based on the processor on your computer. For all of us Windows, we operate as "little endian" -- whereas mac and java folks operate in "big endian" format. Endianness is just how the processor stores numbers in byte format. There's some handy functions used for socket programming (but also available generally) -- see ntohs()[^]
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ Peter Weyzen Staff Engineer [SoonR Inc -- PC Power delivered to your phone](http://www.soonr.com)
Peter Weyzen wrote:
For all of us Windows, we operate as "little endian" -- whereas mac and java folks operate in "big endian" format. Endianness is just how the processor stores numbers...
So if the processor ultimately determines how the numbers are stored, what difference does the OS or the programming language make?
"Normal is getting dressed in clothes that you buy for work and driving through traffic in a car that you are still paying for, in order to get to the job you need to pay for the clothes and the car and the house you leave vacant all day so you can afford to live in it." - Ellen Goodman
"To have a respect for ourselves guides our morals; to have deference for others governs our manners." - Laurence Sterne
-
Peter Weyzen wrote:
For all of us Windows, we operate as "little endian" -- whereas mac and java folks operate in "big endian" format. Endianness is just how the processor stores numbers...
So if the processor ultimately determines how the numbers are stored, what difference does the OS or the programming language make?
"Normal is getting dressed in clothes that you buy for work and driving through traffic in a car that you are still paying for, in order to get to the job you need to pay for the clothes and the car and the house you leave vacant all day so you can afford to live in it." - Ellen Goodman
"To have a respect for ourselves guides our morals; to have deference for others governs our manners." - Laurence Sterne
it was just a generalization -- about how windows runs on intel, and intel is little endian
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ Peter Weyzen Staff Engineer [SoonR Inc -- PC Power delivered to your phone](http://www.soonr.com)