Hex n' junk

This is a discussion on Hex n' junk within the C++ Programming forums, part of the General Programming Boards category; Hi
I know there's been loads of threads about this, but I searched for like an hour straight, an' couldn't ...

Hex n' junk

Hi

I know there's been loads of threads about this, but I searched for like an hour straight, an' couldn't find anythin'.

Anyway, I have a txt document saved in C: . It consists of many hex values, all strung together. I wish to simply read the file, take the first 14 characters, ( or use 14 characters to the middle of the document ) change them to ascii values, and store them to a string.

not possible if there is nothing separating the hex values, unless you know that they were stored as pairs of hex values, ffff is really ff and ff. In that case it is pretty simple. read the first 14 characters into a buffer and convert them in memory.

Code:

char buf[] = "fc";
int a;
sprintf(buf,"%x",&a);

You might be able to do it with stringstream c++ class, but I'm not proficient enough to show you how.

You can read in different numbers from streams and stuff after you include a header called iomanip In that header is a function called setbase that allows you to change between number systems. You can still store them in normal variables too.
It works something like this:

A buffer is simply temporary storage. In this case, it's a Character Array (C-style string).

You might find a hex editor helpful. I use XVI32 (FREE !!!). A hex editor will allow you to "look at" (and optionally edit) the data in any file. Most hex editors will also show the ASCII characters for any value that's in the ASCII range (i.e. 32 - 126). So, you can easily see if your program is altering the file as you expect.

...change them to ascii values, and store them to a string.

You don't really change the value. You display the value's associated ASCII character. If you read-in 41 hex, that's an ASCII 'A'. If you if you increment it to 42 hex, it now represents an ASCII 'B'.

Everything in the file (and your computer's memory) is a binary numerical value. That "number" might represent an ASCII character, the numerical value, or something else. You can display that number in decimal, octal, hex, as an ASCII character, or even in binary with a little more work.

You do need to read the data in as type char, or else you will get more than one character packed-into each integer.