IOS:Convert string to hexadecimal array

时光怂恿深爱的人放手 提交于 2019-12-01 06:53:54

问题


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

My string data is like this

          NSString *stringsdata=@"helloworld1234567812345q";

i need to convert to hex format array like

       {0x0h,0x0e............0x0q}. 

so by using this array i can keep the data in the crc and write it to the peripheral data as

            Byte comm[24];
            comm[0]=0x01;
            comm[1]=0x30;
            comm[2]=0x62;
            comm[3]=0x00;................

have tried with many possible solutions but not luck.can any body help will be greatly appreciated.


回答1:


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.




回答2:


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


来源:https://stackoverflow.com/questions/27700431/iosconvert-string-to-hexadecimal-array

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!