Converting alphanumaric charecter in bianary

This is a discussion on Converting alphanumaric charecter in bianary within the C Programming forums, part of the General Programming Boards category; Pls tell me what logic I can use to convert a alphanumaric charecter in binary form....

  1. #1
    Registered User
    Join Date
    Dec 2005
    Posts
    16

    Lightbulb Converting alphanumaric charecter in bianary

    Pls tell me what logic I can use to convert a alphanumaric charecter in binary form.

  2. #2
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    I'd suggest the 'search button' logic.


    Quzah.
    Hope is the first step on the road to disappointment.

  3. #3
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,548
    a) It's all binary inside the machine
    b) convert it to what
    c) at least post some examples so we have some idea of what you want.

  4. #4
    cwr
    cwr is offline
    Registered Luser cwr's Avatar
    Join Date
    Jul 2005
    Location
    Sydney, Australia
    Posts
    868
    Code:
    char a = 'x'; /* x is now in binary form */

  5. #5
    Cached User mako's Avatar
    Join Date
    Dec 2005
    Location
    Germany.Stuttgart
    Posts
    69
    Code:
    /*try*/ printf("100101011101011lOl1!001000001010110");
    Code:
    char str_to_convert[10];
    int int_to_save_result;
    
    atoi(str_to_convert, int_to_save_result, 2);
    the atoi function takes a string or character array and turns all elements into one long integer with the base of the last argument. 2 meaning that it would turn the values into bianary
    Last edited by mako; 01-07-2006 at 06:00 AM.

  6. #6
    ... kermit's Avatar
    Join Date
    Jan 2003
    Posts
    1,528
    Nope - consider the function prototype for the atoi function:

    Code:
    int atoi(const char *nptr);
    It does not expect three arguments, just one.

    Perhaps you were thinking of strtol? (Which I don't think will work the way you want either)

    ~/
    Last edited by kermit; 01-07-2006 at 06:39 AM.

  7. #7
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,548
    Maybe it was the non-standard itoa function.
    Besides which, the buffer was woefully too small for 32 bits (and a \0) of potential binary data stored in your typical 32-bit integer.

  8. #8
    Rabble Rouser Slacker's Avatar
    Join Date
    Dec 2005
    Posts
    116
    >the atoi function takes a string or character array and turns all
    >elements into one long integer with the base of the last argument.
    My compiler's atoi only takes a string, and only handles base 10. The C standard seems to agree with that. I'd wager that you're using a non-standard extension and you need to be sure that the person you're helping has the same compiler as you do before suggesting it.

    >Pls tell me what logic I can use to convert a alphanumaric charecter in binary form.
    The simplest way, in this case, is probably to do it manually. Once you get your head around the bit twiddling stuff, it's pretty neat.
    Code:
    #include <stdio.h>
    #include <limits.h>
    
    void to_binary ( unsigned long x, int bits )
    {
      while ( --bits >= 0 )
        putchar ( ( x >> bits & 1 ) ? '1' : '0' );
    }
    
    int main ( void )
    {
      to_binary ( 'A', CHAR_BIT );
    
      return 0;
    }
    The expression funky bitwise expression is supposed to shift the bit that I want into the first position and test whether it's non-zero or not, then replace it with '0' or '1' accordingly. Since I start--theoretically--at the highest bit for the logical type and count down, that results in printing out the bit representation of the value. I used unsigned long as the type for x because how many bits the function prints depends on the bits argument, so to_binary() can be way more generic than if I just make x an char.

    Cheers!

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Converting Sign Magnitude Integer Binary Files
    By Arthur Dent in forum C Programming
    Replies: 7
    Last Post: 09-13-2004, 10:07 AM
  2. Converting from Screen to World Coordinates
    By DavidP in forum Game Programming
    Replies: 9
    Last Post: 05-11-2004, 12:51 PM
  3. converting string to float
    By twhubs in forum C Programming
    Replies: 1
    Last Post: 09-16-2001, 09:02 AM

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