Thread: More hex and binary conversions

  1. #1
    Registered User
    Join Date
    Sep 2001
    Posts
    4,912

    More hex and binary conversions

    CGIASM again - if you haven't heard the whole story - nevermind, it's not relevant.

    Lets say I need to enter 11001101 into a file. If I used the conventional way, I'd get the ascii character representing a 1 and a 0. I need it in the actually binary, so that it should actually represent ascii 205, or "-" Now once I can convert it to actuall binary, I'm OK. But how can I work with binary numbers in C? I'll need to declare them, initilaize them, add to them using a literal, and then convert them to decimal ascii numbers/characters. How do I do this?

  2. #2
    ....
    Join Date
    Aug 2001
    Location
    Groningen (NL)
    Posts
    2,380
    What do you mean with binary numbers? This way you can declare a byte.

    unsigned char byte;

    And I guess this way you print the ASCII value of 205.

    byte = 205;
    printf ("%c\n", byte);

  3. #3
    *
    Guest
    It does not matter whether it is a char, int, long, pointer, etc. It is _all_ binary.

    *Stop* thinking about these elements as if they are different types of data-- they are not. they are all binary in different bit-widths.

    The easiest way to work with binary in C, is to work with it in its hex form. that is because hex characters break down to 4-bit nybles. They are recognized by the compiler as numbers, so you don't have to play silly string games with them.

    You can shift and mask them, and perform math on them to get what you want done.

    ---

    Let me ask this critical question-- is it difficult, or awkward for you to convert between hex and binary representations? Perhaps there are some mechanics I could explain that will make working with decimal/hex/binary conversion trivial for you.

  4. #4
    ....
    Join Date
    Aug 2001
    Location
    Groningen (NL)
    Posts
    2,380
    Even easier is to make use of bitfields.

  5. #5
    Registered User
    Join Date
    Sep 2001
    Posts
    4,912
    In response to * - Duh. I know. I wanted to know how to enter a number in the binary number system, and trhen have it stored as decimal format.

  6. #6
    Registered User
    Join Date
    Oct 2001
    Posts
    2,934
    Use strtoul() or strtol().
    Code:
    #include <stdlib.h>
    #include <stdio.h>
    
    int main(void)
    {
       char s[33] = "11111111";
       unsigned long num;
       char *endptr;
    
       num = strtoul(s,&endptr,2);
       printf("num:%08lX\n",num);
       return 0;
    }

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Memo Box to Binary
    By peckitt99 in forum C++ Programming
    Replies: 8
    Last Post: 11-14-2007, 05:22 PM
  2. Binary to Hex Conversion
    By elrookie in forum C Programming
    Replies: 8
    Last Post: 06-26-2007, 10:58 AM
  3. Hex to binary conversion and storing in nybble
    By shoobsie in forum C Programming
    Replies: 14
    Last Post: 10-25-2005, 02:51 AM
  4. Printing Hex as Binary
    By Mystic_Skies in forum C Programming
    Replies: 6
    Last Post: 11-22-2004, 04:18 PM
  5. Is binary HEX?
    By Budgiekarl in forum Tech Board
    Replies: 11
    Last Post: 11-23-2003, 09:02 AM