Home > Net >  About hexadecimal and ASCII mutual problem solving?
About hexadecimal and ASCII mutual problem solving?

Time:09-20

Demand is very simple, a text box, two CheckBox, HEX and ASCII respectively,
So when the model is now HEX, input text 81 30 (note that this is the hexadecimal, click the ASCII option, will automatically be converted to ASCII code, but only one character string of conversion,
Is the "?" , cause I'll into HEX, 30 and 81 not corresponding!

For general characters, such as hex: 30, 31 into ASCII is 10, transfers are do not have what problem, is 81 this cannot resolve the characters, how to deal with the effect, can be realized just who is going to challenge,

Pictures of others to achieve effect, after converted to ASCII apparently only one question mark this one character, but into hex can smoothly switch for the 81 30 two characters,

Your own code: tried many methods are not
Your code will only appear? Turn again, if it becomes 3 f

 private void rbnChar_Tx_CheckedChanged (object sender, EventArgs e) 
{
String strTemp=String. The Empty, strConv=String. The Empty;
If (rbnHEX_Tx. Checked)
{
Byte [] byteBuf=System. Text. Encoding. The Default. The GetBytes (tbxSendData. Text);
If (byteBuf. Length & gt; 0)
{
for (int i=0; i {
StrTemp=the Convert. ToString (byteBuf [I], 16);
StrTemp=strTemp. PadLeft (2 '0'); ;
StrConv +=strTemp. The ToUpper ();
StrConv +="";
}
TbxSendData. Text=strConv;
}
}
The else
{
If (tbxSendData. Text. Length & gt; 0)
{
StrTemp=tbxSendData. Text. Replace (" ", "");
If (strTemp. Length % 2!=0)/do/odd '0'
{
StrTemp=strTemp. Insert (strTemp. Length - 1, "0");
}
//Byte [] byteBuff=strToHexBytes (strTemp);
//strConv=System. Text. Encoding. The Default. Get string (byteBuff);
//tbxSendData. Text=strConv;
Byte [] buff=new byte [strTemp. Length/2];
Int index=0;
for (int i=0; i {
Buff [index]=the Convert. ToByte (strTemp. Substring (I, 2), 16);
+ + index;
}
The string result=Encoding. The Default. Get string (buff);
TbxSendData. Text=the result;

}

}
}


CodePudding user response:

Seven, more than 81 ASCII string is missing top, you can't use string to hold the intermediate results, internal use real byte array to save, hex and ASCII is displayed
  •  Tags:  
  • C#
  • Related