如何在 C++ 中将 char* 转换为 byte*?
How to cast a char* to a byte* in c++?
我是 c++ 新手,想将 char*
从 std::string
转换为 byte*
。
这是我的代码:
inline string XOR(const string &value, const string &key) {
string retval(value);
CryptoPP::xorbuf(&retval[0], &key[0], retval.length());
return retval;
}
在g++
中,输出为:
AESXCBC128.cpp: In function ‘std::string CryptoPP::XOR(const string&, const string&)’:
AESXCBC128.cpp:79:48: error: invalid conversion from ‘char*’ to ‘byte* {aka unsigned char*}’ [-fpermissive]
xorbuf(&retval[0], &key[0], retval.length());
^
AESXCBC128.cpp:45:6: error: initializing argument 1 of ‘void CryptoPP::xorbuf(byte*, const byte*, size_t)’ [-fpermissive]
void xorbuf(byte *buf, const byte *mask, size_t count)
^
AESXCBC128.cpp:79:48: error: invalid conversion from ‘const char*’ to ‘const byte* {aka const unsigned char*}’ [-fpermissive]
xorbuf(&retval[0], &key[0], retval.length());
^
AESXCBC128.cpp:45:6: error: initializing argument 2 of ‘void CryptoPP::xorbuf(byte*, const byte*, size_t)’ [-fpermissive]
void xorbuf(byte *buf, const byte *mask, size_t count)
inline string XOR(const string &value, const string &key) {
string retval(value);
CryptoPP::xorbuf(&retval[0], &key[0], retval.length());
return retval;
}
加密++ typedefs a byte
in confg.h
:
typedef unsigned char byte;
您可以使用类似的东西:
CryptoPP::xorbuf(
reinterpret_cast<byte*>(&retval[0]),
reinterpret_cast<const byte*>(&key[0]),
retval.length());
或者,您可以使用 C 风格的转换来实现:
CryptoPP::xorbuf((byte*)&retval[0], (const byte*)&key[0], retval.length());
下面是SecByteBlock
的一些类似问题,它是字节数组而不是字符数组:
- string to SecByteBlock conversion
这里有一些关于 C++ 转换的参考资料:
- When should static_cast, dynamic_cast, const_cast and reinterpret_cast be used?
- Regular cast vs. static_cast vs. dynamic_cast
我是 c++ 新手,想将 char*
从 std::string
转换为 byte*
。
这是我的代码:
inline string XOR(const string &value, const string &key) {
string retval(value);
CryptoPP::xorbuf(&retval[0], &key[0], retval.length());
return retval;
}
在g++
中,输出为:
AESXCBC128.cpp: In function ‘std::string CryptoPP::XOR(const string&, const string&)’:
AESXCBC128.cpp:79:48: error: invalid conversion from ‘char*’ to ‘byte* {aka unsigned char*}’ [-fpermissive]
xorbuf(&retval[0], &key[0], retval.length());
^
AESXCBC128.cpp:45:6: error: initializing argument 1 of ‘void CryptoPP::xorbuf(byte*, const byte*, size_t)’ [-fpermissive]
void xorbuf(byte *buf, const byte *mask, size_t count)
^
AESXCBC128.cpp:79:48: error: invalid conversion from ‘const char*’ to ‘const byte* {aka const unsigned char*}’ [-fpermissive]
xorbuf(&retval[0], &key[0], retval.length());
^
AESXCBC128.cpp:45:6: error: initializing argument 2 of ‘void CryptoPP::xorbuf(byte*, const byte*, size_t)’ [-fpermissive]
void xorbuf(byte *buf, const byte *mask, size_t count)
inline string XOR(const string &value, const string &key) { string retval(value); CryptoPP::xorbuf(&retval[0], &key[0], retval.length()); return retval; }
加密++ typedefs a byte
in confg.h
:
typedef unsigned char byte;
您可以使用类似的东西:
CryptoPP::xorbuf(
reinterpret_cast<byte*>(&retval[0]),
reinterpret_cast<const byte*>(&key[0]),
retval.length());
或者,您可以使用 C 风格的转换来实现:
CryptoPP::xorbuf((byte*)&retval[0], (const byte*)&key[0], retval.length());
下面是SecByteBlock
的一些类似问题,它是字节数组而不是字符数组:
- string to SecByteBlock conversion
这里有一些关于 C++ 转换的参考资料:
- When should static_cast, dynamic_cast, const_cast and reinterpret_cast be used?
- Regular cast vs. static_cast vs. dynamic_cast