473,231 Members | 1,497 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,231 software developers and data experts.

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 18605
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
by: Cesar Andres Roldan Garcia | last post by:
Hi I'm trying to write an hexadecimal file... I mean not a text plain... I have to convert a float decimal number in float hexadecimal one, and that's done. That number is the one I'm gonna...
0
by: thomasp | last post by:
I have not had any luck with this on other groups so I am posting it here. I created a .rtf file using MS Word that included text and graphics(.png file) . I then viewed this file with notepad. ...
5
by: Pete | last post by:
I having a problem reading all characters from a file. What I'm trying to do is open a file with "for now" a 32bit hex value 0x8FB4902F which I want to && with a mask 0xFF000000 then >> right...
3
by: Shivani | last post by:
Hi, I am looking for a utility that reads hexadecimal numbers from a file into a string. The numbers in the file are seperated by "," as delimeter. Is there a standard C utility that can do that?...
5
by: Damon | last post by:
I'm getting '', hexadecimal value 0x02, is an invalid character when I'm deseralizing XML from a 3rd party XML gateway. How do I get rid of these hexadecimal values before I deserialize? Cheers...
1
by: Jordan | last post by:
I have a unicode XML file that I am trying to read using the .NET XmlTextReader in C#. How do I read the unicode file? If I try to using the XmlTextReader.Read() method, it throws an exception. ...
7
by: elliotng.ee | last post by:
I have a text file that contains a header 32-bit binary. For example, the text file could be: %%This is the input text %%test.txt Date: Tue Dec 26 14:03:35 2006...
0
by: Killer42 | last post by:
Here is a very simple example routine which reads a file from disk, in one big lump. This uses the built-in VB statements only. Later we will cover the FileSystemObject, which provides greater...
6
by: sweeet_addiction16 | last post by:
hello Im writin a code in c... can sum1 pls help me out in writing a c code to convert decimalnumber to hexadecimal number.The hexadecimal number generated has to be an unsigned long.
0
by: VivesProcSPL | last post by:
Obviously, one of the original purposes of SQL is to make data query processing easy. The language uses many English-like terms and syntax in an effort to make it easy to learn, particularly for...
3
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 3 Jan 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). For other local times, please check World Time Buddy In...
0
by: jianzs | last post by:
Introduction Cloud-native applications are conventionally identified as those designed and nurtured on cloud infrastructure. Such applications, rooted in cloud technologies, skillfully benefit from...
0
by: abbasky | last post by:
### Vandf component communication method one: data sharing ​ Vandf components can achieve data exchange through data sharing, state sharing, events, and other methods. Vandf's data exchange method...
0
Git
by: egorbl4 | last post by:
Скачал я git, хотел начать настройку, а там вылезло вот это Что это? Что мне с этим делать? ...
1
by: davi5007 | last post by:
Hi, Basically, I am trying to automate a field named TraceabilityNo into a web page from an access form. I've got the serial held in the variable strSearchString. How can I get this into the...
0
by: MeoLessi9 | last post by:
I have VirtualBox installed on Windows 11 and now I would like to install Kali on a virtual machine. However, on the official website, I see two options: "Installer images" and "Virtual machines"....
0
by: Aftab Ahmad | last post by:
Hello Experts! I have written a code in MS Access for a cmd called "WhatsApp Message" to open WhatsApp using that very code but the problem is that it gives a popup message everytime I clicked on...
0
by: Aftab Ahmad | last post by:
So, I have written a code for a cmd called "Send WhatsApp Message" to open and send WhatsApp messaage. The code is given below. Dim IE As Object Set IE =...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.