[SOLVED] How to convert negative integer to byte array?
ProgrammingThis forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
I need to convert an integer to a byte array of size 2 and vice versa. The code shown below works well for positive values but not for negative values. Also, using an array of size four makes the conversion works. However, I am limited to an array of size 2.
Code:
#include <stdio.h>
main() {
int input = -1;
unsigned char buff[2];
buff[0] = input & 0xFF;
buff[1] = (input & 0xFF00) >> 8;
// buff[2] = (input & 0xFF0000) >> 16;
// buff[3] = (input & 0xFF000000) >> 24;
int value = 0;
value = buff[0];
value += buff[1] << 8;
// value += buff[2] << 16;
// value += buff[3] << 24;
printf("value[%d]\n", value);
}
Code:
// output with array of size 2
value[65535]
// output with array of size 4
value[-1]
Uhm, point out to your teacher that you're not using 16-bit machines?
On a modern desktop PC, an byte is 8 bits and an int is 32 bits. That means you cannot convert an int to two bytes without throwing away half the data in it. For a small positive number, you're throwing away leading zeros, so you get the same value. Negative numbers, being represented in twos complement, use all 32 bits. That means you cannot convert them to two-byte values without changing the number.
On a 16-bit machine (read: a 286 or 386SX), an int is 16-bits, and therefore can be represented with two bytes.
Thanks dugan for your insightful reply.
EDIT: thanks for recommending the book
As a work around for the problem, using "short int" would do the job. However, a "short int" may be larger than 2 bytes.
You could sign-extend the number when converting to int:
Code:
int value = 0;
value = buff[0];
value += buff[1] << 8;
if (buff[1] & 0x80) {
// for bonus points use a value that doesn't assume int size.
value |= 0xFFFF0000;
}
Values that really don't fit into 2 bytes will still be changed, but it works for negative numbers greater than -2^16 + 1.
You could also convert the negative number to positive before converting it to a byte array, then convert the contents of the byte array to its twos complement representation. That would work for as many negative numbers as positive ones.
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.