All submissions

Convert Integer to Hex String Without sprintf or itoa functions

Code

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


const char hex_chars [] = "0123456789ABCDEF"; 

void print_hex(uint16_t num) {
    // Your logic here
    char buff[5];
    buff[4] = '\0';
    if(num == 0)
    {
        printf("0\n");
    }

    int index = 3;
    while(num > 0)
    {
        buff[index] =  hex_chars[(num & 0xF)];
        index--;
        num >>= 4;
    }

    printf("%s\n", &buff[index+1]);

}

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

Solving Approach

 

 

 

Loading...

Input

255

Expected Output

FF