如何将char转换为十六进制然后将其存储到变量? [英] How do I convert a char to hex then store it to a variable?

查看:506
本文介绍了如何将char转换为十六进制然后将其存储到变量?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我知道如何将十六进制打印到屏幕,但我不知道如何将它存储到变量。

 buff =    Hello; 
for (i = 0 ; i< strlen(buff); i ++){
kar [i] = buff [i];
cout<< 0x<< hex<< ( int )kar [i]<< ENDL;
}



那么,我如何将每个十六进制数存储到变量中 - 只要我知道,类型是unsigned char。

谢谢

解决方案

其中一种方法是:使用 sprintf

http://www.cplusplus.com/reference/cstdio/sprintf [ ^ ]。



对于1字节字符的十六进制字符串表示,您需要两个十六进制数字,因此格式为%02x,例如在此示例中:http://stackoverflow.com/questions/11070183/sprintf-how-to- print-hexadecimal-number-with-leading-0-to-wide-width-2 [ ^ ]。



-SA


Rudi-peto问:

但无论如何,我将每个十六进制存储到缓冲区......但是如何将它存储到指针数组中,所以看起来喜欢:...

请参阅我对解决方案1的评论。



你真的需要熟悉指针和数组,就是这样。让我展示一种非常简单且记忆效率高的方法。比方说,你在问题中输入你称为buff的字节缓冲区,其长度为 count

  int  count = strlen(buff); 



在继续之前,你必须为输出字符串分配缓冲区,调用它输出。不是单独分配每个字符串并使用一些指向指针数组的指针(锯齿状数组),而是简单地将所有字符串放在一个缓冲区中。你需要多少记忆?如果我们计算'x',每个十六进制字符串将占用4个字节:还有两个半字节十六进制数字和null,因为我们正在弄乱那些愚蠢的以null结尾的字符串。因此,对于整个输出缓冲区,您只需要 count * 4 bytes:

  char  * output =  new   char  [count *  4 ]; 

// 做点什么......

delete [] output;



在循环中,你可以调用 sprintf (解决方案1)计算次。我们来做简单的手动指针算术。作为第一个 sprintf 调用传递的第一个字符串将位于缓冲区的最后,因此指针将与输出。要获取指向下一个字符串的指针,请增加此指针正好添加4.由于数组输出元素的编译时类型为1 -byte char ,编译器会将此4解释为4个字节。因此,在每次循环迭代中,每个字符串的字符串指针将等于 output + 4 * index ,其中 index 循环控制变量。这样,你将使用相同长度的 count 字符串填充数组。



基本上,我写了完整的伪代码给你。您在循环中填充所有输出十六进制字符串。稍后,您可以使用此输出数组来使用完全相同的指针算法来索引这些字符串中的任何一个。



或者,您也可以使用2D数组,如此处所述,理解在C ++中2D只是一个隐喻,解释同一指针的方法: http://www.cplusplus.com/doc/tutorial/arrays [ ^ ]。



还要注意术语2D,3D等等,只不过是一些术语。数学上正确的工作是等级。在严格的数学术语中,所有那些有限集总是零维对象。只有连续体才能具有维度这样的属性。从基本线性代数和分析几何,您可以了解连续线性空间可以具有一些整数维度。如果它们是某些连续空间中的歧管,则几何对象可以具有1,2,3和更多的尺寸;对于其他几何对象,尺寸可以是分数或完全未定义。但这是一个不同的故事......



-SA


有一个 C ++ 方式。

ostringstream class [ ^ ]为字符串提供 cout 用于标准输出。



尝试,例如:

  #include   <   iostream  >  
#include < string >
< span class =code-keyword> #include < sstream >
#include < vector >
使用 命名空间标准

int main()
{
string buff = Hello;

vector<字符串> v;

for size_t n = 0 ; n< buff.size(); ++ n)
{
ostringstream oss;
oss<< 0x<< hex<< ( int )buff [n];
v.push_back(oss.str());
}

for size_t n = 0 ; n< v.size(); ++ n)
{
cout<< v [n]<< ENDL;
}
}





或者,使用更新的编译器

  #include   <   iostream  >  
#include < string >
#include < sstream >
#include < vector >
使用 namespace std;

