How to read a Hexadecimal file ?

V

Vijay

Hi ,

I am doing a small project in c.

I have a Hexadecimal file and want to convert into ascii value.
(i.e., Hexadecimal to Ascii conversion from a file).

Could anyone help me?

Thanks in adv.
Vijay.
 
R

Richard Heathfield

Vijay said:
Hi ,

I am doing a small project in c.

I have a Hexadecimal file and want to convert into ascii value.
(i.e., Hexadecimal to Ascii conversion from a file).

"Hexadecimal file" doesn't mean a lot, but I'm guessing - and it's just a
guess - that you have a file containing arbitrary data and you would like
to represent that file in a readable way - what we call a "hex dump" (as if
hex were readable).

This is simple enough, but can easily be made more complicated if you don't
think it has enough features. To simplify the explanation, I have assumed
that there are 8 bits in a byte. Beware! This is not necessarily the case.
But it is probably the case on the system you are using.

Define a counter, and set it to value 0.
Define an array of char, and initialise it to "0123456789ABCDEF".
Define an int, which I will call ch.

Using ch, capture each value in turn from the file. For as long as that
capture is successful, do the following:

1) increment your counter
2) display a space character
3) display the ((ch & 0xF0) >> 4)th character of your array
4) display the (ch & 0xF)th character of your array
5) if the counter is equal to your maximum desired page-width:
5a) display a newline character
5b) set your counter to 0
6) back to the top of the loop

Once the capture fails, test (using ferror()) to see whether it actually
failed, or whether it simply hit the end of the file. If it is the end of
the file:
if your counter is not equal to 0, display a newline character
otherwise:
display an appropriate error message.

Finally, terminate the program.
 
J

J. J. Farrell

Vijay said:
I am doing a small project in c.

I have a Hexadecimal file and want to convert into ascii value.
(i.e., Hexadecimal to Ascii conversion from a file).

"Hexadecimal file" is not well-defined. You need to tell us exactly
what you mean by that term. We could guess, but there are several
sensible possible meanings, so guessing is likely to cause confusion.

You also need to tell us how you want it to be converted. What do we
read out of the "hexadecimal file", and how do we convert it to ASCII?

If you specify precisely what you want to do, we can help you write
some C code to do it.
 
R

Richard

Vijay said:
Hi,

Sample Hexadecimal data is given below.

Hi Vijay : go and do some reading. That is NOT hexadeciaml data
below. It is just data represented in ascii. Not the same at all.

I think that another poster (Heathfield I think) mentioned that you
might be looking for a program to display any program as a hex dump. Use
google there are loads of resources for this.

good luck!

r.
 
K

Keith Thompson

Richard said:
Hi Vijay : go and do some reading. That is NOT hexadeciaml data
below. It is just data represented in ascii. Not the same at all.

No, it's not data represented in ASCII. It appears to be just a
binary file, neither ASCII nor hexadecimal.

To the original poster: You need to understand a few things. All
files are "binary", in the sense that they're made of bits; on most
systems you're likely to use, they're composed of 8-bit bytes. (The C
standard requires a byte to be *at least* 8 bits, but you're not
likely to run into a system with bytes larger than 8 bits unless you
work with digital signal processors, and I don't think they have file
systems.)

A given file may or may not contain some specific kind of data. If
the bytes are all ASCII characters (remember, ASCII is a 7-bit
character set), it's (presumably) an ASCII file. There are 8-bit
character sets that are extensions of ASCII. There are also character
sets not based on ASCII; EBCDIC is the most common, but you're
unlikely to run into that unless you work with IBM mainframes.

Hexadecimal is a way of representing numbers as text, using the digits
'0' to '9' and the letters 'a' to 'f' or 'A' to 'F'. You could have a
"hexadecimal file" that contains just those (ASCII) characters
(possibly with spaces and newlines added for formatting) -- but that's
not what you showed us. Hexadecimal can be a convenient way of
displaying binary data, but it's a *display* format; the binary file
is not hexadecimal.

