Difference between Text And Binary


DevX Home    Today's Headlines   Articles Archive   Tip Bank   Forums   

Results 1 to 10 of 10

Thread: Difference between Text And Binary

Hybrid View

  1. #1
    Join Date
    Nov 2005
    Posts
    12

    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

  2. #2
    Join Date
    Jan 2005
    Location
    UK
    Posts
    604
    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

  3. #3
    Join Date
    Nov 2003
    Posts
    4,118
    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

  4. #4
    Join Date
    Nov 2005
    Posts
    12
    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!

  5. #5
    Join Date
    Nov 2003
    Posts
    4,118
    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

  6. #6
    Join Date
    Nov 2005
    Posts
    12
    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

  7. #7
    Join Date
    Nov 2003
    Posts
    4,118
    Quote 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

  8. #8
    Join Date
    Dec 2003
    Posts
    3,366
    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.

  9. #9
    Join Date
    Dec 2003
    Posts
    3,366
    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).

  10. #10
    Join Date
    May 2006
    Posts
    1

    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

  1. Replies: 0
    Last Post: 12-20-2005, 05:29 AM
  2. Hash Text in XML????
    By Chris Beckett in forum .NET
    Replies: 1
    Last Post: 08-08-2002, 04:36 PM
  3. Replies: 1
    Last Post: 07-25-2002, 06:06 PM
  4. Why is this not binary compatible
    By Kip Fryman in forum VB Classic
    Replies: 2
    Last Post: 09-07-2001, 06:19 AM
  5. Binary to Text Conversion
    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
  •  
HTML5 Development Center
 
 
FAQ
Latest Articles
Java
.NET
XML
Database
Enterprise
Questions? Contact us.
C++
Web Development
Wireless
Latest Tips
Open Source


   Development Centers

   -- Android Development Center
   -- Cloud Development Project Center
   -- HTML5 Development Center
   -- Windows Mobile Development Center