Convert to hex

Apr 19, 2011 at 3:10pm
Hello everybody!
I have a big question about conversion from a base to a base.

Yesterday a write a mini program that comunicate throw UDP with a server.
When I receive information from server I must convert data to int, so I try to convert data to hex and than to unsigned int. I can't convert from data to hex.

That is what I create:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
char *ToHex(char *str)
{
    char *newstr = new char[(strlen(str)*2)+1];
    char *cpold = str;
    char *cpnew = newstr;

    while('\0' != *cpold)
    {
        sprintf(cpnew, "%02X", (char)(*cpold++));
        cpnew+=2;
    }
    *(cpnew) = NULL;
    return(newstr);
}


This code must convert string to hexadecimal.
All work fine, but this script doesn't convert to C1, or DA, it convert to numbers and to FF.

Can anyone help me?
Last edited on Apr 19, 2011 at 4:43pm
Apr 19, 2011 at 4:43pm
Up!
Apr 19, 2011 at 5:15pm
What does this data format look like??
Apr 19, 2011 at 5:48pm
One example: Ÿôe And my script convert like this 10 FFFFFF9F FFFFFFF4 65.
It must be 10 F9 F4 65

I resolve my problem. Was easy, but I'm happy now.

Last edited on Apr 19, 2011 at 6:08pm
Apr 19, 2011 at 6:13pm
What was it???

I was about to post something about sign extension (printing as negative hexadecimal numbers
for values >= 80 hex)
Apr 19, 2011 at 7:35pm
10 FFFFFF9F FFFFFFF4 65
cpnew+=(strlen(cpnew)-2);
That must be put at the end of the script.
Topic archived. No new replies allowed.