473,326 Members | 2,192 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,326 software developers and data experts.

byte to bit & bit to byte

mb29
3
i have been working on this problem for last 2 days but i think i need someones help

so here's my problem . . .

i want to read a character from a file, convert that into a string of 0's n 1's; store them into an array binary[8] and write them to another file.

then read the second file which contained the binary code of the file an convert it back to its exact original form . . . . but how?

n one last question what does this mean a bit or a byte :

fstream cfile("abc.dat");
char O = ' 0 ';
cfile<<O-48;
cfile.close();

I m working on c++ 3.0 compiler
Jun 26 '07 #1
6 21031
DeMan
1,806 1GB
a bit literally stands for "binary digit" and is basically a single digit in binary (a 1 or a 0). A Byte (look up in wikipedia, there is some debate over the exact origins of the word) is a series of bits (these days, usually 8) which are grouped together to represent something (eg with 8 bits to a byte we have 2^8 different arrangements - 0 to 255, which can be represented by the ascii characters).

It is not totally clear to me what you want to do, but it sounds like you want to convert a character to it's bit string. A character is simply a one-byte integer, and can (mathematically) be treated as such.

One way you could get this value is (in pseudocode)
Expand|Select|Wrap|Line Numbers
  1. char b = <the caracter you want>;
  2. string bin="";
  3. for(int i=0; i<8; i++)
  4. {
  5.   bin = (b mod 2) + bin;
  6.   b=b/2;
  7. }
  8.  
if you have a string of characters, just iterate through each character and repeat this for each one....
Jun 26 '07 #2
mb29
3
ok thank u for ur reply.
let me explain my question once again to you

i want to read a character from a file (fstream, xyz.ext), convert that into a string of 0's n 1's(either integers or char's anyone); store them into an array int binary[8] and write them to another file.

after that again read the binary[8] data from the file(abc.dat) which we wrote in the first step and again convert that back into the original file( that was xyz.ext)
Jun 26 '07 #3
DeMan
1,806 1GB
to convert back from the bit string above we can do something like

Expand|Select|Wrap|Line Numbers
  1. char bin[] = <the binary string from before>
  2. char val = 0;
  3. for(int i=0; i<8 i++)
  4. {
  5.   val = val*2;
  6.   val = val + bin[i];
  7. }
  8.  
Jun 26 '07 #4
to convert back from the bit string above we can do something like

Expand|Select|Wrap|Line Numbers
  1. char bin[] = <the binary string from before>
  2. char val = 0;
  3. for(int i=0; i<8 i++)
  4. {
  5.   val = val*2;
  6.   val = val + bin[i];
  7. }
  8.  
You want to read character from file ..mean you read "c" , then u want to convert into binary. " 1100011 " and then want to store it into int binary[8] = { 1,1,0,0,0,1,1} ; and then want to write binary data into other file.

and then after u want to do reverse of that ...Wrong then correct me ..

.
Kiran Parmar
Jun 26 '07 #5
mb29
3
You want to read character from file ..mean you read "c" , then u want to convert into binary. " 1100011 " and then want to store it into int binary[8] = { 1,1,0,0,0,1,1} ; and then want to write binary data into other file.

and then after u want to do reverse of that ...Wrong then correct me ..

.
Kiran Parmar
yes u are right. if u can help me then plsease tell me further step in better way
Jun 26 '07 #6
weaknessforcats
9,208 Expert Mod 8TB
Don't overlook using the STL:

Expand|Select|Wrap|Line Numbers
  1. int data = 11234;
  2. bitset<32> bits(data);
  3. string onesandzeroes = bits.to_string();
  4.  
and you are done.
Jun 26 '07 #7

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

Similar topics

15
by: jeko | last post by:
Hi, is it possible to read a double variable one byte at a time. I would want store each byte into unsigned char and then assemble again the double variable again. thanx Andrea
1
by: Wasim Akram | last post by:
Hi, I have a field "Month" in my SQL server table. The type of this field is "tinyint". Now what I am doing in the code is using DataRow to read this field in a 'int' variable. int month...
1
by: Carl | last post by:
Hi, I have an array of bytes like Byte m_Data = new Byte ; Byte sequences starting at a random index (4 byte aligned) should be interpreted as unsigned long values and compared with each...
32
by: Guoqi Zheng | last post by:
I am really do not know so much about byte/bit, etc. Question, if I defined a byte(), how can I add a single byte to it? what I want is that I have an array of bytes, I loop that array, look at...
6
by: Narshe | last post by:
How can I convert a byte into a byte?? You can assign a byte to a byte?, but you obviously can't do that with an array. The way I'm currently doing it is a for loop that just copies the data...
16
by: Hugh Janus | last post by:
Hi all, I am using the below functions in order to convert strings to bytes and vice versa. I totally ans shamefully stole these functions from this group btw! Anyway, they work great but as...
6
by: Seabass | last post by:
Hello everyone, I'm trying to do a Hex Viewer in C#. I'm reading the user selected file byte by byte but it seems to take forever ( one minute to read a larger file ). Is there any way to make...
3
by: efdeugenio | last post by:
Hi, I will really appreciate if someone cans help me with this: I have a managed c++ class that I am calling from C#. The declaration of a function in this class is: bool CanAddTemplate(unsigned...
6
by: Ashit Vora | last post by:
Hi, I had one small query. I 'm coping data from an unsigned short to a char*. code is #include<stdio.h> int main(void){ unsigned short type=0xFE10;
18
by: wquatan | last post by:
Does someone know an (fast) example of reading files in VBA, Byte per Byte ? I need to be able to take action on every single byte Thx
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
1
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 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 former...

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.