IOS:将字符串转换为十六进制数组 [英] IOS:Convert string to hexadecimal array

查看:163
本文介绍了IOS:将字符串转换为十六进制数组的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有字符串表示数据。我需要将这些数据转换为十六进制数组。通过使用十六进制数组数据,我可以将它传递给CRC写入外设。



我的字符串数据是这样的

  NSString * stringsdata = @helloworld1234567812345q; 

我需要转换为十六进制格式数组,如

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

所以通过使用这个数组,我可以将数据保存在crc中并将其写入外设数据中

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

已经尝试过很多可能的解决方案,但是没有运气。任何机构的帮助都将不胜感激。

Byte [] class是一个字符数组。我的意思是你只能在它的索引处设置一个字符。



如果我们有

Byte comm [24]; 然后 comm [0] = 0x01; 在这里看起来很混乱,因为它只保存一个字符。 >

并且该语句将像 comm [0] ='x';



下面的代码会从给定的字符串中创建 Byte []

  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 comm [i] = array [i];





转换函数:

$ b pre> 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;
}
返回NULL;
}

输出:

 通讯打印说明:
(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'





$ p

这里的东西是,如果你仍然从 Byte [] 那么你只能在任何索引处保存一个字符。



因为对于上面的字符,它的十六进制值不止一个字符,而你只能保存 Byte [] 中的一个字符。



我建议使用 NSArray 将每个字符的十六进制值保存为 NSString 格式。


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.

解决方案

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.

这篇关于IOS:将字符串转换为十六进制数组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