473,609 Members | 2,134 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Coversion of Binary file to ASCII file using C.

Hi,

I am able to open a binary file for reading but can someone tell me as
how to go about converting a Binary file to ASCII file using C.

Thanks In Advance
SandyMan

May 3 '06 #1
6 10031
"SandyMan" <rs*********@gm ail.com> wrote:
I am able to open a binary file for reading but can someone tell me as
how to go about converting a Binary file to ASCII file using C.


Not until you decide what you want the conversion to actually _do_. A
binary file (as opposed to a binary stream, which is the other side of
the medal, as it were) is generally supposed to consist of any possible
data, including some that has no equivalent printable text character.
One can always, of course, "convert" a binary file to text by means of a
hex dump or something similar; but is that really what you want to do,
or was there some other kind of conversion you were thinking of?

Richard
May 3 '06 #2
The input to my C program is a Binary file consisting of certain
variables and its corresponding value.All i want to do is to read the
the variable name and its associated value one by one in ASCII.

SandyMan

May 3 '06 #3
I have to do the following in C

I need to

A)Open a file (a binary file)
B) Read a field ( i know the offset and length)
C) Convert the value to Ascii /Hex
D)And then change the value of the field (which i have in Hex)
E) Convert it to Ascii
F)And put in the original file.

Till now I just know how to open a file
FILE *fopen(const char *filename, const char *mode);
like i have coeded as f=fopen("/user/binaryfile","rb ")

to read i have used
ret = fread(ptr,1,2,f );
where ptr is
byte ptr[2];

But i am not sure of how to use binary arrays how to get value from
these and also how to write data in these
which will then be used to write data in the file

I have to write a filed of length 13 eg 0604040404011

I also know the offset of the fleld

I am exploring fseek with SEEK_SET for the beginning of the file
main problem currently is handling of binary arrays (Byte array)

Here is my complete code

#include<stdio. h>

void main(){
typedef unsigned char byte;
/* Define the "byte" type name to be an unsigned char. */
byte ptr[2];
/* Declare a two byte array. */

int ret;

FILE* f; // create a new file pointer
if((f=fopen("/user/binaryfile","rb "))==NULL) { //
printf("could not open file"); // print
exit(1);
}

ret = fread(ptr,1,2,f );
printf("%d",ret );
printf("%o",*pt r);
fclose(f); // close the filepointer
}
Please tell me how to convert the binary data to Ascii/Hex data and
also back to binary

Thanks

May 3 '06 #4
"SandyMan" writes:
I have to do the following in C

I need to

A)Open a file (a binary file)
B) Read a field ( i know the offset and length)
C) Convert the value to Ascii /Hex
D)And then change the value of the field (which i have in Hex)
E) Convert it to Ascii
F)And put in the original file.

<snip>

What if someone just told you to do this, and that person (and you) didn't
even know that binary files existed? Would you have any question in that
case?

Binary files have a terribly inappropriate name and I think that might be
misleading you. What the prefix "binary" means has had tens of thousands of
bytes of discussion on this newsgroup and it is just laying there waiting
for you to read. It only has to do with the representation of the '\n'
character. An 'a' is an 'a' whether the file it is in is called an ASCII
file or or it is called a binary. ASCII is often used (wrongly) as a
synonym for "text".
May 3 '06 #5
if you have a structure dumped into a file and you are going to read
the fields of the structure ,

simplest way is declare a variable of the strutcure .. erad into the
structure and print it like you would normally print any structure ..

if you have an array of structures , put this in a loop

if you have different structures(one after the other) , then you either
know the order of the structures or you can infer the type from the
file somehow .. use point 1 again case by case

May 3 '06 #6
On 2006-05-03, SandyMan <rs*********@gm ail.com> wrote:
I have to do the following in C

I need to

A)Open a file (a binary file)
B) Read a field ( i know the offset and length)
C) Convert the value to Ascii /Hex
D)And then change the value of the field (which i have in Hex)
Why does it need to be in ascii/hex? why can't you change it in binary?
E) Convert it to Ascii
F)And put in the original file.

May 3 '06 #7

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

11
3214
by: rbt | last post by:
Is there an easy way to exclude binary files (I'm working on Windows XP) from the file list returned by os.walk()? Also, when reading files and you're unsure as to whether or not they are ascii or binary, I've always thought it safer to 'rb' on the read, is this correct... and if so, what's the reasoning behind this? Again all of this pertains to files on Windows XP and Python 2.4 Many thanks!
1
6293
by: Ferran Foz | last post by:
Hello, I'm using ADODB.Stream to open a binary file on the server and write it down to the browser using Response.BinaryWrite. It's working fine, but i need to make some changes to the binary data before it is send to the browser. I'm trying to use REPLACE, but it's not finding a string that i know it's in the binary file. Using InstrB i've found that the search inside the binary data is being done in a Unicode format, but i don't
10
9101
by: J. Campbell | last post by:
OK...I'm in the process of learning C++. In my old (non-portable) programming days, I made use of binary files a lot...not worrying about endian issues. I'm starting to understand why C++ makes it difficult to read/write an integer directly as a bit-stream to a file. However, I'm at a bit of a loss for how to do the following. So as not to obfuscate the issue, I won't show what I've been attempting ;-) What I want to do is the...
9
11406
by: Hemang Shah | last post by:
Hello fellow Coders! ok, I"m trying to write a very simple application in C#. (Yes its my first program) What I want to do is : 1) Open a binary file 2) Search this file for a particular string. 3) Close the file
12
5866
by: Adam J. Schaff | last post by:
I am writing a quick program to edit a binary file that contains file paths (amongst other things). If I look at the files in notepad, they look like: <gibberish>file//g:\pathtofile1<gibberish>file//g:\pathtofile2<gibberish> etc. I want to remove the "g:\" from the file paths. I wrote a console app that successfully reads the file and writes a duplicate of it, but fails for some reason to do the "replacing" of the "g:\". The code...
10
3637
by: joelagnel | last post by:
hi friends, i've been having this confusion for about a year, i want to know the exact difference between text and binary files. using the fwrite function in c, i wrote 2 bytes of integers in binary mode. according to me, notepad opens files and each byte of the file read, it converts that byte from ascii to its correct character and displays
5
2893
by: bwv539 | last post by:
I have to output data into a binary file, that will contain data coming from a four channel measurement instrument. Since those data have to be read from another C program somewhere else, the reading program must know how many channels have been acquired, date, time, and so on. I mean that the position of each datum is not fixed in the file but depends on the conditions when acquired. That is, I need something like a header in the file to...
1
2596
by: Vic | last post by:
I have a c program which writes mac address entries onto a text file. Text file when opened in vim looks like this index mac 1 ^@^@^Q^@^@^A ascii of (00.00.11.00.00.01) 2 ^@^@^Q^@^@^B ascii of (00.00.11.00.00.02) .. ........ Likewise I read the mac entries from the text file using a perl script and do some processing
5
11245
by: dm3281 | last post by:
Hello, I have a text report from a mainframe that I need to parse. The report has about a 2580 byte header that contains binary information (garbage for the most part); although there are a couple areas that have ASCII text that I need to extract. At the end of the 2580 bytes, I can read the report like a standard text file. It should have CR/LF at the end of each line. What is the best way for me to read this report using C#. It is...
0
8127
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
8567
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...
1
8215
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
8398
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
4015
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...
0
4076
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
2529
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
1
1658
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
1380
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.