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) {
    // Your logic here
   
      uint8_t pos = 3;
      while( pos > 0 && ((num >> (pos * 4))& 0xF) == 0){
        pos--;
      }

      for(int i = pos ; i >= 0; i--){
        uint8_t val = ((num >> (i * 4))& 0xF);
        if(val < 10){
            printf("%c", '0' + val );
        }
        else{
            printf("%c", 'A' + (val - 10 ));
        }
      }
    
}

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

Solving Approach

 

 

 

Loading...

Input

255

Expected Output

FF