Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Int array to Char array conversion? 1

Status
Not open for further replies.

ezeke1

Programmer
Mar 20, 2003
41
0
0
US
Hello, I'm trying to convert an integer array into an unsigned char array, but my experience with this is limited. I don't want to lose any information during the conversion. Can anyone provide some tips?

 
Well, generally speaking (though it is OS/compiler dependant), an int is 4 bytes, and a char is one byte - so depending on the actual size of your int (ie whether or not it is taking up the whole 4 bytes), you cannot avoid losing data.

--------------------------------------------------
Free Java/J2EE Database Connection Pooling Software
 
Your problem is not clear and can be interpreted in several ways. What form do you want to convert it?

Say you have an array with

123 -1 99 -5 0

Do you want an array with

0x7B 0xFF 0x63 0xFB 0x00

or do you want an array with

"123 -1 99 -5 0"

or do you want a 2D array with

"123", "-1", "99", "-5", "0"

What do you do with numbers over 128 or less than -127? Do you want it in text form or big endian/little endian? These are just some of the questions that I would ask from such a specification.
 
Thank you for the response sedj and xwb :)

xwb, you bring up a good point.

What I have is an integer array where each element is a number from 0-100. e.g. int array[100] = {1,2,66..etc};

In addition I have an unsigned char array and I would like this array to have "1 2 66..etc" since this type of data format will be easier to send over a network. In other words I guess this should be text form because I do not have to account for a little/big endian issue at this time.

Like sedj pointed out, a int is 4 bytes while a char is only 1 byte. To make matters a little more complicated both arrays are set sizes. So the integer array would be size 178 and the unsigned char array would have to be 178 too.
 
If the range is only 1-100, it will fit in a signed char. You can just transmit it as a series of chars.
 
Given the value range, wouldn't it just be like this?
Code:
void foo(unsigned char *dst, const int *src, size_t size)
{
   while ( size-- )
   {
      *dst++ = *src++;
   }
}
 
Thanks all for the help.

Since the int array only has positive numeric values and I wanted the char array to contain the text format of each integer, I was able to make a simple assignment like so,

......includes......
#define size 2

int main()
{
int i;
int a[size] = {1,2};
unsigned char b[size];

for(i=0; i<size; i++)
b = size;
return 0;
}
 
ezeke1 said:
I wanted the char array to contain the text format of each integer
If you want a number like 100 to be an array of 3 chars "100", that won't work.
Try sprintf() instead.
Code:
#define SIZE  50

int main()
{
   int i;
   int a[ SIZE ] = {...}; /* Max int size of 100 */
   char temp[ 4 ];
   char str[ (SIZE * 3) + 2 ];
   memset( str, '\0', sizeof( str ) );

   for ( i = 0; i < SIZE; ++i )
   {
      sprintf( temp, "%d", a[ i ] );
      strcat( str, temp );
      strcat( str, " " );
   }
}
 
Just send the integer buffer byte by byte after converting the data to the network host order (using htonl function).
Is there a hidden reason your are not proceeding in the normal way ?

"In other words I guess this should be text form because I do not have to account for a little/big endian issue at this time."

What is the endianess issue if using the htonl / ntohl functions ?
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top