Solved

How to printf("%02X",CBuf) to FF but not to FFFFFFFF

Posted on 1998-12-13
3
3,182 Views
Last Modified: 2008-02-26
char CBuf;

CBuf=0xFE;
 printf("%02X",CBuf);

I wish the result is FE, but it is FFFFFFFE.
Who can help me this? thanks!
0
Comment
Question by:wpy
3 Comments
 
LVL 84

Expert Comment

by:ozo
ID: 1255115
unsigned char CBuf;
0
 

Author Comment

by:wpy
ID: 1255116
thanks !! it work well!
0
 
LVL 10

Accepted Solution

by:
RONSLOW earned 10 total points
ID: 1255117
the problem is that the %x format is expecting an int.  You have put in a (signed) character with a negative value so the sign bit gets extended (ie. you end up with a negative int).

Either change CBuf to and unsigned char (as suggested) OR
cast it to unsigned char in the printf
eg.

print ("%02x",unsigned char(CBuf));

The best solution depends on what you subsequently want to do with CBuf.  If you are going to put in into a string, then you probably want to keep it a plain char (not make it unsigned) and just cast when you want to see the HEX value.

0

Featured Post

Highfive + Dolby Voice = No More Audio Complaints!

Poor audio quality is one of the top reasons people don’t use video conferencing. Get the crispest, clearest audio powered by Dolby Voice in every meeting. Highfive and Dolby Voice deliver the best video conferencing and audio experience for every meeting and every room.

Join & Write a Comment

Suggested Solutions

Title # Comments Views Activity
stack 22 155
Which checksum is this? 7 136
C++ check and remove last word from a string 5 116
Problem to save 10 121
This tutorial is posted by Aaron Wojnowski, administrator at SDKExpert.net.  To view more iPhone tutorials, visit www.sdkexpert.net. This is a very simple tutorial on finding the user's current location easily. In this tutorial, you will learn ho…
Summary: This tutorial covers some basics of pointer, pointer arithmetic and function pointer. What is a pointer: A pointer is a variable which holds an address. This address might be address of another variable/address of devices/address of fu…
The goal of this video is to provide viewers with basic examples to understand and use pointers in the C programming language.
The goal of this video is to provide viewers with basic examples to understand and use conditional statements in the C programming language.

708 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

17 Experts available now in Live!

Get 1:1 Help Now