473,386 Members | 1,654 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,386 software developers and data experts.

read string and convert to bits?

Hi
I have a txt file full of words and numbers
for example:
-------------------------------------------------
`smash the stack` [C programming] n. On many C implementations
it is possible to corrupt the execution stack by writing past
the end of an array declared auto in a routine. Code that does
this is said to smash the stack, and can cause return from the
outine to jump to a random address. This can produce some of
-------------------------------------------------
i need to read the txt file char by char and convert it into bits(i.e 10100101010).
how can I do that?
Apr 23 '07 #1
6 7153
gpraghuram
1,275 Expert 1GB
HI,
1)Open the input file
2)use fgetc to a single character
3)sprintf it to a string using %d to get the ascii value
4)convert the bit to binary value
5)print that to another file

Hope this is what u want....

Thanks
Raghuram
Apr 23 '07 #2
HI,
1)Open the input file
2)use fgetc to a single character
3)sprintf it to a string using %d to get the ascii value
4)convert the bit to binary value
5)print that to another file

Hope this is what u want....

Thanks
Raghuram
hi
thanks for the quick reply
fgetc seem to use the FILE class
for the simplicity of my code I have used ofstream. is there a way to convert from ofstream to FILE?

thanks in advnaced
Apr 23 '07 #3
gpraghuram
1,275 Expert 1GB
Hi,
FILE is a C Library and ofstream u have used is C++
Check for a function from the class u have used to read a single character.
Check this page for refernce
Thanks
Raghuram
Apr 23 '07 #4
AdrianH
1,251 Expert 1GB
HI,
1)Open the input file
2)use fgetc to a single character
3)sprintf it to a string using %d to get the ascii value
4)convert the bit to binary value
5)print that to another file

Hope this is what u want....

Thanks
Raghuram
You can do this with streams as well. Use istream::operator >>(char) and ostream::operator <<(char). Using a file in this way is not necessarily efficient though and is not portable.

A better way would be to read in the binary string, and know the length from it. Then use the left shift operator to convert.

I.e convert an 8 bit string in pseudo code (approximate, not been tested):

Expand|Select|Wrap|Line Numbers
  1. bitstring = "00000000"
  2. value=0
  3. for i=0 to 7
  4.   value |= (bitstring[i] == '1')
  5. end for
Hope this helps.


Adrian
Apr 23 '07 #5
AdrianH
1,251 Expert 1GB
Expand|Select|Wrap|Line Numbers
  1. bitstring = "00000000"
  2. value=0
  3. for i=0 to 7
  4.   value |= (bitstring[i] == '1')
  5. end for
Err, it should read:

Expand|Select|Wrap|Line Numbers
  1. bitstring = "00000000"
  2. value=0
  3. for i=0 to 7
  4.   value |= ((bitstring[i] == '1') << i)
  5. end for
Note that this is fairly simplistic and can be done in a variety of different ways.

Adrian
Apr 23 '07 #6
blinktude do you still need "help"? Message me if you do...this is very interesting...

-Ari
Apr 28 '07 #7

Sign in to post your reply or Sign up for a free account.

Similar topics

2
by: Dmitri Shvetsov | last post by:
Hi All, Does somebody know what's the length's ratio in we convert byte to base64 string? Is it a constant ratio or it can be different from case to case? For example if I convert a byte array...
7
by: Jim | last post by:
I have to access a third party api to work with their hardware. Everything was going great UNTIL I ran into the problem of C# char being 16 bits and the C char being 8 bits. I cannot seem to...
6
by: Ricardo Quintanilla | last post by:
i have a code that sends data to a socket listening over as400 platform, the socket responds to me as a "byte array". then i need to convert the "byte array" into a string. the problem is that...
6
by: juli | last post by:
Hello dear Cor or anyone around!:) This didn't help me(convert) : I have 3 strings in an array : str='11/02/04' ,str='11:23:00" and str=AM. How do I convert str+str+str to a proper datetime...
3
by: James | last post by:
Hi, I am developing a ActiveX Control which will be used in a web page. The control will encrypt some value then decrypt it when the web page opens next time. I tested the control in a windows...
14
by: ern | last post by:
Does a function exist to convert a 128-bit hex number to a string?
2
by: duylam76 | last post by:
I'm programming a server and corresponding client application that sends data to each other over a socket. I want to encode an integer to send over the socket. Right now everything is fine because...
3
by: sam | last post by:
same as subject?
1
by: tom thomas | last post by:
Dear sir, i am very new to c# and i request you to... please look into the problem The following is the code(please see the code below:) which i uses to open the binary file,currently it works...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
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...
0
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,...
0
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...

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.