我有一个接受无符号字符的方法,但我想将 0x01 传递给它。
我知道我可以通过“\x01”来表示字符串中的十六进制字符... 但这仍然是一个带符号的字符。
编辑: 一些代码:
kennys_hash((unsigned char const *)"\x00"); // the method call
错误:
src/main.cpp:88: error: invalid conversion from ‘const unsigned char*’ to ‘unsigned char’
src/main.cpp:88: error: initializing argument 1 of ‘unsigned char kennys_hash(unsigned char)’
方法头:
unsigned char kennys_hash(unsigned char out)
另外,当强制转换为 unsigned char 时,我得到这个错误:
src/main.cpp:88: error: cast from ‘const char*’ to ‘unsigned char’ loses precision
最佳答案
0x01
与1
一样,都是正数,因此不管是有符号还是无符号,值都是一样的。
如果您想在文字上使用无符号类型,请使用 0x01u
。
关于C++:如何将十六进制字符转换为无符号字符?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5421152/