Thread: Is there any function for converting a character to a binary value?

  1. #1
    Registered User
    Join Date
    Dec 2006
    Posts
    85

    Is there any function for converting a character to a binary value?

    Thanks in advance!

  2. #2
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    EVERYTHING in a computer is stored as binary, so a char is binary already. I suspect what you are actually asking is how you DISPLAY a char as a binary value. To do that, you need to use modulo and divide to split the number into individual digits.

    --
    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.

  3. #3
    Registered User
    Join Date
    Dec 2006
    Posts
    85
    Could you please give a simple example?because i'm a bit confused!Let's say we have 'w' and we wold like to get its value how do we use divide and modulo ?

  4. #4
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Let's say we have the value 5.
    5 Modulo 2 gives 1.
    5 divide by 2 gives 2.
    2 modulo 2 gives 0
    2 divide by 2 gives 1
    1 modulo 2 gives 1
    1 divide by 2 gives 0.
    The value of 5 in binary is therefore 101 (note that the numbers we get from the above calculation are "backwards", so you need to store the digits and display them from the back in some way).

    For all intents and purpose, 'w' will be seen as a number by the computer (compiler) - if for example you do:
    Code:
    printf("'w' is %d\n", 'w');
    you would get the numeric value of 'w' (most likely 119, but it does depend on what type of character representation your computer uses - most use something based on ASCII, where 'w' is 119, but there are other standards in existence).

    --
    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.

  5. #5
    Registered User
    Join Date
    Dec 2006
    Posts
    85
    Thank you very much!It worked!

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. In over my head
    By Shelnutt2 in forum C Programming
    Replies: 1
    Last Post: 07-08-2008, 06:54 PM
  2. Undefined Reference Compiling Error
    By AlakaAlaki in forum C++ Programming
    Replies: 1
    Last Post: 06-27-2008, 11:45 AM
  3. Screwy Linker Error - VC2005
    By Tonto in forum C++ Programming
    Replies: 5
    Last Post: 06-19-2007, 02:39 PM
  4. Game Pointer Trouble?
    By Drahcir in forum C Programming
    Replies: 8
    Last Post: 02-04-2006, 02:53 AM
  5. Contest Results - May 27, 2002
    By ygfperson in forum A Brief History of Cprogramming.com
    Replies: 18
    Last Post: 06-18-2002, 01:27 PM