Thread: ascii to binary, how??

  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, 150 views)

  2. #2
    Registered User
    Join Date
    Jun 2005
    Posts
    6,815
    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%.

    If I seem grumpy or unhelpful in reply to you, or tell you you need to demonstrate more effort before you can expect help, it is likely you deserve it. Suck it up, Buttercup, and read this, this, and this before posting again.

  3. #3
    Registered User
    Join Date
    Sep 2008
    Location
    Toronto, Canada
    Posts
    1,834
    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
    Registered User
    Join Date
    Sep 2012
    Posts
    357
    Instead of looking at the file with a text editor, try a hex editor.

  5. #5
    Registered User
    Join Date
    May 2012
    Posts
    505
    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.
    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.
    https://github.com/MalcolmMcLean


  6. #6
    Registered User
    Join Date
    May 2009
    Posts
    4,183
    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.
    "...a computer is a stupid machine with the ability to do incredibly smart things, while computer programmers are smart people with the ability to do incredibly stupid things. They are,in short, a perfect match.." Bill Bryson

  7. #7
    Registered User
    Join Date
    Sep 2012
    Posts
    357
    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, 05:51 AM

Tags for this Thread