473,900 Members | 3,343 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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 18677
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).
"Hexadecima l 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 "0123456789ABCD EF".
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).
"Hexadecima l 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 "hexadecima l 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.comwrite s:
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"0m Dƒh€ÔHh*60 Å,`4Ålš}
V1+¡¢36*ø ÜÓøûìû ì¤H%\g#ÀF z*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).

"Hexadecima l 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 "hexadecima l 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.c omwrites:
"Vijay" <Vi************ @gmail.comwrite s:
>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
"hexadecima l 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_Keit h) 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.orgw rites:
Richard <rg****@gmail.c omwrites:
>"Vijay" <Vi************ @gmail.comwrite s:
>>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.c omwrites:
Keith Thompson <ks***@mib.orgw rites:
>Richard <rg****@gmail.c omwrites:
>>"Vijay" <Vi************ @gmail.comwrite s:
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_Keit h) 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).
"Hexadecima l 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 "hexadecima l 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*6 0,`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.c omwrites:
>Keith Thompson <ks***@mib.orgw rites:
>>Richard <rg****@gmail.c omwrites:
"Vijay" <Vi************ @gmail.comwrite s:
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
15521
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 write in the hex file... can anybody help me 'cause i don't know how python write an hex file! Thanks!
0
1845
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. Using VB2005 I created a string variable that could recreate the file with values input be the user. I can get the text with formating to work perfectly, but I have not had any luck with the graphics. As best I can tell the graphic is saved as a...
5
2932
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 shift 24 bits storing in result then printing the result. I thing a while or for loop is needed but I'm not quite sure how to go about it. How do I step through each character in this case and store it for use and passing to another function. ...
3
2463
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? I am trying to read the hexadecimal numbers and trying to write this to a buffer in binary format. To do so, I am reading the numbers in a string format and then converting the string (ASCII) to hex and write it in binary format in the buffer.
5
7919
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 Damon
1
4198
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. The exception reads: The '' character, hexadecimal value 0x80, cannot begin with a name. Line 1, position 2. Any suggestions? I read on Microsoft's website about writing surrogate pairs, but I can't find any documentation that confirms the
7
19248
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 00000000000000001111111111111111 11111111111111111111111111111111 00000000000000000000000000000000 11111111111111110000000000000000
0
20676
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 functionality at the expense of slightly greater code complexity. This self-contained routine can be pasted into a code module and called from anywhere, including the immediate window. It will expect you to pass the name of a file (including the path if...
6
16926
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
9845
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
11276
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10866
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
10976
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
9671
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development projectplanning, coding, testing, and deploymentwithout human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
8043
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupr who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
7204
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5891
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
2
4301
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.