iosobjective-cnsstringnsstringencoding

How to convert string into NSData to pass it to BLE Device?


I have textField where the user enters any text doesn't know the length even, then I have to convert it to hexadecimal. which I have done it through this code.

NSString * str = @"Hello World";
NSString * hexStr = [NSString stringWithFormat:@"%@",
                     [NSData dataWithBytes:[str cStringUsingEncoding:NSUTF8StringEncoding] 
                                    length:strlen([str cStringUsingEncoding:NSUTF8StringEncoding])]];

for(NSString * toRemove in [NSArray arrayWithObjects:@"<", @">", @" ", nil]) 
    hexStr = [hexStr stringByReplacingOccurrencesOfString:toRemove withString:@""];

NSLog(@"%@", hexStr); 48656c6c6f20776f726c64

But hard parts start from that. How to separate every two hex into uint8_t. Because I don't know the length, but the user can write anything of any length of string which will be huge hexadecimal.if I know the length I can do like this

NSMutableData *concatenate = [NSMutableData data];
uint8_t first=0x48;
uint8_t second = 0x56;
uint8_t third = 0x6c;
uint8_t fourth = 0x6f;
uint8_t fifth=0x20;
uint8_t sixth=0x77;
uint8_t seventh=0x6f;
uint8_t eighth=0x72;
uint8_t ninth=0x6c;
uint8_t tenth=0x64;

NSData* one = [NSData dataWithBytes:(void*)&first length:sizeof(first)];
NSData* two = [NSData dataWithBytes:(void*)&second length:sizeof(second)];
NSData* three = [NSData dataWithBytes:(void*)&third length:sizeof(third)];
NSData* four = [NSData dataWithBytes:(void*)&fourth length:sizeof(fourth)];
NSData* five = [NSData dataWithBytes:(void*)&fifth length:sizeof(fifth)];
NSData* six = [NSData dataWithBytes:(void*)&sixth length:sizeof(sixth)];
NSData* seven = [NSData dataWithBytes:(void*)&seventh length:sizeof(seventh)];
NSData* eight = [NSData dataWithBytes:(void*)&eighth length:sizeof(eighth)];
NSData* nine = [NSData dataWithBytes:(void*)&nineth length:sizeof(nineth)];
NSData* ten = [NSData dataWithBytes:(void*)&tenth length:sizeof(tenth)];

[concatenate appendData:one];
[concatenate appendData:two];
[concatenate appendData:three];
[concatenate appendData:four];
[concatenate appendData:five];
[concatenate appendData:six];
[concatenate appendData:seven];
[concatenate appendData:eight];
[concatenate appendData:nine];
[concatenate appendData:ten];

But when I don't know the length then I don't know.Any help or clue will be highly appreciated.


Solution

  • First time you should convert NSString to NSData

    NSString* str = @"Hello World";
    NSData* data = [str dataUsingEncoding:NSUTF8StringEncoding];
    

    Next you send this data to device

    [discoveredPeripheral writeValue:data forCharacteristic:charForValue type:CBCharacteristicWriteWithResponse];
    

    Here you can send data with response from device or without. Of course your device should be ready to receive such data with/without response.

    For understanding discoveredPeripheral is variable that you receive from

    -(void)centralManager:(CBCentralManager *)central didDiscoverPeripheral:(CBPeripheral *)peripheral advertisementData:(NSDictionary *)advertisementData RSSI:(NSNumber *)RSSI
    

    charForValue is variable received in

    - (void)peripheral:(CBPeripheral *)peripheral didDiscoverCharacteristicsForService:(CBService *)service error:(NSError *)error
    

    for necessary service and characteristic.