C ++六角形解析 [英] C++ Hex Parsing

查看:157
本文介绍了C ++六角形解析的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想知道如何将十六进制字符串转换为人类可读的字符串(如果这有任何意义),这将是我第一次真正遇到十六进制值,所以我仍然在学习他们和如何管理它们。 / p>

我有一个程序从包含原始数据包数据(十六进制)的文件读取数据,我需要解析这些信息,以便人类可读。



我需要做的一个例子是这样的网站 http://home2.paulschou.net/tools/xlate/ 其中可以放入十六进制并将其转换为文本。

解决方案

获取包含给定数字的十六进制表示的字符串的C ++ - ish方法是使用流的 hex 修饰符,如本示例:

  const int i = 0xdeadbeef; 
cout<< 0x< hex<< i<< endl; // prints0xdeadbeef

您可以对字符串流使用相同的修饰符,在字符串变量中的十六进制表示:

  const int i = 0xdeadc0de; 
ostringstream stream;
stream<< 0x< hex<<一世;

const string s = stream.str(); // s now contains0xdeadc0de

UPDATE:

如果您的输入数据是以包含字符串字符的十六进制表示形式的字符串形式给出的,则需要知道输入字符串的编码才能正确显示。在最简单的情况下,字符串就像ASCII,它将一个字节映射到一个字符。因此,在给定输入414243中,每两个字符(41,42,43)映射到映射到字符(A,B)的ASCII值(65,66,67) ,C)。



以下是C ++中的操作方法:

  const string hexData =414243; 

assert(hexData.size()%2 == 0);

ostringstream asciiStream;
istringstream hexDataStream hexData);
vector< char> buf(3); //两个字符为十六进制字符,一个用于尾随零
while(hexDataStream.good()){
hexDataStream.get & buf [0],buf.size());
if(hexDataStream.good()){
asciiStream<< static_cast< char>(std :: strtol(& buf [ 0],0,16));
}
}

const string asciiData = asciiStream.str(); // asciiData ==ABC

使用 std :: strtol ; cstdlib> 使这很容易;如果你坚持使用模板类,使用std :: stringstream执行单个子字符串(如41)到十进制值的转换)。


I'm wondering how to convert a hex string to a human readable string (if that makes any sense) this would be my first real encounter with hex values so I'm still learning about them and how to manage them.

I have a program which is reading in data from a file which contains raw packet data (hex) and I need to parse this information so it's human readable.

An example of what I need to do is something like this site does http://home2.paulschou.net/tools/xlate/ where you can put in hex and have it converted to text.

解决方案

The C++-ish way to get a string containing the hexadecimal representation of a given number is to use the hex modifier for streams, as in this example:

const int i = 0xdeadbeef;
cout << "0x" << hex << i << endl; // prints "0xdeadbeef"

You can use the same modifier on string streams in case you need to have the hexadecimal representation in a string variable:

const int i = 0xdeadc0de;
ostringstream stream;
stream << "0x" << hex << i;

const string s = stream.str(); // s now contains "0xdeadc0de"

UPDATE:

If your input data is given as a string containing the hexadecimal representation of the characters of a string, you will need to know the encoding of the input string in order to display it correctly. In the simplest case, the string is something like ASCII which maps one byte to one character. So in a given input "414243", every two characters ("41", "42", "43) map to an ASCII value (65, 66, 67), which map to a character ("A", "B", "C").

Here's how to that in C++:

const string hexData = "414243";

assert( hexData.size() % 2 == 0 );

ostringstream asciiStream;
istringstream hexDataStream( hexData );
vector<char> buf( 3 ); // two chars for the hex char, one for trailing zero
while ( hexDataStream.good() ) {
    hexDataStream.get( &buf[0], buf.size() );
    if ( hexDataStream.good() ) {
        asciiStream << static_cast<char>( std::strtol( &buf[0], 0, 16 ) );
    }
}

const string asciiData = asciiStream.str(); // asciiData == "ABC"

Using std::strtol from <cstdlib> makes this easy; if you insist on using a template class for this, use std::stringstream to perform the conversion of the single sub strings (like "41") to decimal values (65).

这篇关于C ++六角形解析的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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