Converting INT to Hex string to be sent over BLE

2019-08-26 02:08发布

问题:

I am trying to convert several 'int' values to hex so that they can sent over bluetooth as a command.

I have tried numerous things, but while I get the same result with different methods, I cannot get the desired result to send over BLE so that the device recognizes the command.

- (void)sendValues:(int)value1 value2:(int)value2 value3:(int)value3 value4:(int)value4
{
    // value1 = 734,
    // value2 = 43
    // value3 = 50
    // value4 = 7

    // this string should be constructed from the values passed through
    // NSString * command = @"021202de343325353025203725"; 

    NSMutableData * _data = [[NSMutableData alloc] init];
    unsigned char whole_byte;
    char byte_chars[3] = {'\0','\0','\0'};
    int i;
    for (i=0; i < ([command length]/2); i++) {
        byte_chars[0] = [command characterAtIndex:i*2];
        byte_chars[1] = [command characterAtIndex:i*2+1];
        whole_byte = strtol(byte_chars, NULL, 16);
        [_data appendBytes:&whole_byte length:1];
    }

    //<021202de 34332535 30252037 25> // the desired NSMutableData command
}

Some incorrect results

// command = [NSString stringWithFormat:@"0212%02x25%02x25%02x%02x25", value1, value2, value3, value4];
// <02122de2 52b25320 72>

回答1:

Here, what can inspire you:

I made a category method of NSData (you may find this code elsewhere on SO, don't remember where exactly, it's not mine, there are a few questions about that in SO, but I think I mixed the various answers), adding this:

+(NSData *)dataWithStringHex:(NSString *)string
{
    NSString *cleanString;
    cleanString = [string stringByReplacingOccurrencesOfString:@"<" withString:@""];
    cleanString = [cleanString stringByReplacingOccurrencesOfString:@">" withString:@""];
    cleanString = [cleanString stringByReplacingOccurrencesOfString:@" " withString:@""];

    NSInteger length = [cleanString length];
    uint8_t buffer[length/2];
    for (NSInteger i = 0; i < length; i+=2)
    {
        unsigned result = 0;
        NSScanner *scanner = [NSScanner scannerWithString:[cleanString substringWithRange:NSMakeRange(i, 2)]];
        [scanner scanHexInt:&result];
        buffer[i/2] = result;
    }
    return  [[NSMutableData alloc] initWithBytes:&buffer length:length/2];
}

I use it sometimes to debug. That explains the thing going with cleanString for removing the spaces, the "<" and the ">". In other words, if you have a NSString 012345, it will make a NSData of 012345. Pretty handy sometimes.

So checking with you sample (by the way, it was missing a "0" in the stringWithFormat:):

int value1 = 734;
int value2 = 43;
int value3 = 50;
int value4 = 7;

NSString *command = [NSString stringWithFormat:@"02102%02x25%02x25%02x%02x25", value1, value2, value3, value4];
NSLog(@"Command: %@", command);
NSData *data = [NSData dataWithStringHex:command];
NSLog(@"Data: %@", data);

With output:

Command: 021022de252b25320725
Data: <021022de 252b2532 0725>



回答2:

- (void)sendValues:(int)value1 value2:(int)value2 value3:(int)value3 value4:(int)value4
{

    uint8_t command [] = {0x02, 0x12,0x02,value1,0x25,value2, 0x25, value3, value4, 0x25};
    NSData *data = [NSData dataWithBytes:command length:sizeof(command)];
}

data contains <021202de 252b2532 0725>

maybe this gets you started.