convert decimal to octal and hex in C with putchar() and >>

The name of the pictureThe name of the pictureThe name of the pictureClash Royale CLAN TAG#URR8PPP


convert decimal to octal and hex in C with putchar() and >>



In the example, I have this following code that can convert a decimal to binary.


void show_bin(unsigned short int byte)
{
for (int shift=15; shift>=0; --shift) {
putchar((byte>>shift)&1 ? '1' : '0');
}
}



How can I follow this format and write code for converting decimals to octal and hex?



For example, I would like to convert 65066 to 0177052 and 0XFE2A.


65066


0177052


0XFE2A





hint: >> is define as divide by 2...
– Stargateur
4 mins ago


>>





What details is it you need help with? How many bits there are to represent an octal digit? How to make a lookup table for more than two entries? How to use printf with format specifiers to influence the representation?
– Yunnosch
4 mins ago





Why do you want to do it this way instead of using printf with specifiers o and x ? If this is a homework assignment (the usual reason for doing things in an unnecessarily complicated way), then pleas say so and indicate whether you would appreciate help according to the compromise described here: meta.stackoverflow.com/questions/334822/…
– Yunnosch
1 min ago


o


x









By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

Popular posts from this blog

Makefile test if variable is not empty

Will Oldham

Visual Studio Code: How to configure includePath for better IntelliSense results