How to print a unsigned char to a human readable format in c? - c

I would like to print the unsigned char in this format:
0xff. How can I do so? Thanks.

printf("0x%02X", value & 0xFF);
if you want to values to always be 2 characters. also "& 0xFF" will avoid some values to be printed with a lot of "F" in the front (happens sometimes)

%x is the basic conversion specifier for printing an unsigned int (that's what an unsigned char is converted to when passed to a variadic function, such as printf()) in hexadecimal format with lowercase letters. If you want leading zeroes and at least two digits, use %02x. If, in addition, you want the 0x prefix as well, prepend the # modifier. So your format string will look like
"%#02x"

printf("0x%x\n", variable);
Visit printf documentation page for more details

%x is the format specifier you are looking for.
printf("0x%x\n",your_char);

For chars I use this
printf("0x%hhx\n",your_char);

Related

printf short in hex with uppercase in C

I'm trying to printf unsigned short in hex, e.g. 0XFFFF. The problem is that I want a lowercase x not X. I have no idea how to do this.
fprintf(c,"%#06X,\n\t", pixel[i]));
Change the %X specifier to %x and you should get lower case:
fprintf(c, "%#06x\n", pixel[i]);
Outputs the number as 0xffff. If pixel is an array of unsigned shorts and you want the format to match the data, you can do:
fprintf(c, "%#06hx\n", pixel[i]);
If you really want 0xFFFF, then that's trivial:
fprintf(c, "0x%04hX\n", pixel[i]);

C Control Strings?

I know that in other languages such as Java when I use System.out.println(); and I put a variable inside of it like an int that holds the number 22 it will print 22 to the console.
In C if I do the same thing with printf(); I need to specify the type in the string such as printf("%d", n); I also know that Java has its own printf function.
What I am trying to get at here is how the C control String works compared to other languages such as Java where you don't have to provide the type identifier in the System.out.println(); and it automatically recognizes the variable is an int.
Is this part of C's way of efficiency and does it not actually check the type and rely's on the programmer to understand the type they are providing?
In fact, printf is neither efficient nor type-safe way of writing data.
There is a performance penalty because format string is parsed at compile time and type specific actions are chosen at run time as well, whereas in C++ and Java it can be done at compile time. Moreover, variadic arguments are forced to be passed on the stack, that is less efficient than passing them in registers.
And what is even more important is that printf is not type-safe. It is possible to pass any number of parameters of any types to printf, ignoring format string prescriptions. Of course, it can easily trigger undefined behavior.
The only reason for such behavior is that there is no function overloading in C.
On the other hand, it's not that bad. First, most of the compilers parse format string and issue a warning if it isn't consistent with parameters passed to printf. Second, aforementioned performance penalty is in fact negligible compared to the cost of formatting and printing text after types have been deduced.
Both C's predefined data types (integers, characters, etc.) as well as user defined types carry no type information with them at run-time. Thus if you use a 4 byte integer in your code, it occupies only 4 bytes in memory (disregarding any padding needed for alignment). This is great for efficiency reasons, but it means that functions that handle multiple data types (like printf) need to be told via the format/control string what the types of the arguments are.
So when printf receives a format string of "%d %f" it knows that the type of the first non-format argument is integer and the second argument is of type float.
By passing control characters in printf() we tell the compiler how to allocate the memory and what is the range of the data that we wanna to print.
Control strings are also known as format specifiers. They are used in formatted input and output operations of the data. Remember, format specifiers are used for formatting the data, for example, in printf() and scanf().
Control strings in scanf() are used to transfer the data to the processor's memory in a formatted way, whereas printf () transfers the data to the output device e.g. monitor screen in a formatted way.
Some common Format Specifierss are listed below:
________________________________________________________________
FORMAT SPECIFIER :: DESCRIPTION
________________________________________________________________
%c Character
%d Signed Integer
%e or %E Scientific notation
%f Floating point
%g or %G. Similar as %e or %E
%hi. Signed Integer
%hu Unsigned Integer(Short)
%i Signed Integer
%l or %ld or %li Signed Integer
%lf Floating point
%Lf Floating point
%lu Unsigned integer
%lli, %lld Signed long long int
%llu. unsigned long long int
%o. Octal representation of Integer.
%p Address of pointer to void void *
void *
%s String char *
%u Unsigned int
Unsigned short int
%n Prints nothing
%% Prints % character
%o Octal representation
%p Address of pointer to void void *
void *
%s String char *
%u Unsigned Integer
%x or %X Hexadecimal representation of
Unsigned Int.
%n Prints nothing
%% Prints % character
________________________________________________________________

What is the meaning of the format control specifier %016I64X in sprintf_s

What is the meaning of the format control specifier "%S\%016I64X%S" in this sprintf_s command ?
As far as I know, it defines a string which converts numbers to unsigned 64 bit integer in Hexadecimal format. I would like to know whether I am right ? Please help me..
char lFileName[MAX_PATH];
sprintf_s( lFileName, MAX_PATH, "%S\\%016I64X%S", mSavePath.GetBuffer(),aBuffer->GetTimestamp(), lExt );
First, it looks like a Visual C++ usage of
int sprintf_s(char *buffer, size_t sizeOfBuffer, const char *format, ...);
The format consists of multiple directives: "%S", "\\", "%016I64X", "%S".
"%S" "When used with printf functions, specifies a wide-character string; ..." more
"\\" is simply a \.
"%016I64X" is an X format specifier of hexadecimal output. 0 to indicate zero-filling as needed. 16 to indicate the minimum output length. I64 is a windows specific modifier indicating the expected integer is of windows specific type unsigned __int64. more
You are on the right track with "unsigned 64 bit integer".

Byte order (endian) of int in NSLog?

NSLog function accepts printf format specifiers.
My question is about %x specifier.
Does this print hex codes as sequence on memory? Or does it have it's own printing sequence style?
unsigned int a = 0x000000FF;
NSLog(#"%x", a);
Results of above code on little or big endian processors are equal or different?
And how about NSString's -initWithFormat method? Does it follows this rule equally?
%x always prints the most significant digits first. Doesn't matter what kind of processor it is running on.

C - Convert long int to signed hex string

MASSIVE EDIT:
I have a long int variable that I need to convert to a signed 24bit hexadecimal string without the "0x" at the start. The string must be 6 characters followed by a string terminator '\0', so leading zeros need to be added.
Examples:
[-1 -> FFFFFF] ---
[1 -> 000001] ---
[71 -> 000047]
Answer
This seems to do the trick:
long int number = 37;
char string[7];
snprintf (string, 7, "%lX", number);
Because you only want six digits, you are probably going to have to do some masking to make sure that the number is as you require. Something like this:
sprintf(buffer, "%06lx", (unsigned long)val & 0xFFFFFFUL);
Be aware that you are mapping all long integers into a small range of representations. You may want to check the number is in a specific range before printing it (E.g. -2^23 < x < 2^23 - 1)
Look at sprintf. The %lx specifier does what you want.
Use itoa. It takes the desired base as an argument.
Or on second thought, no. Use sprintf, which is standard-compliant.
In the title you say you want a signed hex string, but all your examples are unsigned hex strings. Assuming the examples are what you want, the easiest way is
sprintf(buffer, "%06X", (int)value & 0xffffff);

Resources