All submissions

Convert Integer to Hex String Without sprintf or itoa functions

Code

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

typedef union
{
    uint16_t num;
    uint8_t arr[2];
}DTX_T;

void print_hex(uint16_t num) {

    DTX_T T;

    T.num=num;

    if(num>255)
    {
        printf("%X",T.arr[1]);
        printf("%X",T.arr[0]);
    }

    else
    {

        printf("%X",T.arr[0]);

    }
}

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

Solving Approach

 

 

 

Loading...

Input

255

Expected Output

FF