简体   繁体   English

如何将fom字符串转换为十六进制?

[英]How to convert fom string to hexadecimal?

I have the following string: s="80" . 我有以下string: s="80" I need to put this in an unsigned char k[]. The unsigned char should look like this: unsigned char k[]={0x38,0x34}, where 0x38=8 and 0x34=0 我需要将其放入一个unsigned char k[]. The unsigned char should look like this: unsigned char k[]={0x38,0x34}, where 0x38=8 and 0x34=0 unsigned char k[]. The unsigned char should look like this: unsigned char k[]={0x38,0x34}, where 0x38=8 and 0x34=0 These are the hexadecimal values for 8 and 0. How to do this? unsigned char k[]. The unsigned char should look like this: unsigned char k[]={0x38,0x34}, where 0x38=8 and 0x34=0这些是8和0的十六进制值。如何执行此操作? Need some help! 需要一些帮助!

Please give some code. 请输入一些代码。 Thx 谢谢

I am working on ubuntu c++ code. 我正在研究ubuntu c ++代码。 THX! 谢谢!

I use this for an encryption! 我用它来加密! I need 0x38 in an unsigned char.PLEASE HELP! 我需要0x38的未签名字符。请帮助! Need some code:) 需要一些代码:)

EDIT: 编辑:

HOW TO OBTAIN THE DEC/CHAR VALUE AND PUT IT IN AN unsigned char k[] ? 如何获得DEC / CHAR值并将其放入unsigned char k[] I've realised that it's ok if in the unsigned char [] i have the dec values {56,52} of the 8 and 0 that i have in the string! 我已经意识到,如果在unsigned char [] i have the dec values {56,52} of the 8 and 0 that i have in the string!

我认为无论您存储的是'8'还是0x39 ,它们在计算机中都将以相同的二进制数字形式出现。

Assuming you want this string converted as ASCII (or UTF-8) it is already in the correct format. 假设您要将此字符串转换为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";

If you want it in an int array, then just copy it: 如果要在int数组中进行复制,则只需将其复制:

int   data[2];
std::copy(s.begin(), s.end(), data);

I think you do not really understand what you are asking. 我认为您不太了解您的要求。

The following are synonyms: 以下是同义词:

std::string s = "\x38\x30";
std::string s = "80";

As the following are synonyms: 以下是同义词:

char c = '8',  s = '0' ;
char c = s[0], s = s[1];
char c = 0x38, s = 0x30;

It is exactly the same (except if your base encoding is not ASCII). 完全相同(除非您的基本编码不是ASCII)。 This is not an encryption. 这不是加密。

You can try it. 你可以试试。 I did not write these codes. 我没有写这些代码。 I found I like you 我发现我喜欢你

#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");
}
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;

将该字符串转换为char数组,然后从每个char中减去“ 0”。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM