Requirements:
How to convert a hexadecimal string cstring to a byte [] Array
Cstring STR = "56 45 52 30 30 ";
Byte Buf [64];
After assignment
Buf [64] = {0x56,0x45,0x52,0x30,0x30 };
Int ccp210xserialtestdlg: str2hex (cstring STR, unsigned char * Data)
{
Unsigned int T;
Int length;
Int Len = Str. getlength ();
Char A [10] = {0 };
Length = 0;
For (INT I = 0; I <Len ;)
{
If (STR [I] = '') // Space
{
I ++;
Continue;
}
A [0] = STR [I]; // high byte
I ++;
If (I> = Len)
{
Break;
}
A [1] = STR [I]; // low byte
A [2] = '/0'; // string end flag
T = strtoul (A, null, 16); // converts a string to an unsigned long integer.
Data [length] = (unsigned char) T;
Length ++;
I ++;
}
Return length; // The number of converted bytes.
}
There are many ways to deal with such problems. My method features the use of the strtoul () function.
Although it is feasible, I still have doubts. Can the cstring class be accessed directly using the array underlying method (STR [I? I feel that I cannot do this, but can solve the problem. I am wondering. I hope to have a finger at it. Thank you very much.
Fu shaobing
2010-11-11