Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6719979
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T09:09:26+00:00 2026-05-26T09:09:26+00:00

I am trying to convert a character to its binary representation (so character –>

  • 0

I am trying to convert a character to its binary representation (so character –> ascii hex –> binary).

I know to do that I need to shift and AND. However, my code is not working for some reason.

Here is what I have. *temp points to an index in a C string.

char c;
int j;
for (j = i-1; j >= ptrPos; j--) {
    char x = *temp;
    c = (x >> i) & 1;
    printf("%d\n", c);
    temp--;
}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-26T09:09:27+00:00Added an answer on May 26, 2026 at 9:09 am

    We show up two functions that prints a SINGLE character to binary.

    void printbinchar(char character)
    {
        char output[9];
        itoa(character, output, 2);
        printf("%s\n", output);
    }
    

    printbinchar(10) will write into the console

        1010
    

    itoa is a library function that converts a single integer value to a string with the specified base.
    For example… itoa(1341, output, 10) will write in output string “1341”.
    And of course itoa(9, output, 2) will write in the output string “1001”.

    The next function will print into the standard output the full binary representation of a character, that is, it will print all 8 bits, also if the higher bits are zero.

    void printbincharpad(char c)
    {
        for (int i = 7; i >= 0; --i)
        {
            putchar( (c & (1 << i)) ? '1' : '0' );
        }
        putchar('\n');
    }
    

    printbincharpad(10) will write into the console

        00001010
    

    Now i present a function that prints out an entire string (without last null character).

    void printstringasbinary(char* s)
    {
        // A small 9 characters buffer we use to perform the conversion
        char output[9];
    
        // Until the first character pointed by s is not a null character
        // that indicates end of string...
        while (*s)
        {
            // Convert the first character of the string to binary using itoa.
            // Characters in c are just 8 bit integers, at least, in noawdays computers.
            itoa(*s, output, 2);
    
            // print out our string and let's write a new line.
            puts(output);
    
            // we advance our string by one character,
            // If our original string was "ABC" now we are pointing at "BC".
            ++s;
        }
    }
    

    Consider however that itoa don’t adds padding zeroes, so printstringasbinary(“AB1”) will print something like:

    1000001
    1000010
    110001
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

is there a simple way to convert a character to its binary representation? Im
I'm trying to convert all instances of the > character to its HTML entity
I'm trying to convert a character code to a character with chr(), but VBScript
I'm trying to convert some code that worked great in VB, but I can't
I'm trying to convert all character strings that match a particular regex in a
I am writing this code to convert a hex entry into its integer equivalent.
Trying to convert this c code into MIPS and run it in SPIM. int
I'm trying to convert old QuickTime framework code to the 64-bit Cocoa-based QTKit on
Its been a while now and im still trying to get a certain code
I'm trying to convert the variable $num into its reverse byte order and print

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.