You are seeing the ffffff
because char
is signed on your system. In C, vararg functions such as printf
will promote all integers smaller than int
to int
. Since char
is an integer (8-bit signed integer in your case), your chars are being promoted to int
via sign-extension.
Since c0
and 80
have a leading 1-bit (and are negative as an 8-bit integer), they are being sign-extended while the others in your sample don’t.
char int c0 -> ffffffc0 80 -> ffffff80 61 -> 00000061
Here’s a solution:
char ch = 0xC0; printf("%x", ch & 0xff);
This will mask out the upper bits and keep only the lower 8 bits that you want.
Related Posts:
- printf() formatting for hexadecimal
- How to display hexadecimal numbers in C?
- How to convert decimal to hexadecimal in JavaScript
- How many spaces for tab character(\t)?
- What is a string of hexadecimal digits?
- Correct format specifier for double in printf
- Scanf/Printf double variable C
- Strings and character with printf
- What is the argument for printf that formats a long?
- What is the printf format specifier for bool?
- How to printf “unsigned long” in C?
- How I can print to stderr in C?
- Is there a printf converter to print in binary format?
- What does “%.*s” mean in printf?
- How to print a char array in C through printf?
- How to printf a memory address in C
- Data argument not used by format strings in C
- How can one print a size_t variable portably using the printf family?
- %i or %d to print integer in C using printf()?
- How can you print multiple variables inside a string using printf?
- error: aggregate value used where an integer was expected
- Why are hexadecimal numbers prefixed with 0x?
- How to print an unsigned char in C?
- How to tell if hex value is negative?
- C’s printf and fprintf(stdout,) are not printing
- Is there a way to have printf() properly print out an array (of floats, say)?
- What do numbers using 0x notation mean?
- How do you allow spaces to be entered using scanf?
- How to write to a file using open() and printf()?
- Using floats with sprintf() in embedded C
- Printf width specifier to maintain precision of floating-point value
- Which of sprintf/snprintf is more secure?
- Does C have a string type?
- Execution of printf() and Segmentation Fault
- printf format specifiers for uint32_t and size_t
- How to repeat a char using printf?
- Two decimal places using printf( )
- How do you format an unsigned long long int using printf?
- What primitive data type is time_t? [duplicate]
- Reading float using scanf in c
- How to format strings using printf() to get equal length in the output
- Reading float using scanf in c
- How to format strings using printf() to get equal length in the output
- What do \t and \b do?
- hexadecimal converting back into decimal
- write() to stdout and printf output not interleaved?
- What is the difference between ++i and i++?
- Stack smashing detected
- Why am I getting “void value not ignored as it ought to be”?
- Pointer Arithmetic
- Openssl : error “self signed certificate in certificate chain”
- How to generate a random int in C?
- C pointers and arrays: [Warning] assignment makes pointer from integer without a cast
- How to print the array?
- How to solve the error: assignment to expression with array type
- segmentation fault : 11
- What does “1e” mean?
- Getting “conflicting types for function” in C, why?
- how to use uint64_t in C [duplicate]
- How do I solve the following errors: “Undefined reference to WinMain”, “[Error] Id returned 1 exit status”?
- What is the LD_PRELOAD trick?
- Using %s in C correctly – very basic level
- How do you make an array of structs in C?
- Is there a function to copy an array in C/C++?
- C error: Expected expression before int
- Returning string from C function
- Need a good hex editor for Linux
- Char Comparison in C
- Converting Hexadecimal String to Decimal Integer
- C read file line by line
- Copying a part of a string (substring) in C
- Incompatible implicit declaration of built-in function ‘malloc’
- expression must have integral type
- Quick Way to Implement Dictionary in C
- gdb: “No symbol table is loaded”
- C: error: expected ‘)’ before ‘;’ token
- execv vs execvp, why just one of them require the exact file’s path?
- difference between
and - Writing binary number system in C code
- OpenGL — GL_LINE_LOOP —
- Valgrind complains with “Invalid write of size 8”
- Read and write to binary files in C?
- Expression must be a pointer to a complete object type using simple pointer arithmetic
- Difference between sizeof(char) and sizeof(char *)
- How can I do GUI programming in C?
- Preventing console window from closing on Visual Studio C/C++ Console application
- What is the proper equivalent of “while(true)” in plain C?
- How do I compile the asm generated by GCC?
- What is the difference between %g and %f in C?
- What is the source of the data for the ProgramFiles, ProgramW6432Dir, ProgramFilesDir (x86), CommonProgramFiles environment variables?
- Structure padding and packing
- warning: return makes pointer from integer without a cast but returns integer as desired
- Dynamic vs static array in c
- Suggestions needed: Effective Java to C source code converter
- The difference between char * and char[] [duplicate]
- How to clear input buffer in C?
- In C, what exactly happens when you pass a NULL pointer to strcmp()?
- how to stop a loop arduino
- How to normalize a mantissa
- Compiler warning – suggest parentheses around assignment used as truth value