如何在目标c中动态创建无符号字符类型数组

发布于 2024-12-04 19:00:56 字数 653 浏览 1 评论 0原文

我想根据另一个 nsmutablearray 的值在 Objective c 中创建一个 unsigned char 数组, 请帮助我,

unsigned char buffer2[24];
        buffer2[0] = 0X55; buffer2[1]=0x66; buffer2[2]=0x77; buffer2[3]=0x88; buffer2[4]=0x44;//print command
        buffer2[5] = 0X1D; buffer2[6]=0x6B; buffer2[7]=02; buffer2[8]=0x0D; 
        buffer2[9] = 0X35; buffer2[10]=0x30; buffer2[11]=0x30; buffer2[12]=0x30; buffer2[13]=0x33;
        buffer2[14] = 0X35; buffer2[15]=0x37; buffer2[16]=0x37; buffer2[17]=0x30; buffer2[18]=0x33;
        buffer2[19] = 0X30; buffer2[20]=0x31; buffer2[21]=0x38; buffer2[22]=0x37; buffer2[23]=0x30;

我想动态传递该数组的值。

提前致谢。 希瓦姆

I want to create an unsigned char array in objective c from the values of another nsmutablearray,
Please help me

unsigned char buffer2[24];
        buffer2[0] = 0X55; buffer2[1]=0x66; buffer2[2]=0x77; buffer2[3]=0x88; buffer2[4]=0x44;//print command
        buffer2[5] = 0X1D; buffer2[6]=0x6B; buffer2[7]=02; buffer2[8]=0x0D; 
        buffer2[9] = 0X35; buffer2[10]=0x30; buffer2[11]=0x30; buffer2[12]=0x30; buffer2[13]=0x33;
        buffer2[14] = 0X35; buffer2[15]=0x37; buffer2[16]=0x37; buffer2[17]=0x30; buffer2[18]=0x33;
        buffer2[19] = 0X30; buffer2[20]=0x31; buffer2[21]=0x38; buffer2[22]=0x37; buffer2[23]=0x30;

I want to pass values of this array dynamically.

Thanks in advance.
Shivam

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

謌踐踏愛綪 2024-12-11 19:00:56

您可以使用 C 的 malloccalloc 函数:

int count = 24;
unsigned char *buffer = (unsigned char *)calloc(count, sizeof(unsigned char));

使用后不要忘记释放它:

free(buffer);

简单的例子:

NSMutableArray *array;
unsigned char *buffer = (unsigned char *)calloc([array count], sizeof(unsigned char));
for (int i=0; i<[array count]; i++)
    buffer[i] = [[array objectAtIndex:i] unsignedCharValue];
// use array
free(buffer);

You can use malloc or calloc functions of C:

int count = 24;
unsigned char *buffer = (unsigned char *)calloc(count, sizeof(unsigned char));

Don't forget to free it after using:

free(buffer);

Simple example:

NSMutableArray *array;
unsigned char *buffer = (unsigned char *)calloc([array count], sizeof(unsigned char));
for (int i=0; i<[array count]; i++)
    buffer[i] = [[array objectAtIndex:i] unsignedCharValue];
// use array
free(buffer);
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文