Differentiating between binary and ASCII input streams

This is a discussion on Differentiating between binary and ASCII input streams within the C Programming forums, part of the General Programming Boards category; I am currently working on a project where I will be having both binary and text information streams. If the ...

  1. #1
    Registered User
    Join Date
    Oct 2001
    Posts
    1

    Question Differentiating between binary and ASCII input streams

    I am currently working on a project where I will be having both binary and text information streams. If the stream is binary then I do nothing and send it on. If the stream is text then I need to do some parsing. Does anyone have a great suggestion for quickly and reliably determing whether a buffer contains binary information or not?
    I'm looking at the source for the UNIX "strings" command and may steal code out of that but I thought I would check to see if there are other methods. Ideally I would want to check the first few bytes of the buffer so I don't get bugs if the first byte of the binary coincidentally maps to an ASCII character.

    thanks,
    -Peleus

  2. #2
    Unregistered
    Guest

    Lightbulb

    The sender of the stream must know what type of data is being sent...maybe the stream sender can put a header record on the stream which will indicate the stream type?

  3. #3
    Registered User pinko_liberal's Avatar
    Join Date
    Oct 2001
    Posts
    284
    If you mean distinguishing a file pointer obtained by opening a file in binary mode with that in text mode , I dont think this is possible using just ansi C , for in many systems there is no difference between the two modes .

  4. #4
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,687
    > If the stream is text then I need to do some parsing
    You can't really know the answer to this until you've read the whole file (there could be a single binary value at the last byte in the file).

    You need to know something about the files you're getting in order to make this determination.

    > I'm looking at the source for the UNIX "strings" command
    The 'file' command is the one which determines file types.

    Typically, a text file is all printable characters in the first 256 bytes of the file.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  5. #5
    Unregistered
    Guest
    > Typically, a text file is all printable characters in the first 256
    > bytes of the file.

    In which case, just include ctype.h and use 'isprint()' on each byte.

    Quzah.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. help with c program: binary to ascii program
    By bigmac(rexdale) in forum C Programming
    Replies: 26
    Last Post: 02-03-2008, 01:26 PM
  2. Binary to Ascii Conversion
    By codemaster12 in forum C Programming
    Replies: 2
    Last Post: 10-24-2007, 10:57 AM
  3. Wininet Binary and ASCII
    By maxorator in forum Windows Programming
    Replies: 5
    Last Post: 11-26-2005, 02:16 AM
  4. Binary Search Trees Part III
    By Prelude in forum A Brief History of Cprogramming.com
    Replies: 16
    Last Post: 10-02-2004, 03:00 PM
  5. ASCII -> Binary
    By Unregistered in forum C Programming
    Replies: 5
    Last Post: 11-13-2001, 04:51 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21