All submissions

Convert Integer to Hex String Without sprintf or itoa functions

Code

#include <stdio.h>
#include <stdint.h>

char alphabet[17] = "0123456789ABCDEF";

void print_hex(uint16_t num)
{
    if (num >> 12 & 0xf) printf("%c", alphabet[num >> 12 & 0xf]);
    if (num >> 8 & 0xf) printf("%c", alphabet[num >> 8 & 0xf]);
    if (num >> 4 & 0xf) printf("%c", alphabet[num >> 4 & 0xf]);
    if (num == 0 || num >> 0 & 0xf) printf("%c", alphabet[num >> 0 & 0xf]);
}

int main() {
    uint16_t num;
    scanf("%hu", &num);
    print_hex(num);
    return 0;
}

Solving Approach

 

 

 

Loading...

Input

255

Expected Output

FF