Home > Back-end >  Turn cstrings unsigned char [] array or char [] array - more urgent, thank you
Turn cstrings unsigned char [] array or char [] array - more urgent, thank you

Time:09-16

Serial port is used to collect data, is to use hexadecimal transmission, using cstrings receiving, carries on the transformation cstrings strbuf="55511400 efff0808f809b95552000000000000f809a85553aeff9cff53caf8090e5554f2f5ba0227e9f8095d
"Converted to unsigned charTemp []={80} 55,51,14,00, EF, F0, and finally get charTemp [0]=55, charTemp [1]=51, like this, but after the hexadecimal data using cstrings receiving, will change? Return to char [], like charTemp [0] is 55 - string, or zero x55,

The static unsigned char chrTemp [2000].//define the buffer
TCHAR sz2 [3]={0};
Int nLen=m_EditRev. GetLength ()/2;
For (int j=0; J & lt; NLen; J++)
{
Sz2 [0]=m_EditRev [j * 2];
Sz2 [1]=m_EditRev [j * 2 + 1);

_stscanf (sz2, _T (" % s "), & amp; ChrTemp [j]);
}
Debug:
M_EditRev value is 55511400 f2ff0a
CharTemp 0 x001a3fb8 "5510 ff0010d550"//the resulting value is not the same as
Sz2 value has a problem

CodePudding user response:

Based on my experience, when you receive should not be using cstrings,
Design data transmission, the binary stream generally recommended, or you can be regarded as a hex value,
When you receive, also should adopt the corresponding binary array, so the best,
Only responsible for the display, you may need to translate into character form,

CodePudding user response:

Hello, I also want to use the char [] array receiving to come in, but I am using MFC MSComm to receive, see there seems to be a useless char to receive, module of data is the hexadecimal! This is I received the code
If (m_mscom get_CommEvent ()==2)
{
Cstrings STR, str1;
Cstrings strtemp;
Unsigned char chrBuffer [2000]={0};
Long k;
Short usLength=0, usCnt=0, uklength=0;
The VARIANT InputData=https://bbs.csdn.net/topics/m_mscom.get_Input ();
COleSafeArray fs.
Fs=InputData;
Uklength=fs. GetOneDimSize ();
For (k=0; K & lt; Uklength; K++)//error
{
Char bk [2000]={0};
Fs. GetElement (& amp; K, chrBuffer + k);//to write data into the array
Unsigned char bt=* (char *) (chrBuffer + k);//character


If (m_ctrlHexReceive GetCheck ())
{
Strtemp. The Format (_T (" % 02 x "), bt);//in hexadecimal characters into temporary variables strtemp store, add an empty partition can display the
//memcpy (& amp; Bk, & amp; Strtemp, strtemp. GetLength ());
//USES_CONVERSION;
//strcpy (bk, T2A (strtemp));
The else
Strtemp. The Format (_T (" % c "), bt);//the character into temporary variables strtemp store
M_EditRev +=strtemp;//add receive corresponding string edit box
}
UpdateData(false);
}

CodePudding user response:

You this is very simple, you transfer is the number of sets of byte string

CodePudding user response:

Efff0808f809b95552000000000000f809a85553aeff9cff53caf8090e5554f2f5ba0227e9f8095d cstrings strbuf="55511400"
Converted to unsigned charTemp []={80} 55,51,14,00, EF, F0, and finally get charTemp [0]=55, charTemp [1]=51,

Here you have a problem description itself, cstrings string into unsigned char should be {' 5 ', '5', '5', '1',... }, converted to digital description is {53,53,53,49,... }, is not {55,51,14, you wrote... }
  • Related