用ascii数字操作一个字符

时间:2015-02-18 23:56:36

标签: c char int ascii

我想使用ascii值更改char。

到目前为止,我有这个:

unsigned char invertChar(char c){

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

}

//// EDIT。

到目前为止一直很好,但我遇到了另一个问题。

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]);

}

问题在于,当我执行printf时,我得到了这个(在我的所有打印件上):

Inverted Char: 4294967194

感谢您的时间和专业知识。

1 个答案:

答案 0 :(得分:0)

由于invertedBuffer []数组是一个带符号的char,它将被符号扩展为一个负整数,当用%u打印时,将产生2 ^ 32-x。 - Lee Daniel Crocker