Send HEX data over TCP/IP

Hello everyone. I have this code:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
#include <iostream>
#include <string>

using namespace std;
string HexToAscii(string);

int main( )
{
    string ASCII;
    ASCII = HexToAscii("84FAEF0001EF053101010120F9C7DC798099F1F2F3");;
    cout << ASCII << endl;
    cout << ASCII.data() << endl;
    cout << ASCII.c_str() << endl;
    return 0;
}

string HexToAscii(string cad)
{
    int i = 0;
    int j = 0;
    wchar_t *numDec;
    wchar_t numHex[5];
    string numASCII;
    numHex[0] = '0';
    numHex[1] = 'x';
    numHex[4] = '\0';
    for(i = 0; i <= cad.length()-1; i+=2)
    {
        numHex[2] = cad[i];
        numHex[3] = cad[i + 1];
        numASCII += wcstol(numHex, &numDec, 16);
        j++;
    }
    return numASCII;
}


Im using String just for hex values like "00" or nulls that have problems with chars vars.

I been trying some like this:
 
nBytesSent = send(Socket, ASCII.c_str(), ASCII.length(), 0);


But the value in ASCII.data() or ASCII.c_str(), is not what i need, i need the value in ASCII... and when i try to convert it to Char i lose some data.

What can i do ???

Thanks you !
What do you mean by Hex? Is "84FAEF0001EF053101010120F9C7DC798099F1F2F3" a Hex value?
I mean hexadecimal...

84 FA EF 00 01 EF 05 31 01 01 01 20 F9 C7 DC 79 80 99 F1 F2 F3
That's different from the string you're using.

If we were to assume your numbers are 1 byte unsigned numbers, your declaration would be:
1
2
3
4
5
6
7
unsigned char value[] = {
    0x84, 0xFA, 0xEF, 0x00,
    0x01, 0xEF, 0x05, 0x31,
    0x01, 0x01, 0x01, 0x20,
    0xF9, 0xC7, 0xDC, 0x79
    0x80, 0x99, 0xF1, 0xF2,
    0xF3 };


Now you have your source data defined, take another look at what you've done.
As i said... if you have a char type, what ever you want, char, wchar, signed or unsigned, the hexadecimal code 0x00 is the end !

Try the code that i wrote with your var, is the same !

My problem is simple... i have a variable STRING Type, and i need to send the data to a tcp port. The function SEND, request a char parameter... but i have a string one.

Now, if i try to send the string.c_str() or if i try to convert the variable into any type of char, like wchar or unsigned char... some of the data lost in the transfer... that is becouse im sending somes null strings like DEC(05) or HEX(05).

Thanks for your answer.
It's not a string, you shouldn't be using string functions. Strings are zero termated, so you'd expect a string function to stop on zero.
Topic archived. No new replies allowed.