You said you want to decode it to ASCII format. There are multitudes
of ways you could do that, generating a sequence of ASCII characters
that specify the content of the binary file. (The ASCII
representation is likely to be larger than the original binary file;
if you represent it as hexadecimal digits, it will be twice the size,
one 8-bit character for each 4-digit hexadecimal digit.)

I could guess what you're trying to do, but I'm not going to. You
need to define the problem. You have a binary file as input (BTW,
please don't post binary files here), and you want some kind of ASCII
output, but you haven't said *how* the ASCII should correspond to the
binary data.

But even if you define the problem, you may still be asking in the
wrong place. You mention that you want to do this in C, but you
haven't shown any indication that you've tried to do it yourself.

First you need to define the problem. Then you need to make some
effort to solve it yourself. If you have some partially working code
and you can't figure out why it's not working, post it here and we can
offer advice. We won't do it for you.

Unless, of course, this is a homework assignment; in that case, just
give us your instructor's e-mail address so we can submit our
solutions directly. It's up to you whether we mention your name or
not.
 
R

Richard

Keith Thompson said:
No, it's not data represented in ASCII. It appears to be just a
binary file, neither ASCII nor hexadecimal.

Converted by my newsreader. But yes, I saw he just pasted raw data
in. For sure a homework assignment so hence the "google is that way"
followup and the mention of him needing to hex dump.
 
K

Keith Thompson

Richard said:
Converted by my newsreader. But yes, I saw he just pasted raw data
in. For sure a homework assignment so hence the "google is that way"
followup and the mention of him needing to hex dump.

Ah. My newsreader displayed it as a bunch of garbage characters
(presumably as ISO-8859-1), which reinforces the idea that posting
binary data is a bad idea. (There are methods for posting binary
files as attachments, but that's inappropriate in a discussion group
like this one.)
 
J

J. J. Farrell

Top-posting corrected.
Sample Hexadecimal data is given below.

‹ ÿCF0000.DAT
ìÏ;ŽÂ0ÐûlÚ$"2"0mDƒh€ÔHh*60Å,`4Ålš}
...

Could u give some codes in c to decode.. (i.e, in ascii format).

You appear to have a file of what is usually referred to as "binary"
data. The file contains a number of bytes. Each byte contains a certain
number of bits, most likely 8 (but that depends on the computer you are
working on). If we assume 8 bits and consider these bytes as unsigned
numbers, each byte in the file contains a number in the range 0 to 255.

You need to read each byte in, then apply whatever conversion algorithm
you require to turn the value in that byte into ASCII, then write out
the result.

For example, your algorithm might be

if the value of the input byte is 3
write out the ASCII character 'x'
else if the value of the input byte is 179
write out the ASCII character 'y'
else
write out the ASCII character 'z'
fi

Another possibility is that for each byte you read in, you would write
out three ASCII digits giving the value of the byte in decimal. There's
a very large number of other possible ways of converting the file to
ASCII.

It's not likely that either of these is the algorithm you want. You
need to understand what conversion algorithm you want to implement, and
tell us what it is. Then we'll be able to guide you in writing a C
program to implement it.
 
J

Joe Wright

Keith said:
Ah. My newsreader displayed it as a bunch of garbage characters
(presumably as ISO-8859-1), which reinforces the idea that posting
binary data is a bad idea. (There are methods for posting binary
files as attachments, but that's inappropriate in a discussion group
like this one.)
Not to disagree about anything, this is a convenient place to jump in..

1. All files are binary!
2. All files are product of the creator of the file!
3. We cannot know, by inspection only, how to interpret the file!

There is general agreement among some of us the format of the 'ASCII
text file'. "Zero or more 'lines' of zero or more ASCII characters, each
line terminated with a newline character." This is the only 'text' file.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,581
Members
45,055
Latest member
SlimSparkKetoACVReview

Latest Threads

Top