-
Difference between Text And Binary
Dear All,
I would appreciate some help regarding what is the difference between Binary and Text. For example:
char s[6] = "bytes";
s is a bunch of bytes that you tell the compiler to treat them as an array of chars.
1) How do you convert this to an array of bytes using C? (binary mode)
2) If you explore a string with an Hex Editor, what are the differences in memory if you have this string in a text mode or if you have it in a binary mode?
Thank you.
fsn
-
Hi,
what you have here is an array of bytes. the bytes you have here are just all "printable characters" by coincidence. There are some escape sequences for special characters like \n (newline) \r (carriage return) \t (horiz tab) but if you want to use other characters you would need to address them by their ASCII code.
(look at www.asciitable.com).
When you need an array of bytes, you could do something like:
char s[] = { 0x01, '3', '\n', '\0', 5, 'a', 'b', 'c' };
which is an array of 7 bytes:
asc(1),asc(51),asc(10),asc(0),asc(5),asc(97),asc(98),asc(99)
When you try to print that array with something like printf() the result will probably not be quite what you expect, though you can loop through the array and handle the array elements as you would any other array.
Hope that helps?
Cheers,
Dieter
-
The difference between text and binary mode is pretty small, and in some cases it doesn't exist. However, if you want to treat a sequence of bytes as octets (i.e., a binary number containing 8 bits), you should declare it as unsigned char to avoid signing.
On some platforms, a binary file differs from a text file in the way in which the CR and FF ascii codes are treated but in the case of the quoted string you included, there's no real difference between the two. In either case, the char array is stored in contigious memory as a sequence of bytes.
Danny Kalev
-
Danny understood! Is there a way I can find an algorithm to convert an array of chars to binary without using C++ streams only using C?
Thanks!
-
What do you mean by "binary"? zeroes and ones? In C you will have to write it all by yourself. If you just need the number, say in hex or whatever, simply treat the char array as an array of integers.
Danny Kalev
-
I just wanted a C algorithm equivalent to C++ streams. Is it as you quoted: " .. just need the number, say in hex or whatever, simply treat the char array as an array of integers" ?
Thanks.
fsn
-
Yes. The char array, taken value by value, is the binary you seek. For examle 'A' is 65 or something, which is whatever in hex, they are all the same and all ones and zeros to the computer. AB is {65,66} etc.
-
On that note, the real issue here is age. C is an old language, and long ago, someone decided that it would be handy to make binary and text different things, altho text is always a subset of binary so all files are "binary" files. I find the distinction silly, but we are stuck with it.
Remember that binary for ints and floats and the like are a little more complex (endian issues, size of issues, etc).
-
 Originally Posted by fsn
I just wanted a C algorithm equivalent to C++ streams. Is it as you quoted: " .. just need the number, say in hex or whatever, simply treat the char array as an array of integers" ?
Thanks.
fsn
then simply copy each byte into an intger:
char ch[]="hello";
for (...)
myints[i]=ch[i];
Danny Kalev
-
Difference between text and binary
Binary was designed for efficiency when transmitting text only files. Binary is strictly a 7 bit character representation. Binary says the eighth bit does not matter so we will only transmit 7 bits and this is where the efficiency comes into play. You might remember modem settings can be set to 8N1. This means 8 data bits no parity and one stop bits. If you wanted to save some transmission time downloading a text file you could set your modem to 7E1.
This way the modem transmits seven data bits per character and saves you some transmission time. This setting drops the high bit which is realy not relevant to the ASCII character set in the range of 0-127. The FTP protocol can also be set to binary prior to transmission of text files. If the eigth bit is critical for transmitting binary files such as program executables 8 data bits must always be sent. Likewise if you use the extended IBM ASCII chacter set
in the range 128-255 (example ASCII 195 represents ├ ) then 8 bits must also be transmitted. Remember this was invented back in the days when 300 baud modems were used. Hope this gives some insight into the pupose and history of binary transmissions of character text.
Ron Stevenhaagen
Similar Threads
-
By symbian_ravi in forum Java
Replies: 0
Last Post: 12-20-2005, 05:29 AM
-
By Chris Beckett in forum .NET
Replies: 1
Last Post: 08-08-2002, 04:36 PM
-
By Karen in forum VB Classic
Replies: 1
Last Post: 07-25-2002, 06:06 PM
-
By Kip Fryman in forum VB Classic
Replies: 2
Last Post: 09-07-2001, 06:19 AM
-
By Nicholas in forum Database
Replies: 0
Last Post: 05-03-2001, 02:23 AM
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Development Centers
-- Android Development Center
-- Cloud Development Project Center
-- HTML5 Development Center
-- Windows Mobile Development Center
|