How do I convert a 24-bit integer into a 3-byte array?
Asked Answered
W

3

2

Hey Im totally out of my depth and my brain is starting to hurt.. :(

I need to covert an integer so that it will fit in a 3 byte array.(is that a 24bit int?) and then back again to send/receive this number from a byte stream through a socket

I have:

NSMutableData* data = [NSMutableData data];

 int msg = 125;

 const void *bytes[3];

 bytes[0] = msg;
 bytes[1] = msg >> 8;
 bytes[2] = msg >> 16;

 [data appendBytes:bytes length:3];

 NSLog(@"rtn: %d", [[[NSString alloc] initWithData:data encoding:NSASCIIStringEncoding] intValue]);

 //log brings back 0

I guess my main problem is that I do not know how to check that I have indeed converted my int correctly which is the converting back that I need to do as well for sending the data.

Any help greatly appreciated!

Whittier answered 7/12, 2010 at 15:17 Comment(0)
C
2

You could use a union:

union convert {
    int i;
    unsigned char c[3];
};

to convert from int to bytes:

union convert cvt;
cvt.i = ...
// now you can use cvt.c[0], cvt.c[1] & cvt.c[2]

to convert from bytes to int:

union convert cvt;
cvt.i = 0; // to clear the high byte
cvt.c[0] = ...
cvt.c[1] = ...
cvt.c[2] = ...
// now you can use cvt.i

Note: using unions in this manner relies on processor byte-order. The example I gave will work on a little-endian system (like x86).

Chartulary answered 7/12, 2010 at 15:45 Comment(0)
E
7

Assume you have a 32-bit integer. You want the bottom 24 bits put into a byte array:

int msg = 125;
byte* bytes = // allocated some way

// Shift each byte into the low-order position and mask it off
bytes[0] = msg & 0xff;
bytes[1] = (msg >> 8) & 0xff;
bytes[2] = (msg >> 16) & 0xff;

To convert the 3 bytes back to an integer:

// Shift each byte to its proper position and OR it into the integer.
int msg = ((int)bytes[2]) << 16;
msg |= ((int)bytes[1]) << 8;
msg |= bytes[0];

And, yes, I'm fully aware that there are more optimal ways of doing it. The goal in the above is clarity.

Eureka answered 7/12, 2010 at 15:35 Comment(3)
This is working fine as long as the number is < 255 I gather thats the max value for a 24 bit int?Whittier
@loststudent: No, the maximum value for a 24 bit unsigned int is (2^24)-1, or 16,777,216. The range of a signed 24 bit int is -8,388,608 to 8,388,607. Which part isn't working?Eureka
Its just when the int goes over 255Whittier
C
2

You could use a union:

union convert {
    int i;
    unsigned char c[3];
};

to convert from int to bytes:

union convert cvt;
cvt.i = ...
// now you can use cvt.c[0], cvt.c[1] & cvt.c[2]

to convert from bytes to int:

union convert cvt;
cvt.i = 0; // to clear the high byte
cvt.c[0] = ...
cvt.c[1] = ...
cvt.c[2] = ...
// now you can use cvt.i

Note: using unions in this manner relies on processor byte-order. The example I gave will work on a little-endian system (like x86).

Chartulary answered 7/12, 2010 at 15:45 Comment(0)
C
1

How about a bit of pointer trickery?

int foo = 1 + 2*256 + 3*65536;
const char *bytes = (const char*) &foo;
printf("%i %i %i\n", bytes[0], bytes[1], bytes[2]); // 1 2 3

There are probably things to be taken care of, if you are going to use this in production code, but the basic idea is sane.

Caren answered 7/12, 2010 at 15:28 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.