C字符串的无符号字符数组

时间:2021-09-09 19:59:32

I have an array of strings like such

我有一个这样的字符串数组

char *T[] = {"0000", "0001", "0010", "0011", "0100", "0101", "0110", "0111", "1000", "1001", "1010", "1011", "1100", "1101", "1110", "1111"};

When I do it like this, however, where each string is an array of unsigned chars

当我这样做时,每个字符串都是一个无符号字符数组

unsigned char *T[] = {"0000", "0001", "0010", "0011", "0100", "0101", "0110", "0111", "1000", "1001", "1010", "1011", "1100", "1101", "1110", "1111"};

I get the error "Initializing 'unsigned char *' with an expression of type 'char[5]' converts between pointers to integer types with different sign." I'm guessing that means that some of the representations of "0" and "1" that are getting used are signed, but I'm not sure why/how to deal with that. I'd like to have an array of strings where each string is an array of unsigned chars rather than signed chars. Can someone help with that?

我得到了错误的“初始化'unsigned char *',其表达式类型为'char[5]',将指针转换为具有不同符号的整数类型。”我猜这意味着一些正在使用的“0”和“1”的表示已经签名,但我不知道为什么/如何处理它。我希望有一个字符串数组,其中每个字符串都是一个未签名的字符数组,而不是签名的字符。有人能帮忙吗?

Note: this is for a HW problem, but is not the actual problem, and is just a minor step in one of many possible solutions. However, it would be good if you could help me understand it without giving me an explicit answer. Thanks.

注意:这是一个HW问题,但不是实际的问题,只是许多可能解决方案中的一个小步骤。但是,如果你能帮助我理解它而不给我一个明确的答案,那就太好了。谢谢。

1 个解决方案

#1


2  

C strings, one of many ways one could represent a string, consist of arrays of char terminated by a trailing char which has the null value. That's what you get type-wise when you have "0000" in your code.

C字符串是表示字符串的一种方式,它由以具有空值的尾字符结尾的字符数组组成。当您的代码中有“0000”时,这就是类型方面的结果。

What you want is to assign "0000" to be an array of unsigned char terminated by a trailing unsigned char which has the null value. Considering what you are starting with, you will have to cast, or perhaps represent your initial data in a manner that doesn't require casting.

您想要的是将“0000”赋值为一个无符号字符数组,该数组以具有空值的尾无符号字符作为结束。考虑到您要从什么开始,您将不得不强制转换,或者可能以不需要强制转换的方式表示初始数据。

unsigned char T[][] = { { 0x30, 0x30, 0x30, 0x30, 0x00 }, 
               { 0x30, 0x30, 0x30, 0x31, 0x00 }, 
               { 0x30, 0x30, 0x31, 0x30, 0x00 }, 
               { 0x30, 0x30, 0x31, 0x31, 0x00 }, 
               { 0x30, 0x31, 0x30, 0x30, 0x00 }, 
               { 0x30, 0x31, 0x30, 0x31, 0x00 }, 
               { 0x30, 0x31, 0x31, 0x30, 0x00 }, 
               { 0x30, 0x31, 0x31, 0x31, 0x00 }, 
               { 0x31, 0x30, 0x30, 0x30, 0x00 }, 
               { 0x31, 0x30, 0x30, 0x31, 0x00 }, 
               { 0x31, 0x30, 0x31, 0x30, 0x00 }, 
               { 0x31, 0x30, 0x31, 0x31, 0x00 }, 
               { 0x31, 0x31, 0x30, 0x30, 0x00 }, 
               { 0x31, 0x31, 0x30, 0x31, 0x00 }, 
               { 0x31, 0x31, 0x31, 0x30, 0x00 }, 
               { 0x31, 0x31, 0x31, 0x31, 0x00 }
              };

The main problem I see with this approach is that it removes most of the advantage of having a C style string in the first place. With an unsigned char "string", you have none of the standard string libraries at your disposal, so you will have to cast back to signed char string types if you want to use printf, or any other string oriented function.

我认为这种方法的主要问题是,它首先去掉了C样式字符串的大部分优点。对于无符号字符“字符串”,您没有任何标准的字符串库可以使用,因此如果您想使用printf或任何其他面向字符串的函数,您将不得不返回到有符号字符字符串类型。

Really, you are only using two values for each possible character position "0" and "1". Unless there is a compelling reason to do it in a string, consider an array of boolean values to reduce the chance of a string like "0hello" working it's way into the code, or better yet if you have been introduced to bit fields, use the bits within an unsigned char as bit fields (discarding any concept that you're dealing with strings).

实际上,每个可能的字符位置“0”和“1”只使用两个值。除非有一个令人信服的理由去做在一个字符串,考虑一个布尔值数组来减少字符串的机会像“0你好”工作进入代码,或者更好的是,如果你介绍了一些字段,使用位域的比特在一个unsigned char(丢弃任何概念,你处理字符串)。

