I have to following code, which doesn't generate my expected results. Adding unsigned char would make it work. ( I realized it is not working by having another function doing the reverse, but not getting the reverse effect!) Can someone explain a bit to me.
The code is to substitute a 8 char string to some random chars.
My current code :
char subs[8][256];
void perfSubs(char* input){
for (int i = 0; i < 8; i++)
input[i] = subs[i][input[i]];
}
I should change it to
input[i] = subs[i][(unsigned char)input[i]];
As mentioned by others and is well known that C has unsigned char
, signed char
and char
types.
char
has the same range as either unsigned char
, signed char
but is still a different type.
subs[i][input[i]];
may involve a negative index into subs[i][some_negative_value]
casuing the problem.
input[i] = subs[i][(unsigned char)input[i]];
folds the negative values into positive ones that may come out of input[i]
, but that is implementation defined behavior as to how.
A simple solution can be had just using char
.
To avoid a negative index, subtract the minimum char
value, CHAR_MIN
, whenever code uses char
as an index. A typical value for CHAR_MIN
is 0 or -128.
Also avoid the magic number 256 and use an array size based on char
.
char subs[8][CHAR_MAX + 1 - CHAR_MIN];
input[i] = subs[i][input[i] - CHAR_MIN];
Depending on if char
is signed or not, the location of using index 'A'
may result in a different index, though consistent within a given compilation.
subs[0]['A' - CHAR_MIN] = 'x';
The standard does not dictate whether char
is a signed type or an unsigned type. It can be signed type in one platform and unsigned type in another platform. If you want your code to be portable, pick a type that you can depend on.
In your case, you should use:
void perfSubs(unsigned char* input){
for (int i = 0; i < 8; i++)
input[i] = subs[i][input[i]];
}
This makes sure that you get an unsigned char*
to the function and not have to rely on whether the char
type is a signed type or an unsigned type.
带符号的char可以保存-128到127之间的值。由于您使用该字节存储0到255之间的值,因此您需要一个unsigned char。
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.