I am trying to convert the input ascii string to hex format using the format specifier “%.2X” . I have pasted the program at below which does the same. But, this program works in Linux server its giving improper hex output.
#include <iostream>
#include <cstring>
#include <string>
using namespace std;
int main ()
{
char *buffer = "<9e>¥/gÿbbbbABCD";
char *newBuffer = new char[strlen(buffer)*2 + 1];
for(int i = 0; i< strlen(buffer); ++i)
{
sprintf(newBuffer+(2*i), "%02x", buffer[i]);
//cout<<"\t"<<newBuffer;
}
cout<<"\t"<<newBuffer;
return 0;
}
o/p: 3c39653effff2f67ffff6262626241424344
for this extended ASCII char ¥ and ÿ is giving ffff and ffff.
Please tell me how to convert properly.
If we typecast the source string with
unsigned char, the problem gets solved: