printing hex values for chars

Posted on 2011-05-03
Last Modified: 2012-05-11
I need to send a packet that contains the following 3 bytes to another program.

The bytes are the hex value 0x202

I wanted to store the value in an integer and then copy the first 3 bytes of the integer
into a buffer for transmission.

However when I print out the buffer contents I get the hex values 220 and not 202

I also tried using a constant char buffer set to 0x202 and this works, but I'm a little bit
confused why the integer method didn't work and I would like to figure this out.

The output for the function attached is

ID == 514  D(hex) == 202
BYTE[0]  == 2 // why does this print 220 and not 202?
BYTE[1]  == 2
BYTE[2]  == 0

CONST_CHAR[0]  == 2
CONST_CHAR[1]  == 0
CONST_CHAR[2]  == 2

void printHex()
	const unsigned char CONST_CHAR[] = "\x2\x0\x2";
	const unsigned int ID = 0x202;
	unsigned char buf[3];


	printf("ID == %d  D(hex) == %x \n",ID,ID);
	for(int i =0; i < sizeof(buf);i++){
		printf("BYTE[%d]  == %x \n",i,buf[i]);

	for(int i =0; i < sizeof(CONST_CHAR)-1;i++){

		printf("CONST_CHAR[%d]  == %x \n",i,CONST_CHAR[i]);


Open in new window

Question by:dev110
    LVL 53

    Expert Comment

    0x202 is only two bytes : the byte 0x02 and the byte 0x02.

    So, use :

    >> const unsigned char CONST_CHAR[] = "\x02\x02";

    and send both bytes.
    LVL 53

    Accepted Solution

    It's better to initialize the array like this though :

            const unsigned char CONST_CHAR[] = { 0x02, 0x02 };

    just to avoid problems with null bytes being interpreted as the end of a string, and to make it explicit that you're dealing with unsigned char values, and not with char's.

    Author Comment

    I knew it would be something simple.
    I should have done
    const unsigned int ID = 0x00020002;


    const unsigned int ID = 0x202;

    Thanks for both of your comments

    Featured Post

    How your wiki can always stay up-to-date

    Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
    - Increase transparency
    - Onboard new hires faster
    - Access from mobile/offline

    Join & Write a Comment

    C++ Properties One feature missing from standard C++ that you will find in many other Object Oriented Programming languages is something called a Property (…
    Basic understanding on "OO- Object Orientation" is needed for designing a logical solution to solve a problem. Basic OOAD is a prerequisite for a coder to ensure that they follow the basic design of OO. This would help developers to understand the b…
    Video by: Grant
    The goal of this video is to provide viewers with basic examples to understand and use while-loops in the C programming language.
    The viewer will learn how to use the return statement in functions in C++. The video will also teach the user how to pass data to a function and have the function return data back for further processing.

    754 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