Handling binary data in C - questions

B

Bruce Lee

Hi
I have the following 2 questions:
1.How to write binary data to a text file in C? I have a number like
10001010110001 say, I need to put it in the text file so that I can see
the corresponding characters when I open the file.

2. How to extract bits from a byte. I think it is using bit operations,
but for example, how would I extract the 2nd most significant bit in a
byte?

Thanks
Bruce
 
N

Neil Kurzman

Bruce said:
Hi
I have the following 2 questions:
1.How to write binary data to a text file in C? I have a number like
10001010110001 say, I need to put it in the text file so that I can see
the corresponding characters when I open the file.

2. How to extract bits from a byte. I think it is using bit operations,
but for example, how would I extract the 2nd most significant bit in a
byte?

Thanks
Bruce

1. mask a bit print a 0 or 1 , shift left and repeat.

2. Mask using and (&)

ex :
if(abyte & 0x04)printf("1"); // mask bit 2 0x04 = 00000100
else printf("0");
 
B

Bruce Lee

Sorry but I am still new to C and unable to understand you clearly

1. How to print it? fprintf(fileNum,"%s","write this"); is what I know.
And I guess %s can be %c as well.
But how to print a bit as you said (0 or 1)? What is the command to
print just 0s and 1s to a file?

2. This masks the 3rd bit from the right since 0x04 is 100? Does &-ing
give a boolean result or an int? In this case, what does abyte & 00x4
return?

Thanks for the explanation
Bruce
 
B

Bruce Lee

Stan said:
try this:

fprintf(stdout,"%c", data & 0x4 ? '1' : '0');

This will print out a '1' if the 3rd bit is on and '0' if not. It will
be printed to your terminal. If you want the result to go to a
different file just substitue your file pointer in place of stdout.


--
Regards,
Stan Milam
=============================================================
Charter Member of The Society for Mediocre Guitar Playing on
Expensive Instruments, Ltd.
=============================================================

Thanks, but data & 0x4 need not necessarily give a 0 or 1 right? I
mean, if data is 11011000 & 0x4, the result would be 0010000
How do I write the bits to a file using fprintf (or anything else)?
If my character is a, its ascii value will be in bits 1100001. I have
this and I need to write it to the file so that the file has the char
'a'.
Could someone please explain this bit extraction and writing?
Thanks
Bruce
 
G

GrandElf

maybe you don't need bit operations.please look at the simple code
fragment:
void main()
{
FILE *fp;
fp = fopen("d:\\a.txt","w+");
printf("---------------\n");
if(fp==NULL)
{
printf("fail to open file \n");
return;
}
fprintf(fp,"%c",'a');
fclose(fp);

return;
}
the program is complied in TC20,it only do create a file "a.txt" on
d:\.The content of a.txt
is 'a'.
 
I

infobahn

GrandElf said:
maybe you don't need bit operations.please look at the simple code
fragment:
1

void main()

2, 3
{
FILE *fp;
4

fp = fopen("d:\\a.txt","w+");
5

printf("---------------\n");
if(fp==NULL)
{
printf("fail to open file \n");
6


7


8


9

}

A: Do some research, to find out why your code is broken (the numbers
should give you a few hints as to where the problems are).

B: Fix those problems.

C: Try re-posting if you wish. Of course, the OP might have got bored
of waiting by then.
 
R

Richard Bos

GrandElf said:
maybe you don't need bit operations.please look at the simple code
fragment:
void main()
{
FILE *fp;
fp = fopen("d:\\a.txt","w+");
printf("---------------\n");
if(fp==NULL)
{
printf("fail to open file \n");
return;
}
fprintf(fp,"%c",'a');
fclose(fp);

return;
}

And how does this non-conforming, badly indented (learn to use Google
Broken Beta correctly or get a real newsreader!) code, with a
system-specific filename, help even the slightest bit with the OP's
problem, which is outputting the binary representation of an object?
Even if we assume a single interpretation of "binary representation"
(there are two reasonable ones), your code does not reliably output
either of those for all objects.

Richard
 

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,578
Members
45,052
Latest member
LucyCarper

Latest Threads

Top