The advantages to the last technique include using less memory and the inability for the value to be other than 0 or 1; however, you will have to write a small collection of routines to translate the packed bits into something human readable.

最后一种技术的优点包括使用更少的内存和无法使值为0或1;但是,您将不得不编写一些例程来将打包的片段翻译成人类可读的内容。

unsigned char[] = { 0x00, 0x01, 0x02, 0x03, 0x04,
                    0x05, 0x06, 0x07, 0x08, 0x09,
                    0x0A, 0x0B, 0x0C, 0x0D, 0x0E,
                    0x0F };

void displayChar(unsigned char value) {
  switch (value) {
    case 0x00: printf("0000"); break;
    case 0x01: printf("0001"); break;
    case 0x02: printf("0010"); break;
    case 0x03: printf("0011"); break;
... and so on ...

#1


2  

C strings, one of many ways one could represent a string, consist of arrays of char terminated by a trailing char which has the null value. That's what you get type-wise when you have "0000" in your code.

C字符串是表示字符串的一种方式,它由以具有空值的尾字符结尾的字符数组组成。当您的代码中有“0000”时,这就是类型方面的结果。

What you want is to assign "0000" to be an array of unsigned char terminated by a trailing unsigned char which has the null value. Considering what you are starting with, you will have to cast, or perhaps represent your initial data in a manner that doesn't require casting.

您想要的是将“0000”赋值为一个无符号字符数组,该数组以具有空值的尾无符号字符作为结束。考虑到您要从什么开始,您将不得不强制转换,或者可能以不需要强制转换的方式表示初始数据。

unsigned char T[][] = { { 0x30, 0x30, 0x30, 0x30, 0x00 }, 
               { 0x30, 0x30, 0x30, 0x31, 0x00 }, 
               { 0x30, 0x30, 0x31, 0x30, 0x00 }, 
               { 0x30, 0x30, 0x31, 0x31, 0x00 }, 
               { 0x30, 0x31, 0x30, 0x30, 0x00 }, 
               { 0x30, 0x31, 0x30, 0x31, 0x00 }, 
               { 0x30, 0x31, 0x31, 0x30, 0x00 }, 
               { 0x30, 0x31, 0x31, 0x31, 0x00 }, 
               { 0x31, 0x30, 0x30, 0x30, 0x00 }, 
               { 0x31, 0x30, 0x30, 0x31, 0x00 }, 
               { 0x31, 0x30, 0x31, 0x30, 0x00 }, 
               { 0x31, 0x30, 0x31, 0x31, 0x00 }, 
               { 0x31, 0x31, 0x30, 0x30, 0x00 }, 
               { 0x31, 0x31, 0x30, 0x31, 0x00 }, 
               { 0x31, 0x31, 0x31, 0x30, 0x00 }, 
               { 0x31, 0x31, 0x31, 0x31, 0x00 }
              };

The main problem I see with this approach is that it removes most of the advantage of having a C style string in the first place. With an unsigned char "string", you have none of the standard string libraries at your disposal, so you will have to cast back to signed char string types if you want to use printf, or any other string oriented function.

我认为这种方法的主要问题是,它首先去掉了C样式字符串的大部分优点。对于无符号字符“字符串”,您没有任何标准的字符串库可以使用,因此如果您想使用printf或任何其他面向字符串的函数,您将不得不返回到有符号字符字符串类型。

Really, you are only using two values for each possible character position "0" and "1". Unless there is a compelling reason to do it in a string, consider an array of boolean values to reduce the chance of a string like "0hello" working it's way into the code, or better yet if you have been introduced to bit fields, use the bits within an unsigned char as bit fields (discarding any concept that you're dealing with strings).

实际上,每个可能的字符位置“0”和“1”只使用两个值。除非有一个令人信服的理由去做在一个字符串,考虑一个布尔值数组来减少字符串的机会像“0你好”工作进入代码,或者更好的是,如果你介绍了一些字段,使用位域的比特在一个unsigned char(丢弃任何概念,你处理字符串)。

The advantages to the last technique include using less memory and the inability for the value to be other than 0 or 1; however, you will have to write a small collection of routines to translate the packed bits into something human readable.

最后一种技术的优点包括使用更少的内存和无法使值为0或1;但是,您将不得不编写一些例程来将打包的片段翻译成人类可读的内容。

unsigned char[] = { 0x00, 0x01, 0x02, 0x03, 0x04,
                    0x05, 0x06, 0x07, 0x08, 0x09,
                    0x0A, 0x0B, 0x0C, 0x0D, 0x0E,
                    0x0F };

void displayChar(unsigned char value) {
  switch (value) {
    case 0x00: printf("0000"); break;
    case 0x01: printf("0001"); break;
    case 0x02: printf("0010"); break;
    case 0x03: printf("0011"); break;
... and so on ...