Home > Back-end >  In C builder how to implement convert hexadecimal string data, such as string is "9 f," co
In C builder how to implement convert hexadecimal string data, such as string is "9 f," co

Time:10-18

In c + + builder how to implement convert hexadecimal string data, such as string is "9 f," convert hexadecimal value is 0 x9f; String is "af", convert hexadecimal value is 0 xaf?
The code is as follows:
 void __fastcall TForm1: : Button3Click (TObject * Sender) 
{
AnsiString Temp.
Char * SendData publishes the event;
Int ln.
Unsigned long LRC England BS;
If (hComm==0) return;//check the Handle value
Temp=Memo1 - & gt; The Text;//send the string to convert Temp to hexadecimal
SendData=https://bbs.csdn.net/topics/Temp.c_str ();//string
//send string data
BS=Temp. Length ();
//BS=StrLen (SendData publishes the event);//can also use this way get the length of the string
//the actual sending action
WriteFile (hComm SendData publishes the event, BS, & amp; LRC England, NULL);//actually send data
}

CodePudding user response:

SendData=https://bbs.csdn.net/topics/Temp.c_str ();
The use of such way is serious is not recommended to use,

String hexadecimal, like this:
String STR="9 f";
DWORD dw=StrToInt (" 0 x "+ STR);

CodePudding user response:

Hexadecimal conversion, with the demon elder brother learning, finally took down StrToInt (" 0 x "+ STR);

CodePudding user response:

reference 1st floor ccrun response:
SendData=https://bbs.csdn.net/topics/Temp.c_str ();
The use of such way is serious is not recommended to use,

String hexadecimal, like this:
String STR="9 f";
DWORD dw=StrToInt (" 0 x "+ STR);

So, spot bamboo, the complete code is so write (I tried it on, as if not receive data?) :
 void __fastcall TForm1: : Button3Click (TObject * Sender) 
{
AnsiString Temp.
//char * SendData publishes the event;
Int ln.
DWORD dw.
Unsigned long LRC England BS;
If (hComm==0) return;//check the Handle value
Temp=Memo1 - & gt; The Text;//send the string to convert Temp to hexadecimal
//SendData=https://bbs.csdn.net/topics/Temp.c_str ();//string
//send string data
Dw=StrToInt (" 0 "x + Temp);
BS=dw. Length ();
//BS=StrLen (SendData publishes the event);//can also use this way get the length of the string
//the actual sending action
WriteFile (hComm, dw, BS, & amp; LRC England, NULL);//actually send data
}

CodePudding user response:

To be clear, you want to send data, exactly is "9 f" two bytes, or 0 x9f a byte of data?

CodePudding user response:

reference 4 floor ccrun response:
you clear, first to send data, exactly is "9 f" two bytes, or 0 x9f a byte of data?

Send 0 x9f a byte (hexadecimal number, converted to binary 10011111 8 a byte) data, the code is wrong?

CodePudding user response:

reference 4 floor ccrun response:
you clear, first to send data, exactly is "9 f" two bytes, or 0 x9f a byte of data?

Oh, demon elder brother, I know, length () can only be used in the type string, can't use on a DWORD dw, that if it is, in the code:
Dw=StrToInt (" 0 "x + Temp);
BS=dw. Length ();
//BS=StrLen (SendData publishes the event);//can also use this way get the length of the string
//the actual sending action
WriteFile (hComm, dw, BS, & amp; LRC England, NULL);//actual sending data
The above BS=dw. The Length () and the WriteFile (hComm, dw, BS, & amp; LRC England, NULL) how should change?

CodePudding user response:

Early to say.

 AnsiString Temp="9 f"; 
Byte bt=(byte) StrToInt (" 0 "x + Temp);
WriteFile (hComm, & amp; Bt, 1, & amp; LRC England, NULL);

CodePudding user response:

Learning, StrToInt, string to an integer

CodePudding user response:

"Convert hexadecimal value is 0 x9f",
The building Lord says so, it is just rubbish, converted to an integer bai, hexadecimal number is a kind of writing,

CodePudding user response:

Seems to like me, do is PC work

CodePudding user response:

 
AnsiString Temp="9 f";
Byte bt=(byte) StrToInt (" 0 "x + Temp);
WriteFile (hComm, & amp; Bt, 1, & amp; LRC England, NULL);

Demon elder brother of this method is very good, but if
AnsiString Temp="* 9 f";
Transformation will go wrong, the following methods are available, and with fault-tolerant
 
Char * p="a0";
Char * STR;//error when pointing error character
Long I=strtol (p, & amp; STR, 16);
ShowMessage (I);
  • Related