简体   繁体   English

以二进制模式扫描文件并将十六进制字符解释为C ++中的实际数字

[英]Scanning a file in binary mode and interpret the hex characters as actual numbers in C++

I know this title might sound confusing. 我知道这个标题可能听起来令人困惑。 I have a simple question which I haven't been able to solve yet. 我有一个简单的问题,但我还没有解决。

Lets imagine I have a file, opening it with an Hex Editor shows it has two characters inside, say 0x1B and 0x00 (obviously unprintable). 让我们想象一下我有一个文件,用十六进制编辑器打开它显示它里面有两个字符,比如说0x1B0x00 (显然是不可打印的)。 I'd like to take that as 1B00 in HEX , which is 6912 in DEC , as opposed to directly converting the characters which would be wrong and is what all other questions I saw asked. 我希望把这个当作是1B00 十六进制 ,这是在6912 DEC,而不是直接转换这将是错误的,是所有其他的问题,我看到问的字符。 Well, thats the task I want to do here. 好吧,这就是我想在这里做的任务。 Seems simple, but everything I've tried just does it wrong! 看似简单,但我尝试的一切都做错了! Even though I am obviously opening the file in binary mode. 即使我显然在二进制模式下打开文件。

I have only managed to read the characters individually, and mess around a bit, but never do what I actually want, which is as simple as taking those 2 hex characters, interpreting them as an Hex Number, and then convert it to Decimal. 我只是设法单独读取字符,乱七八糟,但从来没有做我真正想要的,这就像取这些2个十六进制字符一样简单,将它们解释为十六进制数,然后将其转换为十进制。

Sorry for any unclear idea, Im not a native speaker. 对不起任何不明确的想法,我不是母语人士。 Any help will be appreciated, Im sure you'll think this was quite a noobish question :P 任何帮助将不胜感激,我相信你会认为这是一个非常无聊的问题:P

EDIT: Sorry, apparently I didn't explain myself properly. 编辑:对不起,显然我没有正确解释自己。 I know this might seem abstract, but it is a really concrete little thing which I have struggled to get solved, yet I haven't been able. 我知道这看起来很抽象,但这是一个非常具体的小事,我一直在努力解决,但我还没有。 Maybe I can ask it another way: 也许我可以用另一种方式问:

How can I scan a character in binary mode, lets say 0x1B , and convert that to actual 1B characters. 如何在二进制模式下扫描字符,比如说0x1B ,并将其转换为实际的1B字符。 Just that. 只是。

Sounds like you want to read the file as raw data, and then display it on the screen in decimal? 听起来你想将文件作为原始数据读取,然后以十进制显示在屏幕上? Super easy! 超级简单!

int main() {
    std::ifstream myfile("filename.data", std::ofstream::binary);
    uint16_t number;
    char* buffer = (char*)(&number);
    while(myfile.read(buffer, sizeof(number))) {
        std::cout << number << ' ';
    }
}

The reason it's so easy is that there's no hexidecimal involved. 它如此简单的原因是没有涉及十六进制。 The file is saved as a series of bytes, each byte holds one of 256 values. 该文件保存为一系列字节,每个字节包含256个值中的一个。 They aren't hex, they're just a series of values. 它们不是十六进制,它们只是一系列价值观。 If you read two bytes into the uint16_t , that is the easiest way to interpret two bytes as a single unsigned 2 byte value. 如果在uint16_t读取两个字节,这是将两个字节解释为单个无符号2字节值的最简单方法。 And streaming out a uint16_t will, by default, display that value in decimal. 默认情况下, uint16_t将以十进制显示该值。 There's no hexidecimal involved. 没有涉及十六进制。 The hexidecimal you saw in the hex editor was because a hex editor interprets the bytes as hex values . 您在十六进制编辑器中看到的十六进制是因为十六进制编辑器将字节解释为十六进制值

If all you want to do is print a number in hexadecimal form, use std::hex 如果您只想以十六进制格式打印数字,请使用std::hex

int i = 0x1B;
std::cout << std::hex << i << std::endl;
std::ifstream infile("test.bin", std::ofstream::binary);

while (true) 
{
    char c1 = ifs.get();
    if (!infile.good())
    {
        break;
    }

    char c2 = ifs.get();
    if (!infile.good())
    {
        break;
    }

    int num = (int)c1 |((int)c2 << 8);

    // if you need the oppisite order then
    // int num = (int)c2 &((int)c1 << 8);
    cout << num;
}

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

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