What this function (hex_str) does is, it receives a 1-byte char as argument, and returns its hex representation as string.
I'd like some tips on how to improve my code.
Code:
#include <iostream>
#include <string>
#include <stdio.h>
using namespace std;
char first_4_bits_hex(char value)
{
char hex_value[] = {'0','1','2','3','4','5','6','7','8','9','A','B','C','D','E','F'};
int count = 0;
for(int x = 1; x <= 8; x = (x << 1))
{
if(value & x)
count += x;
}
return hex_value[count];
}
string hex_str(char value)
{
string return_str="00";
return_str[1] = first_4_bits_hex(value);
value = value >> 4;
return_str[0] = first_4_bits_hex(value);
return return_str;
}
int main()
{
string str = hex_str(0x10);
cout<<str;
return 0;
}