int main()
{
string buff = Hello;

vector<字符串> v;

for auto c:buff)
{
ostringstream oss;
oss<< 0x<< hex<<的static_cast< INT>(C);
v.push_back(oss.str());
}

for auto s:v)
cout<< s<< ENDL;
}


I know how to print hex to screen, but I dunno how to store it to variable.

buff = "Hello";
for(i=0; i<strlen(buff); i++){
   kar[i] = buff[i];
   cout << "0x" << hex << (int)kar[i] << endl;
}


So, how do I store each hex number to a variable -- as long as I know, the type is unsigned char.
Thanks

解决方案

One of the way is: use sprintf:
http://www.cplusplus.com/reference/cstdio/sprintf[^].

For the hexadecimal string representation of a 1-byte character, you need two hexadecimal digit, so the format will be "%02x", such as in this example: http://stackoverflow.com/questions/11070183/sprintf-how-to-print-hexadecimal-number-with-leading-0-to-have-width-2[^].

—SA


Rudi-peto asked:

But anyway, I store each hex into buffer... But how do I store it to array of pointer, so it looks like: …

Please see my comments to Solution 1.

You really need to get comfortable with pointers and arrays, that's it. Let me show one very simple and memory-efficient approach. Let's say, you input buffer of bytes you called buff in the question has the length count:

int count = strlen(buff);


Before you proceed, you have to allocate the buffer for output string, call it output. Instead of allocating each string separately and using some pointers to an array of pointers ("jagged array"), let's simply put then all in one buffer. How much memory you need? Each hexadecimal string will take 4 bytes, if we count 'x': also two half-byte hexadecimal digits and null, because we are messing up with those stupid null-terminated strings. So, for whole output buffer, you will need just count*4 bytes:

char * output = new char[count * 4];

// do something...

delete[] output;


In the loop, you can call sprintf (Solution 1) count times. Let's do simple "manual" pointer arithmetic. First string passed as the first sprintf call will be at the very end of the buffer, so the pointer would be the same as output. To get the pointer to next string, increment this pointer adding exactly 4. As the compile-time type of the element of the array output is 1-byte char, the compiler will interpret this 4 as 4 bytes. So, in each loop iteration, the string pointer for each string will be equal to output + 4 * index, where index is the loop control variable. This way, you will populate the array with count strings of the same length.

Essentially, I wrote the complete pseudo-code for you. You populate all the output hex string in the loop. Later on, you can use this output array to access any of these string by index, using exactly the same pointer arithmetic.

Alternatively, you can also use "2D" arrays, as explained here, understanding that in C++ "2D" is just a metaphor, the way to interpret the same pointer: http://www.cplusplus.com/doc/tutorial/arrays[^].

Also note that the term "2D", "3D" and so one is nothing but some jargon. Mathematically correct work is "rank". In the strict mathematical terminology, all those finite sets are always zero-dimensional objects. Only continuums can have such property as "dimension". From elementary linear algebra and analytical geometry, you can learn that continuous linear spaces can have some integer dimensions. And the geometrical object can have dimensions of 1, 2, 3 and more if they are the manifolds in some continuous spaces; for other geometrical objects, the dimension can be fractional or at all undefined. But this is a different story…

—SA


There is a C++ way for that.
The ostringstream class[^] provides, for strings, the same interface provided by cout for standard output.

Try, for instance:

 #include <iostream>
 #include <string>
 #include <sstream>
 #include <vector>
 using namespace std;

int main()
{
  string buff = "Hello";

  vector < string > v;

  for (size_t n=0; n < buff.size(); ++n)
  {
    ostringstream oss;
    oss << "0x" << hex << (int) buff[n];
    v.push_back( oss.str() );
  }

  for (size_t n=0; n<v.size(); ++n)
  {
    cout << v[n] << endl;
  }
}



Or, with an updated compiler

 #include <iostream>
 #include <string>
 #include <sstream>
 #include <vector>
 using namespace std;

int main()
{
  string buff = "Hello";

  vector < string > v;

  for (auto c: buff)
  {
    ostringstream oss;
    oss << "0x" << hex << static_cast<int>(c);
    v.push_back( oss.str() );
  }

  for (auto s: v)
    cout << s << endl;
}


这篇关于如何将char转换为十六进制然后将其存储到变量?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