Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/c/59.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
按ascii数操作字符_C_Char_Int_Ascii - Fatal编程技术网

按ascii数操作字符

按ascii数操作字符,c,char,int,ascii,C,Char,Int,Ascii,我想使用ascii值更改字符 到目前为止,我有: unsigned char invertChar(char c){ unsigned char y; printf("255 - %d=", c); y = 255 - (int)c; printf("%d\n", y); return y; } ////编辑 到目前为止还不错,但我遇到了另一个问题 int main(void){ char source[10]; //Has info. char i

我想使用ascii值更改字符

到目前为止,我有:

unsigned char invertChar(char c){

    unsigned char y;
    printf("255 - %d=", c);
    y = 255 - (int)c;
    printf("%d\n", y);
    return y;

}
////编辑

到目前为止还不错,但我遇到了另一个问题

int main(void){
char source[10]; //Has info.
char invertedBuffer[10];
unsigned char help;
int counter;


for(counter = 0; counter<10; counter++){
help = invertChar(source[counter]);
 invertedBuffer[counter] = help;
  printf("Inverted Char: %u\n", invertedBuffer[counter]);

}

感谢您的时间和专业知识

由于invertedBuffer[]数组是有符号字符,它将被符号扩展为负整数,当使用%u打印时,将导致2^32-xLee Daniel Crocker

使用
无符号字符
代替
字符
%u
代替
%d
@DoxyLover所以y是无符号的?
有符号字符
的范围为-128到+127。显然,在此范围内不能表示255或223<代码>无符号字符的范围为0到255。@DoxyLover谢谢,我边走边学习。你帮我解决了函数中的问题,但是当我在main中调用它时,它会怎么样呢?非常感谢您的时间。同样的问题:由于invertChar是有符号字符,它将被符号扩展为负整数,当使用%u打印时,将产生2^32-x。
Inverted Char: 4294967194