2

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). 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. 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.

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

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. Just that.

Artjom B.
  • 61,146
  • 24
  • 125
  • 222
F.Webber
  • 195
  • 8
  • 1
    You will have to show at least something of an effort, as it's almost impossible to answer all the possible things that you MAY have done wrong, and this is not a site for "guess what I'm trying to do, and give advice that may or may not be right"... – Mats Petersson Jul 17 '13 at 23:18
  • Its a binary file, I mean, its a .txt, but opening it doesn't show anything but 2 weird ASCII characters, so its their hex values what really matters. – F.Webber Jul 17 '13 at 23:19
  • Sorry that comment was a response to a previous one. I do have tried a lot to solve this issue, and Im just baffled I can't do something as simple as it is. Im going to update my main post to explain a littble better. – F.Webber Jul 17 '13 at 23:21
  • You want to "interpret them as hex and convert to decimal"? Whenever I see those words I immediately assume you don't know what your talking about. (And your question clarifies, that yes, you have no idea what you're talking about). **The file has no hex, and you do _not_ want to convert it to decimal**. – Mooing Duck Jul 17 '13 at 23:37
  • Thanks for your help. Sorry but I think it does explain what I wanted, at least with poor english haha. Look, whenever I scan I file in binary mode, the characters are taken in ASCII. If I get across a 0x20 character, it scans a space, pretty logical. What I wanted to do was to take the actual hex value, that is, 20, and convert it to decimal, that is, 32. That's all. So scanning 0x1B and outputting 27. Nevermind though, as I've already been answered with an actual useful answer. – F.Webber Jul 17 '13 at 23:43

3 Answers3

2

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. 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. And streaming out a uint16_t will, by default, display that value in decimal. There's no hexidecimal involved. The hexidecimal you saw in the hex editor was because a hex editor interprets the bytes as hex values.

Mooing Duck
  • 64,318
  • 19
  • 100
  • 158
  • Oh I see, thanks a lot dude, sorry for expressing myself so poorly before, and making all this thing so much harder to understand than it actually is. I now tested that, and it does exactly what I wanted ;) – F.Webber Jul 17 '13 at 23:56
1

If all you want to do is print a number in hexadecimal form, use std::hex

int i = 0x1B;
std::cout << std::hex << i << std::endl;
user123
  • 8,970
  • 2
  • 31
  • 52
  • Oh, thanks a lot, thats exactly what I wanted, can't believe it was so simple, thanks :D I knew it was gonna be simple but wow. Accepted! – F.Webber Jul 17 '13 at 23:36
  • I think you set the OP off on a wild goose chase, since he describes opening the file in hex, and seeing `0x1B` and `0x00`, (where `0x00` is unprintable) – Mooing Duck Jul 17 '13 at 23:39
0
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;
}
madnut
  • 124
  • 5
  • You're checking the state of the stream in all the wrong places, which can lead to reading past the end of the file, which you ignore, and then attempt to interpret, which is undefined behavior. – Mooing Duck Jul 17 '13 at 23:48
  • @MooingDuck Its checking for EOF before every read. There is absolutely no way this code will read beyond the end of the file. – madnut Jul 18 '13 at 00:08
  • 1
    Unfortunately, that is a very common, and incorrect belief. Sadly, checking for EOF before every read does _nothing_ to prevent reading beyond the end of a file. :( http://stackoverflow.com/questions/21647/reading-from-text-file-until-eof-repeats-last-line – Mooing Duck Jul 18 '13 at 00:24
  • @MooingDuck Take that back, always get confused on the order between std::getline and this one. Hopefully ill remember after this one :) – madnut Jul 18 '13 at 01:30
  • tip: most iostream functions (including getline) are designed to work in an `if` condition or `while` loop. `get` doesn't unfortunately, so you have to [`while(c=ifs.get(), ifs) {`](http://coliru.stacked-crooked.com/view?id=690c0adc9d9fdd938458a013804c08ae-4f34a5fd633ef9f45cb08f8e23efae0a) – Mooing Duck Jul 18 '13 at 02:53