سؤال

I am trying to send the following hex to NSOutputstream 0x0000000e000000010000001000003014

I'm able to send hex that is 8 bit with the following code:

long myhex = 0x0000000e00000001;    

NSData *data = [[NSData alloc] initWithBytes:&myhex length: sizeof(myhex)];
[outputStream write:[data bytes] maxLength:[data length]];

The problem is when I try this:

long myhex = 0x0000000e000000010000001000003014;

it says "integer constant is too long for its type"

I cant seem to figure out what type of integer will except this hex value.

هل كانت مفيدة؟

المحلول

Instead of trying to find an integer type long enough, you should probably just create an array of bytes and send that. Not only will you eventually be unable to find a type long enough for the data you wish to send, but there are also differences in the order of bytes in integers on different platforms.

So, to send arbitrarily long data in any order, use an array of bytes (unsigned char, or, preferably, uint8_t from stdint.h), e.g.:

uint8_t dataBytes[] = { 0x00, … , 0x0e, … 0x30, 0x14 };
NSData *data = [[NSData alloc] initWithBytes:dataBytes length:sizeof(dataBytes)];
مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top