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.
Related
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]);
I have a simple program :
#include <stdio.h>
int main()
{
long i = 16843009;
printf ("%02x \n" ,i);
}
I am using %02x format specifier to get 2 char output,
However, the output I am getting is:
1010101
while I am expecting it to be :01010101 .
%02x means print at least 2 digits, prepend it with 0's if there's less. In your case it's 7 digits, so you get no extra 0 in front.
Also, %x is for int, but you have a long. Try %08lx instead.
%x is a format specifier that format and output the hex value. If you are providing int or long value, it will convert it to hex value.
%02x means if your provided value is less than two digits then 0 will be prepended.
You provided value 16843009 and it has been converted to 1010101 which a hex value.
Your string is wider than your format width of 2. So there's no padding to be done.
You are actually getting the correct value out.
The way your x86 (compatible) processor stores data like this, is in Little Endian order, meaning that, the MSB is last in your output.
So, given your output:
10101010
the last two hex values 10 are the Most Significant Byte (2 hex digits = 1 byte = 8 bits (for (possibly unnecessary) clarification).
So, by reversing the memory storage order of the bytes, your value is actually: 01010101.
Hope that clears it up!
I have the following code:
void main()
{
char tmp[3]= "AB";
short k;
memcpy(&k,tmp,2);
printf("%x\n", k);
}
In ASCII, the hex value of char 'A' is 41 and the hex value of char 'B' is 42. Why is the result of this program 4241? I think the correct result is 4142.
You are apparently running this on a "little-endian" machine, where the least significant byte comes first. See http://en.wikipedia.org/wiki/Endianness.
Your platform stores less significant bytes of a number at smaller memory addresses, and more significant bytes at higher memory addresses. Such platforms are called little-endian platforms.
However, when you print a number the more significant digits are printed first while the less significant digits are printed later (which is how our everyday numeric notation works). For this reason the result looks "reversed" compared to the way it is stored in memory on a little-endian platform.
If you compile and run the same program on a big-endian platform, the output should be 4142 (assuming a platform with 2-byte short).
P.S. One can argue that the "problem" in this case is the "weirdness" of our everyday numerical notation: we write numbers so that the significance of their digits increase in right-to-left direction. This appears to be inconsistent in the context of societies that write and read in left-to-right direction. In other words, it in not the little-endian memory that is reversed. It is the way we write numbers that is reversed.
Your system is little-endian. That means that a short (16-bit integer) is stored with the least significant byte first, followed by the most significant byte.
The same goes for larger integers. The following code would result in "44434241".
void main()
{
char tmp[5]= "ABCD";
int k;
memcpy(&k,tmp,4);
printf("%x\n", k);
}
I reading a C book. To print out a memory address of a variable, sometimes the book uses:
printf("%u\n",&n);
Sometimes, the author wrote:
printf("%d\n",&n);
The result is always the same, but I do not understand the differences between the two (I know %u for unsigned).
Can anyone elaborate on this, please?
Thanks a lot.
%u treats the integer as unsigned, whereas %d treats the integer as signed. If the integer is between 0 an INT_MAX (which is 231-1 on 32-bit systems), then the output is identical for both cases.
It only makes a difference if the integer is negative (for signed inputs) or between INT_MAX+1 and UINT_MAX (e.g. between 231 and 232-1). In that case, if you use the %d specifier, you'll get a negative number, whereas if you use %u, you'll get a large positive number.
Addresses only make sense as unsigned numbers, so there's never any reason to print them out as signed numbers. Furthermore, when they are printed out, they're usually printed in hexadecimal (with the %x format specifier), not decimal.
You should really just use the %p format specifier for addresses, though—it's guaranteed to work for all valid pointers. If you're on a system with 32-bit integers but 64-bit pointers, if you attempt to print a pointer with any of %d, %u, or %x without the ll length modifier, you'll get the wrong result for that and anything else that gets printed later (because printf only read 4 of the 8 bytes of the pointer argument); if you do add the ll length modifier, then you won't be portable to 32-bit systems.
Bottom line: always use %p for printing out pointers/addresses:
printf("The address of n is: %p\n", &n);
// Output (32-bit system): "The address of n is: 0xbffff9ec"
// Output (64-bit system): "The address of n is: 0x7fff5fbff96c"
The exact output format is implementation-defined (C99 §7.19.6.1/8), but it will almost always be printed as an unsigned hexadecimal number, usually with a leading 0x.
%d and %u will print the same results when the most significant bit is not set. However, this isn't portable code at all, and is not good style. I hope your book is better than it seems from this example.
What value did you try? The difference unsigned vs. signed, just as you said you know. So what did it do and what did you expect?
Positive signed values look the same as unsigned so can I assume you used a smaller value to test? What about a negative value?
Finally, if you are trying to print the variable's address (as it appears you are), use %p instead.
All addresses are unsigned 32-bit or 64-bit depending on machine (can't write to a negative address). The use of %d isn't appropriate, but will usually work. It is recommended to use %u or %ul.
There is no such difference ,just don't get confused if u have just started learning pointers.
%u is for unsigned ones.And %d for signed ones
hi i am reading a binary file using c as shown here link text
so that all the information read from binary file is stored in "char *buffer".
i have the format standard where it says that one of the lines should be
format: unsigned char, size: 1 byte
i am doing the following:
printf("%x\n", buffer[N]);
but what should i do when the format says:
format: unsigned short, size: 2 bytes
if i do it as follows, would this be correct:
printf("%d%d\n", buffer[N], buffer[N+1]);
if not can you show me the correct way?
Also can you tell me if the following are correct way while printing:
char %c
unsigned long %ul
unsigned short %d
unsigned char %x
double %f
long %ld
all of the data in binary file is in little-endian format! thanks a lot in advance!
Try printf("%d", (short)(buffer[N] + buffer[N+1]<<8)). Now notice that I had to assume that the byte order in the buffer had the least significant byte of the two-byte short stored at the lower address.
I could likely have written *(short *)(&buffer[N]), but that assumes that N has the right alignment to hold a short on your platform, and that the buffer and the platform agree on byte order.
This is actually just the tip of a very large iceberg of a topic. There are many subtle issues lurking, and some really unsubtle ones when you wander into floating point values.