C ++六角形解析

编程入门 行业动态 更新时间:2024-10-24 06:30:03
本文介绍了C ++六角形解析的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

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

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

我需要做的一个例子是这样的网站 home2.paulschou/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 home2.paulschou/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 ++六角形解析

本文发布于:2023-11-30 02:39:38,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1648436.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!