C++:如何将十六进制字符转换为无符号字符?

标签 c++

我有一个接受无符号字符的方法,但我想将 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

最佳答案

0x011一样,都是正数,因此不管是有符号还是无符号,值都是一样的。

如果您想在文字上使用无符号类型,请使用 0x01u

关于C++:如何将十六进制字符转换为无符号字符?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5421152/

相关文章:

c++ - Gauss-Seidel 函数中的类型无效

python - C++/嵌入式 Python : Can I retrieve Python file name and line number when a C++ function is called from Python

c++ - QT-信号和插槽setEnabled(bool)插槽不工作

c++ - CMAKE:在函数内创建和构建列表——具有目录或全局范围

c++ - OpenGL - 矩阵逻辑错误

c++ - 使用 2 个字符串定界符 C++ 选择文本文件的一部分

c++ - 可以使用变量号的函数。参数在 VC++ 中重载?

c++ - 为什么我不能在派生类重写函数中调用 protected 虚拟基类函数?

c++ - OpenCV:是否可以在 Linux 中的进程之间共享网络摄像头

c++ - 如何在模板函数中使用 CUBLAS 库?