468,462 Members | 1,809 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 468,462 developers. It's quick & easy.

How to read a Hexadecimal file ?

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.

Aug 7 '06 #1
8 17702
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.

--
Richard Heathfield
"Usenet is a strange place" - dmr 29/7/1999
http://www.cpax.org.uk
email: rjh at above domain (but drop the www, obviously)
Aug 7 '06 #2

Vijay wrote:
>
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.

Aug 7 '06 #3
"Vijay" <Vi************@gmail.comwrites:
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.
>
‹ ÿCF0000.DAT
ìÏ;ŽÂ0ÐûlÚ$”2"0mDƒh€ÔHh*60Å,` 4Ålš}
V1+¡¢36*øˆÜÓøûìûì¤H%\g#ÀFz*QA -׺
ó‹Ä`Ÿ—ó#ÐÜ·{A±î(äçë:L¬þŒ‡ Ay}fâ‡Ê¶
ö“¼0UzXÕE‚Åì
Œê›qá'ÿ*K0®žt£è·Vm{–éAyº 4*Ÿ\ðU~C¦Í»nå@ì"šèM#/ûìŽï‹u*¨ù¯§:nÓ5iˆˆˆˆˆˆˆˆ ˆˆˆˆˆˆÞ¹

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

J. J. Farrell wrote:
>Vijay wrote:
>
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.
--
Lint early. Lint often.
Aug 7 '06 #4
Richard <rg****@gmail.comwrites:
"Vijay" <Vi************@gmail.comwrites:
>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.
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.

--
Keith Thompson (The_Other_Keith) ks***@mib.org <http://www.ghoti.net/~kst>
San Diego Supercomputer Center <* <http://users.sdsc.edu/~kst>
We must do something. This is something. Therefore, we must do this.
Aug 7 '06 #5
Keith Thompson <ks***@mib.orgwrites:
Richard <rg****@gmail.comwrites:
>"Vijay" <Vi************@gmail.comwrites:
>>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.

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.
Aug 7 '06 #6
Richard <rg****@gmail.comwrites:
Keith Thompson <ks***@mib.orgwrites:
>Richard <rg****@gmail.comwrites:
>>"Vijay" <Vi************@gmail.comwrites:
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.

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

--
Keith Thompson (The_Other_Keith) ks***@mib.org <http://www.ghoti.net/~kst>
San Diego Supercomputer Center <* <http://users.sdsc.edu/~kst>
We must do something. This is something. Therefore, we must do this.
Aug 7 '06 #7
Vijay wrote:
>
Top-posting corrected.
J. J. Farrell wrote:
Vijay wrote:
>
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.

Sample Hexadecimal data is given below.

 CF0000.DAT
;0l$"2"0mDhHh*60,`4l}
...

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.

Aug 7 '06 #8
Keith Thompson wrote:
Richard <rg****@gmail.comwrites:
>Keith Thompson <ks***@mib.orgwrites:
>>Richard <rg****@gmail.comwrites:
"Vijay" <Vi************@gmail.comwrites:
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.
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.

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.

--
Joe Wright
"Everything should be made as simple as possible, but not simpler."
--- Albert Einstein ---
Aug 7 '06 #9

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

3 posts views Thread by Cesar Andres Roldan Garcia | last post: by
reply views Thread by thomasp | last post: by
5 posts views Thread by Pete | last post: by
3 posts views Thread by Shivani | last post: by
5 posts views Thread by Damon | last post: by
7 posts views Thread by elliotng.ee | last post: by
6 posts views Thread by sweeet_addiction16 | last post: by
reply views Thread by NPC403 | last post: by
reply views Thread by kmladenovski | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.