Convertion Hex to Ascii

I want to convert Hex:390041 to ascii "90A",

39: 9
00: 0 -->Null
41: A

i am getting only "9" in the bytearray,i know 0 is terminating the array , could anybody tell me any alternate way .Please help me on this.

unsigned int bytes[3];
int j=0,counter=0;
char c[2];
unsigned char bytearray[4];
for(j=0;j<str.GetLength();j++)
{
if(0 == j%2)
{
c[0] = sSend[j];

c[1] = sSend[j+1];

sscanf(c, "%02x", &bytes[counter]);

bytearray[counter] =(unsigned char )bytes[counter];

counter++;
}
}

Last edited on
Perhaps this is what you're looking for.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
#include <iostream>
#include <sstream>
#include <string>


int main()
{
    int n1 = 0;
    std::cin >> n1;

    std::stringstream hex_number;
    hex_number << std::hex << n1;

    std::string dec_number;
    hex_number >> dec_number;

    std::cout << dec_number << std::endl;
    return 0;
}
Topic archived. No new replies allowed.