All submissions

Convert Integer to Hex String Without sprintf or itoa functions

Code

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

void print_hex(uint16_t num) {
    char hex[5];
    int i = 0;
    if(num == 0) printf("%d",0);
    while(num > 0){
        uint8_t digit = num%16;
        if(digit < 10) hex[i++] = '0' + digit;
        else hex[i++] = 'A' + (digit - 10);
        num /= 16;
    }
    
    //Print in Reverse
    for(int j = i-1; j >= 0; j--) {
        printf("%c",hex[j]);
    }
}

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

Solving Approach

 

 

 

Loading...

Input

255

Expected Output

FF