Like Tree1Likes
  • 1 Post By Malcolm McLean

ascii to binary, how??

This is a discussion on ascii to binary, how?? within the C Programming forums, part of the General Programming Boards category; Hi, i have this piece of code, the encoding function creates a file of binary 1's and 0's but since ...

  1. #1
    Registered User
    Join Date
    Nov 2011
    Posts
    15

    ascii to binary, how??

    Hi,
    i have this piece of code, the encoding function creates a file of binary 1's and 0's but since i am getting output in a text file, the result is shown as ASCII characters which appear as garbage. can anyone please tell me what should i add in the encode function, so that i get binary in the file rather than ascii. I am attaching the code. please only see the encoding part, the decoding part works fine.

    thanks
    Attached Files Attached Files
    • File Type: c r.c (7.0 KB, 33 views)

  2. #2
    Registered User
    Join Date
    Jun 2005
    Posts
    6,299
    Hint only: what is the difference between a character with value zero, and a character that displays as a zero ('0') on screen?

    Presumably if you correct the encoding, you will also need to adjust the decoding.
    Right 98% of the time, and don't care about the other 3%.

  3. #3
    Registered User
    Join Date
    Sep 2008
    Location
    Toronto, Canada
    Posts
    1,832
    My guess is there is nothing wrong with writing binary data to a text file. The flaw is letting NOTEPAD or whatever display the data and interpreting it as ASCII. Hence the garbage appearance. If your encode / decode is correct, you should be able to retrieve the correct data from the file regardless of how garbagey it looks in the screen.

  4. #4
    qny
    qny is offline
    Registered User
    Join Date
    Sep 2012
    Posts
    355
    Instead of looking at the file with a text editor, try a hex editor.

  5. #5
    Registered User
    Join Date
    May 2012
    Posts
    333
    That's not true. If you open a file in text mode, then some characters may be translated between call and write. A common one is expanding newline to newline plus carriage return. Also, if you write 26 (Control Z), on some systems that will be intepreted as end of file.
    So you must open a file with "wb" before writing binary data, or you may get strange results.
    stahta01 likes this.
    I'm the author of MiniBasic: How to write a script interpreter and Basic Algorithms
    Visit my website for lots of associated C programming resources.
    http://www.malcolmmclean.site11.com/www

  6. #6
    Registered User
    Join Date
    May 2009
    Posts
    2,577
    Quote Originally Posted by Malcolm McLean View Post
    So you must open a file with "wb" before writing binary data, or you may get strange results.
    This is true for Windows OSes; I think it is false for most Unix Like OSes.
    It one of the things you need to watch for when converting code from Linux to Windows.

    Tim S.
    "Programming today is a race between software engineers striving to build bigger and better idiot-proof programs, and the universe trying to produce bigger and better idiots. So far, the Universe is winning." Rick Cook

  7. #7
    qny
    qny is offline
    Registered User
    Join Date
    Sep 2012
    Posts
    355
    Quote Originally Posted by stahta01 View Post
    I think [using binary mode with stdio files] is false for most Unix Like OSes.
    Unix-like OSes treat text mode files and binary mode files the exact same way.
    Using binary mode when not dealing with text is, nevertheless, a good idea TM no matter the OS in use.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. binary to ascii
    By bazzano in forum C Programming
    Replies: 3
    Last Post: 06-04-2007, 08:21 AM
  2. ASCII to Binary and Hex
    By Korhedron in forum C++ Programming
    Replies: 8
    Last Post: 07-28-2003, 09:31 AM
  3. ascii v. binary
    By ygfperson in forum C++ Programming
    Replies: 2
    Last Post: 04-26-2002, 04:00 PM
  4. binary to ascii
    By ygfperson in forum C++ Programming
    Replies: 2
    Last Post: 04-20-2002, 08:36 AM
  5. ASCII -> Binary
    By Unregistered in forum C Programming
    Replies: 5
    Last Post: 11-13-2001, 04:51 AM

Tags for this Thread


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