我有以下string: s="80"
。我需要把它放进去
unsigned char k[]. The unsigned char should look like this: unsigned char k[]={0x38,0x34}, where 0x38=8 and 0x34=0
这些是8和0的十六进制值。如何做到这一点?需要一些帮助!
请提供一些代码。 THX
我正在研究ubuntu c ++代码。 THX!
我用它来加密!我需要一个unsigned char中的0x38.PLEASE HELP!需要一些代码:)
编辑:
如何获得DEC / CHAR值并将其投入unsigned char k[]
?
我已经意识到如果在unsigned char [] i have the dec values {56,52} of the 8 and 0 that i have in the string!
答案 0 :(得分:3)
我认为无论您存储'8'
还是0x39
,它们都将由计算机显示为相同的二进制数字。
答案 1 :(得分:3)
假设您希望将此字符串转换为ASCII(或UTF-8),则其格式已正确。
std::string s="80";
std::cout << "0x" << std::hex << static_cast<int>(s[0]) << "\n";
std::cout << "0x" << std::hex << static_cast<int>(s[1]) << "\n";
如果你想在一个int数组中,那么只需复制它:
int data[2];
std::copy(s.begin(), s.end(), data);
答案 2 :(得分:1)
我觉得你真的不明白你在问什么。
以下是同义词:
std::string s = "\x38\x30";
std::string s = "80";
以下是同义词:
char c = '8', s = '0' ;
char c = s[0], s = s[1];
char c = 0x38, s = 0x30;
它完全相同(除非您的基本编码不是ASCII)。这不是加密。
答案 3 :(得分:0)
std::string s = "80";
unsigned char * pArray = new unsigned char[ s.size() ];
const char * p = s.c_str();
unsigned char * p2 = pArray;
while( *p )
*p2++ = *p++;
delete []pArray;
答案 4 :(得分:0)
你可以尝试一下。我没有写这些代码。我发现我喜欢你
#include <algorithm>
#include <sstream>
#include <iostream>
#include <iterator>
#include <iomanip>
namespace {
const std::string test="mahmutefe";
}
int main() {
std::ostringstream result;
result << std::setw(2) << std::setfill('0') << std::hex << std::uppercase;
std::copy(test.begin(), test.end(), std::ostream_iterator<unsigned int>(result, " "));
std::cout << test << ":" << result.str() << std::endl;
system("PAUSE");
}
答案 5 :(得分:-3)
将该字符串转换为char数组,然后从每个char中减去“0”。