IOS:Convert string to hexadecimal array

后端 未结 2 1970
爱一瞬间的悲伤
爱一瞬间的悲伤 2021-01-14 07:16

i have string representing data .i need to convert those data to the hex array .By using the hex array data i can pass it to the CRC for writing to the peripheral

M

相关标签:
2条回答
  • 2021-01-14 07:44

    A. The hexadecimal format is simply another representation of the same data.

    B. You do not convert them into hex array. Every character has a number. For example in ASCII and UTF-8 the A has the number 65 (decimal representation). This is 0x41 in hex representation.

    'A' (ASCII) == 65 == 0x41.

    A hex number has the the digits 0-9, a-f, wherein a has the value of 10, b the value of 11 … It is converter into decimal representation by multiplying the upper digit by 16 and adding the lower digit. (0x41: 4 x 16 + 1 = 65.)

    Please read and understand this: http://en.wikipedia.org/wiki/Hexadecimal

    C. To convert a string into its number, you have to know, which code you want to apply. Probably you want to use UTF-8.

    NSString *text = @"helloworld123123989128739";
    NSUInteger length = [text lengthOfBytesUsingEncoding:NSUTF8StringEncoding];
    char data[length];
    [text getCString:data maxLength:length usingEncoding:NSUTF8StringEncoding];
    // Here we go
    
    0 讨论(0)
  • 2021-01-14 07:45

    Byte[] class is an array of characters. I mean you can only set one character at it's index.

    If we have

    Byte comm[24]; then comm[0]=0x01; is looks like confusing here because it only saves one character.

    And the statement will be like comm[0]='x';.

    Below code will creates Byte[] from given string.

    NSString *stringsdata=@"helloworld1234567812345q";
        CFStringRef cfString = (__bridge CFStringRef)stringsdata;
        char *array = charArrayFromCFStringRef(cfString);
        size_t length= strlen(array);
    
        Byte comm[24];
        for (int i = 0; i < length; i++) {
            comm[i] = array[i];
        }
    

    Conversion function:

    char * charArrayFromCFStringRef(CFStringRef stringRef) {
        if (stringRef == NULL) {
            return NULL;
        }
    
        CFIndex length = CFStringGetLength(stringRef);
        CFIndex maxSize = CFStringGetMaximumSizeForEncoding(length, kCFStringEncodingUTF8);
    
        char *buffer = (char *)malloc(maxSize);
        if (CFStringGetCString(stringRef, buffer, maxSize, kCFStringEncodingUTF8)) {
            return buffer;
        }
        return NULL;
    }
    

    OutPut:

    Printing description of comm:
    (Byte [24]) comm = {
      [0] = 'h'
      [1] = 'e'
      [2] = 'l'
      [3] = 'l'
      [4] = 'o'
      [5] = 'w'
      [6] = 'o'
      [7] = 'r'
      [8] = 'l'
      [9] = 'd'
      [10] = '1'
      [11] = '2'
      [12] = '3'
      [13] = '4'
      [14] = '5'
      [15] = '6'
      [16] = '7'
      [17] = '8'
      [18] = '1'
      [19] = '2'
      [20] = '3'
      [21] = '4'
      [22] = '5'
      [23] = 'q'
    }
    

    The thing here is if you still convert any character from Byte[] then you can only save one character at any index.

    Because for above characters it's hex value is more than one character and you can only save one character in Byte[].

    I suggest to use NSArray to save each character's hex value in NSString format.

    0 讨论(0)
提交回复
热议问题