In need of guidance

This is a discussion on In need of guidance within the C Programming forums, part of the General Programming Boards category; I am currently working on a program to convert a character to binary and binary back to a character. This ...

  1. #1
    Registered User
    Join Date
    Nov 2008
    Posts
    3

    In need of guidance

    I am currently working on a program to convert a character to binary and binary back to a character. This is an assignment for school. I have the first part done and I do understand the function for converting the ASCII into binary. What I am having problems with is the function for converting the binary back into ACSII. I have looked around on the net for examples but for some reason I am not grasping the code.

    Where/how should I look for an explanation on how a function like that should work.

  2. #2
    Banned master5001's Avatar
    Join Date
    Aug 2001
    Location
    Visalia, CA, USA
    Posts
    3,685
    I have posted many.... so to be nice I will just go find one for you.

  3. #3
    Registered User
    Join Date
    Oct 2008
    Location
    TX
    Posts
    2,047
    Have you tried searching the forums for what you need?

  4. #4
    Banned master5001's Avatar
    Join Date
    Aug 2001
    Location
    Visalia, CA, USA
    Posts
    3,685

  5. #5
    Registered User
    Join Date
    Nov 2008
    Posts
    3
    Code:
     void int16tobstr(short x, char s[17])
    {
      int bit;
    
      for(bit = 0; bit < 16; ++bit)
        *s++ = '0' + !!(x & (1 << bit));
     *s = 0;
    }

    So with this function s[17] is the array for the binary number. so is *s++ used to check the numbers in the array against the 0 + !!(x &(1<<bit)). so roughly how does it work??

    but thanks for the function.

  6. #6
    Banned master5001's Avatar
    Join Date
    Aug 2001
    Location
    Visalia, CA, USA
    Posts
    3,685
    Ok lets make it a little more generalized

    Example:
    Code:
    #include <limits.h>
    
    char *longtobstr(long x)
    {
      int bit;
      char *s = malloc(LONG_BIT + 1);
    
      if(!s)
        return 0;
    
      s[LONG_BIT] = '\0'; /* Terminate the string. */
    
      for(bit = 0; bit < LONG_BIT; ++bit)
        s[LONG_BIT - bit] = '0' + !!(x & (1 << bit));
    
      return s;
    }
    Clearer?

  7. #7
    Registered User
    Join Date
    Nov 2008
    Posts
    3
    so I tried the longtobstr functions and I get an LONG_BIT error for being undefined. I have a feeling that my head is fully of jellie right now and I am missing something pitifully simple.

  8. #8
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    I wouldn't call that a good example of how to write such a function.

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  9. #9
    Guest Sebastiani's Avatar
    Join Date
    Aug 2001
    Location
    Waterloo, Texas
    Posts
    5,639
    Maybe not, but at least it can't be used for homework.

    1) Make sure the array size is equal to CHAR_BIT. If not, make adjustments or bail out.
    2) The rightmost digit (array[ CHAR_BIT - 1 ]) is least significant, so the mapping with bit positions is reversed. Alternately, you can reverse the string to take advantage of the more natural mapping. Either way, 1 << index corresponds to array[ index ].
    3) Use X |= Y to enable a bit, X &= ~Y to clear one (assuming X is initialized to zero, the latter probably won't be necessary).
    Last edited by Sebastiani; 11-24-2008 at 06:27 PM. Reason: -verbose
    Code:
    if( numeric_limits< byte >::digits != bits_per_byte )
        error( "program requires bits_per_byte-bit bytes" );
    24bbs.cpp

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Need some guidance.
    By Kinto in forum C Programming
    Replies: 10
    Last Post: 05-31-2009, 12:02 AM
  2. Guidance, please.
    By mattagrimonti in forum C Programming
    Replies: 2
    Last Post: 11-26-2008, 07:50 AM
  3. need guidance to connect to serial port
    By gnychis in forum Linux Programming
    Replies: 1
    Last Post: 06-02-2005, 10:10 AM
  4. Audio guidance.
    By Sebastiani in forum Windows Programming
    Replies: 6
    Last Post: 12-22-2002, 08:14 AM
  5. advice and possibly guidance
    By nazri81 in forum C++ Programming
    Replies: 3
    Last Post: 11-07-2002, 09:19 PM

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