Reputation: 409
printf("%s", 0x216f6c6c6548); // "Hello!" reversed
It should print "Hello!" but it doesn't. I expected the same result of:
long long ll = 0x216f6c6c6548;
printf("%s", &ll);
or
printf("%s", "Hello!");
Is it possible to print a number as a string?
UPDATE
Is it possible to print directly a number as a string?
Upvotes: 0
Views: 130
Reputation: 154280
After accept answer
Easy to print directly. Use a compound literal to form the string.
#include <stdio.h>
#define SBprint(x) ((char [8+1]){ \
1ULL*x>>0 , 1ULL*x>> 8, 1ULL*x>>16, 1ULL*x>>24, \
1ULL*x>>32, 1ULL*x>>40, 1ULL*x>>48, 1ULL*x>>56, 0})
int main(void) {
printf("%s", SBprint(0x216f6c6c6548));
printf("%s", SBprint(0x65726120776F480A));
printf("%s", SBprint(0x0A3F756F7920));
}
Output
Hello!
How are you?
If your compiler use little endian, even simpler.
#define SBprint(x) ((union {long long ll[2]; char s[sizeof(long long)+1]; }){ {x, 0} }).s
Upvotes: 2
Reputation: 41200
You have to extract the characters from the number:
print_ull_as_str (unsigned long long ll)
{
while (ll != 0) { printf("%c", (char )(ll & 0xffu)); ll >>= 8; }
}
Upvotes: 6