🔒 Locked
Saving Binary
Advertisement
Original Post
LoL just convert it from binary to ASCII and save it into a file. Considering all binary was used for was to create other forms of programming code after you werre to convert it from binary to ascii then you save it to a file but make sure its "assembly" code since assembly is a higher form of binary. It is binary then. There is binary but it only is used to program things through the use of ascii now of days I do not belive its still possible I may be wrong on some of this but I know what im talking about at least enough that it makes since to me then again I may be wrong. However if I am I would like to know and see an example of how exactly I am.
Don't click me!
Killer Eagle Software
[edited by - DevLiquidKnight on January 15, 2003 7:34:19 PM]
Don't click me!
Killer Eagle Software
[edited by - DevLiquidKnight on January 15, 2003 7:34:19 PM]
Well 1 open a file and then write \n\n\n\n0011 lol.. end of story
ok if I have a binary number of 0000 0000 0000 and want to save it in a binary file format how do I assign a variable a binary value? Or when I save the variable to a file as binary it will convert the value for me? e.g.
Now how to do assign a binary value to a type?
e.g. int c = 0000 0000 0000?
or any type for that matter?
I have about 24 different values that have a binary value I need to have associated with the numbers 1-24. I will be taking input from the keyboard. e.g. you type 1 you get the binary value 0000 0000 0000 and now I want to save that value in a binary file right justified.
Also how would I see the actual binary code in a file e.g. 000100011010100 So I can look at the file and make sure the binary match correctly?
I hope that clears this up somewhat? =) Thanks
[edited by - Mars_999 on January 15, 2003 11:36:21 PM]
ofstream fout("foo.foo", ios::out | ios::binary);
int main(int argc, char *argv[])
{
int a = 20;
char b = '20';
fout.write(reinterpret_cast<char *>(a), sizeof(a));
//when this saves the value of a will it be binary in a binary file?
fout.write(reinterpret_cast<char *>(b), sizeof(b));
//when this saves the value of b will it be binary in a binary file?
// if these values of a and b are not saved as a binary value I need them to be saved as a binary value.
fout.close();
return 0;
}
Now how to do assign a binary value to a type?
e.g. int c = 0000 0000 0000?
or any type for that matter?
I have about 24 different values that have a binary value I need to have associated with the numbers 1-24. I will be taking input from the keyboard. e.g. you type 1 you get the binary value 0000 0000 0000 and now I want to save that value in a binary file right justified.
Also how would I see the actual binary code in a file e.g. 000100011010100 So I can look at the file and make sure the binary match correctly?
I hope that clears this up somewhat? =) Thanks
[edited by - Mars_999 on January 15, 2003 11:36:21 PM]
I can''t reiterate enough that binary is just a representation. Whenever you save a value to a file, it''s just that, a value, and there isn''t any representation associated with it. If you were to open up the file in a hex editor, you would be able to chose between maybe hex view, which would display that value in hexadecimal format, decimal view, and even binary view if it''s available.
That being said, if you want to assign the value 0110 0101 for example to a variable, there are multiple ways of doing that. You could store the bit pattern in a string using the characters ''0'' and ''1'' and then write a routine that calculates a value based on that string representation.
That would be the reverse of the previous routine. It would take a value and turn it into a string representation. That string would then be saved to a file or outputted to screen.
I think there might be confusion arrising from the difference between values and representation.
That being said, if you want to assign the value 0110 0101 for example to a variable, there are multiple ways of doing that. You could store the bit pattern in a string using the characters ''0'' and ''1'' and then write a routine that calculates a value based on that string representation.
quote:
Also how would I see the actual binary code in a file e.g. 000100011010100 So I can look at the file and make sure the binary match correctly?
That would be the reverse of the previous routine. It would take a value and turn it into a string representation. That string would then be saved to a file or outputted to screen.
I think there might be confusion arrising from the difference between values and representation.
Ok, my problem is I need to goto 400 as a numerical value and char only goes to 256? Am I right? So if I use char to represent value 0-255 I run out of values for 300 and 400. Now a char is 1 byte and each byte has 8 bits right? So if I take a string of data e.g. char *byte_zero = "000000000000" and save it to a binary file what will the binary number be for that defined *byte_zero be? will it be read back in as 000000000000? or just one 0. SO if I have a char *byte_one = "000000000001" what is the results? I am working on this for someone else, who needs basically an interpreter to take a number e.g. 1 and will save the binary number 000000000001 to a binary file not a text file. Sounds simple but I am getting confused. If I have this value char *byte_200 = "000011001000" is that correct binary for 200? And if it is I want that value saved as a binary file. ARGH!!!!!!!!!!!!
You are going about this all wrong. Don''t use char*. You need a value that goes from 0 to 400 you''ll need to use at least a 16 bit type. Might as well use int, which is 32 bits on most platforms, and is more than enough.
// This will write a value to a file, in binary mode.
int num;
num = 400;
FILE* fp = fopen("file.dat", "wb");
fwrite(#, sizeof(num), 1, fp);
fclose(fp);
// This will read the value from the file, in binary mode.
int num;
FILE* fp = fopen("file.dat", "rb");
fread(#, sizeof(num), 1, fp);
fclose(fp);
quote:
Original post by DevLiquidKnight
LoL just convert it from binary to ASCII and save it into a file. Considering all binary was used for was to create other forms of programming code after you werre to convert it from binary to ascii then you save it to a file but make sure its "assembly" code since assembly is a higher form of binary. It is binary then. There is binary but it only is used to program things through the use of ascii now of days I do not belive its still possible I may be wrong on some of this but I know what im talking about at least enough that it makes since to me then again I may be wrong. However if I am I would like to know and see an example of how exactly I am.
Yes, you''re wrong. Binary is simply another way of writing out a number. The traditional number system is decimal (base 10) where each digit place is 10 times greater than the last. The symbols 0,1,2,3,4,5,6,7,8 and 9 are used in the decimal number system. Binary is base 2, meaning that only 0''s and 1''s are used to represent a number. Thus, 255 in decimal equals 11111111 in binary. Another number system, hexadecimal (base 16), uses 16 symbols (0-9, and A-F). Hexadecimal numbers are usually prefixed with a "0x". 255 in decimal, 11111111 in binary, and 0xFF in hexidecimal are all the same number.
Do not confuse "binary" files with binary number systems. They are related, but not in the way most people think. All data on a computer is stored as 0''s and 1''s (binary). Thus, a binary file is a collection of 0''s and 1''s. A "text" file (either ASCII or Unicode) is also a binary file. However, each byte in a text file represents a single character or symbol (mostly English characters).
On a final note, please take the time to proofread your posts! It is very difficult to help someone when they have a problem if they can''t LOL! write clearly and include punctuation and form cohesive thoughts into complete sentences LOL of course I could be wrong but of course I am not because it is obvious that anyone can understand what other ppl write because if they can think at the same time they type and not proofread and then remember what they were thinking after they got done typing they must have typed some good junk and therefore they don''t need to spell-check or grammar-check ok i''m done writing now that''s great!
It is quite understandable to see less-than-perfect grammar and spelling from people who do not speak English as a first language. That does not excuse my fellow Americans (assuming that you are a fellow American who primarily speaks English) from concocting such syntactic drivel.
-Mike
Ok, now for a new question on this topic. If I have a newline binary equavalent which I have determined to be 25. 25 is what the person who is entering in data will enter to say newline. So if the person enters 1 2 3 100 25 50 200 300. Now I save these values as the binary equalavents when I save it to a binary file the whole file is just one long line of data correct? I don''t have to worry about doing newlines like when I am saving text in a text file correct? Thanks
// Converts a byte to bits
void Byte_2_Bits( unsigned char Byte, unsigned char *Bits )
{
for( unsigned char Bit=0; Bit<8; Bit++ )
{
Bits[7-Bit] = Byte%2;
Byte/=2;
}
}
// Converts 8 bits into a byte
unsigned char Bits_2_Bytes( unsigned char *Bits )
{
unsigned char OutByte = 0;
for( char Bit=7 ; Bit>=0 ; Bit-- )
{
if( Bits[Bit]==1 )
OutByte += Bits[Bit]*(unsigned char)pow( 2, 7-Bit );
}
return OutByte;
}
The Bits variable is an array of 8 indexes (8 bits / 1 byte)
KaMiKaZe
[edited by - Kamikaze15 on January 23, 2003 10:39:28 AM]
Topic Locked
This topic has been locked by a moderator. New replies are not allowed.
Sign in to reply to this topic.
Advertisement