LinuxQuestions.org
Share your knowledge at the LQ Wiki.
Home Forums Tutorials Articles Register
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 10-26-2010, 05:14 PM   #1
Basel
Member
 
Registered: Feb 2004
Location: United States
Distribution: Ubuntu 10.10
Posts: 319

Rep: Reputation: 30
How to convert negative integer to byte array?


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]
 
Old 10-26-2010, 05:17 PM   #2
dugan
LQ Guru
 
Registered: Nov 2003
Location: Canada
Distribution: distro hopper
Posts: 11,219

Rep: Reputation: 5309Reputation: 5309Reputation: 5309Reputation: 5309Reputation: 5309Reputation: 5309Reputation: 5309Reputation: 5309Reputation: 5309Reputation: 5309Reputation: 5309
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.

BTW, I'm currently reading Code, by Charles Petzold (http://www.amazon.ca/Code-Language-C...dp/0735611319/), which is a very good overview of issues like this.

Last edited by dugan; 10-26-2010 at 05:29 PM.
 
Old 10-26-2010, 05:36 PM   #3
Basel
Member
 
Registered: Feb 2004
Location: United States
Distribution: Ubuntu 10.10
Posts: 319

Original Poster
Rep: Reputation: 30
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.

Last edited by Basel; 10-26-2010 at 09:25 PM.
 
Old 10-26-2010, 10:22 PM   #4
ntubski
Senior Member
 
Registered: Nov 2005
Distribution: Debian, Arch
Posts: 3,780

Rep: Reputation: 2081Reputation: 2081Reputation: 2081Reputation: 2081Reputation: 2081Reputation: 2081Reputation: 2081Reputation: 2081Reputation: 2081Reputation: 2081Reputation: 2081
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.
 
Old 10-27-2010, 11:09 AM   #5
dugan
LQ Guru
 
Registered: Nov 2003
Location: Canada
Distribution: distro hopper
Posts: 11,219

Rep: Reputation: 5309Reputation: 5309Reputation: 5309Reputation: 5309Reputation: 5309Reputation: 5309Reputation: 5309Reputation: 5309Reputation: 5309Reputation: 5309Reputation: 5309
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.
 
Old 10-27-2010, 12:35 PM   #6
theNbomr
LQ 5k Club
 
Registered: Aug 2005
Distribution: OpenSuse, Fedora, Redhat, Debian
Posts: 5,399
Blog Entries: 2

Rep: Reputation: 908Reputation: 908Reputation: 908Reputation: 908Reputation: 908Reputation: 908Reputation: 908Reputation: 908
Does this help?
Code:
#include <stdio.h>
#include <stdlib.h>

union intToChar {
    int   integer;
    char  bytes[4];
};

int	main( int argc, char * argv[] ){

union intToChar i2c;

    i2c.integer = 0x12345678;
    printf( "%02X %02X %02X %02X\n", (unsigned int)i2c.bytes[0],
    				     (unsigned int)i2c.bytes[1],
				     (unsigned int)i2c.bytes[2],
				     (unsigned int)i2c.bytes[3] );
    exit(0);
}
--- rod.
 
1 members found this post helpful.
  


Reply



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
[SOLVED] Byte array conversion yaplej Programming 9 09-23-2009 01:44 PM
Convert 64 bit integer to char array in C++ syseeker Programming 2 06-27-2006 03:33 AM
Determine if a signed integer is negative in C lowpro2k3 Programming 20 08-16-2005 09:20 AM
integer encoding & byte ordering slzckboy Programming 25 07-09-2005 04:40 PM
how to convert long integer value to byte array appas Programming 11 11-23-2004 01:56 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 08:18 PM.

Main Menu
Advertisement
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
Open Source Consulting | Domain Registration