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 8513275
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 11, 20262026-06-11T04:31:08+00:00 2026-06-11T04:31:08+00:00

I am trying to printout an unsigned char value as a 2-Digit hex value,

  • 0

I am trying to printout an unsigned char value as a 2-Digit hex value, but always getting the result as 4-Digit hex values, not sure what’s wrong with my code.

// unsigned char declaration
unsigned char status = 0x00;
// printing out the value 
printf("status = (0x%02X)\n\r", (status |= 0xC0));

I am expecting a 2 digit hex result as 0xC0, but I always get 0xC0FF.

As well, when I tried to print the same variable (status) as an unsigned char with the %bu format identifier I got the output as 255.

How do you get just the two hex characters as output?

  • 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-06-11T04:31:10+00:00Added an answer on June 11, 2026 at 4:31 am

    As far as I know, the Keil C compiler doesn’t fully conform to the C standard. If so, it’s likely that it doesn’t quite follow the standard promotion rules for things like passing char values to variadic functions; on an 8-bit CPU, there are performance advantages in not automatically expanding 8-bit values to 16 bits or more.

    As a workaround, you can explicitly truncate the high-order bits before passing the argument to printf. Try this:

    #include <stdio.h>
    
    int main(void) {
        unsigned char status = 0x00;
        status |= 0xC0;
    
        printf("status = 0x%02X\n", (unsigned int)(status & 0xFF));
        return 0;
    }
    

    Doing a bitwise “and” with 0xFF clears all but the bottom 8 bits; casting to unsigned int shouldn’t be necessary, but it guarantees that the argument is actually of the type expected by printf with a "%02X" format.

    You should also consult your implementation’s documentation regarding any non-standard behavior for type promotions and printf.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm trying to pullout values from a uint8_t array. But I'm having troubles understanding
Trying to build out an exception if move.UserId does not equal currentUserId then Redirect
I'm trying to start emulator-arm but it seems to get stuck and never shows
I am trying to print out the offset values. (Is the term print correct?
I have been working with C++ strings and trying to load char * strings
this is what i'm trying to do. I have a Student Result Application in
Here's what i'm trying to do. I have a Student Result Application in which
I'm just barely getting into NodeJS a bit and have hit a snag trying
I don't have too much experience with VB Scripting but I'm trying to write
I am trying to take printout an HTML (has 1 page) Firefox 3.1 is

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.