# [Solved] how to convert char in octal format without standart C functions like print(f) [closed]

Decimal and octal are number bases, this only matters when presenting a number (or when inputing a number); the actual number is generally stored in binary in the computer’s memory but this doesn’t often matter to typical programs.

In your case, conversion to octal generally involves extracting three bits at a time (since 23 is 8). This can be done by masking with the mask `7` (binary `111`), and then shifting. This will get the octal digits in right-to-left order.

Assuming a `char` of 8 bits (which is typical), you’re going to be generating 3 octal digits since 3 * 3 = 9. Two digits would only cover 6 bits which is clearly not enough.

That’s so few, you can actually “unroll” it:

``````void to_octal(char *buf, unsigned char x)
{
buf[0] = '0' + ((x >> 6) & 7);
buf[1] = '0' + ((x >> 3) & 7);
buf[2] = '0' + (x & 7);
buf[3] = '\0';
}
``````

Note that the above requires `buf` to have at least four characters of space available. Usage:

``````const unsigned char x = 212;
char buf[8];

to_octal(buf, x);
printf("%d is %s in octal\n", x, buf);
``````

solved how to convert char in octal format without standart C functions like print(f) [closed]