1

I need to represent a NSInteger and NSString into array of bytes. below are the sample of what I am looking for.

For how, this harcodings are working fine. I want to do this through code. Any clue.

First, NSInteger into bytes of Hex:

NSInteger test = 1;
unsigned char byte[] = { 0x00, 0x01 };

NSInteger test = 16;
unsigned char byte[] = { 0x00, 0x10 };
NSData *data = [NSData dataWithBytes:byte length:sizeof(byte)];

Second, NSString into bytes of Hex:

NSString *test = @"31C5B562-BD07-4616-BCBD-130BA6822790";
unsigned char byte[] = {0x31, 0xC5, 0xB5, 0x62, 0xBD, 0x07, 0x46, 0x16, 0xBC, 0xBD, 0x13, 0x0B, 0xA6, 0x82, 0x27, 0x90};
NSData *data = [NSData dataWithBytes:byte length:sizeof(byte)];

I tried with below code and it works well for my UUID but for NSInteger to to be working I need to send "0010" instead of 16 and "0001" instead of 1. So any clue on how to do this conversion.

- (NSData *)hexData {
    NSMutableData *hexData = [NSMutableData data];
    int idx = 0;

    for (idx = 0; idx+2 <= self.length; idx+=2) {
        NSRange range = NSMakeRange(idx, 2);
        NSString* hexStr = [self substringWithRange:range];
        NSScanner* scanner = [NSScanner scannerWithString:hexStr];
        unsigned int intValue;
        [scanner scanHexInt:&intValue];
        [hexData appendBytes:&intValue length:1];
    }

    return hexData;
}

EDIT:

int8_t test = -59;
int8_t bytes = CFSwapInt16HostToBig(test);
NSData *data1 = [NSData dataWithBytes:&bytes length:sizeof(bytes)];

Reaching as 0xFF instead of 0xC4

Abhinav
  • 37,684
  • 43
  • 191
  • 309

1 Answers1

5

Since your string is a UUID string you can do something like this:

NSString *test = @"";
uuid_t uuid;
uuid_parse([test UTF8String], uuid)
NSData *data = [NSData dataWithBytes:uuid length:16];

For the number you can do:

NSInteger test = 1;
NSData *data = [NSData dataWithBytes:&test length:sizeof(test)];

Keep in mind that NSInteger is probably more than two bytes and you may also need to worry about byte order.

Update: Since it seems you need the integer value to be two bytes, you should do:

uint16_t test = 1;
NSData *data = [NSData dataWithBytes:&test length:sizeof(test)];

This will ensure 2 bytes. You also need to worry about byte ordering so you really need:

uint16_t test = 1;
uint16_t bytes = CFSwapInt16HostToBig(test);
NSData *data = [NSData dataWithBytes:&bytes length:sizeof(bytes)];

Change CFSwapInt16HostToBig to CFSwapInt16HostToLitte if appropriate.

rmaddy
  • 314,917
  • 42
  • 532
  • 579
  • Thanks! UUID is working fine. Upvoting for that. I already tried the integer approach mentioned by but it does not work for me. I have to create the byte steam as I mentioned and then only it works. Any idea of how to create that kind of byte stream. – Abhinav Dec 20 '13 at 18:54
  • Actually, I am dealing with a hardware that is accepting HEX representing strings. So, to send 16 I need to send an array of 0x00 and 0x10 bytes. With your solution it treats that byte stream some different value. Please see my updated question. – Abhinav Dec 20 '13 at 19:42
  • Awesome!!! This worked for positive integers. Can you please guide me how to do this for signed integers? – Abhinav Dec 20 '13 at 20:37
  • It will work for signed integers too. Just assign a negative value to `test`. – rmaddy Dec 20 '13 at 20:58
  • I tried and it did not work. Data type it unsigned integer uint16_t. – Abhinav Dec 20 '13 at 21:08
  • 1
    Define "did not work". Please stop being so vague with the issues you are having. What actually happens when you try to use a negative number? – rmaddy Dec 20 '13 at 21:10
  • First of all my apology for not mentioning the issue I faced with negative values. When I tried sending -59 it reached on destination as 0xFF Hex instead of 0xC4. See my edited question. I changed the uint16_t to int8_t. Also tried with int16_t. – Abhinav Dec 20 '13 at 22:19
  • You `int16_t` for two bytes. `int8_t` is only for one byte. – rmaddy Dec 20 '13 at 22:28