Strings of unsigned chars

后端 未结 3 1096
猫巷女王i
猫巷女王i 2021-01-16 04:46

Here\'s an interesting one. I\'m writing an AES encryption algorithm, and have managed to get it making accurate encryptions. The trouble comes when I attempt to write the r

3条回答
  •  迷失自我
    2021-01-16 05:35

    Cast your value to unsigned char first:

    char input = 250;                                    // just an example
    
    unsigned int n = static_cast(input);  // NOT: "unsigned int n = input;"
    //               ^^^^^^^^^^^^^^^^^^^^^^^^^^
    

    The problem is that your char happens to be signed, and so its value is not the "byte value" that you want -- you have to convert to unsigned char to get that.

提交回复
热议问题