[英]Decoding styles in C#
By using the following code, I managed to decode a given hexadecimal string. 通过使用以下代码,我设法解码了给定的十六进制字符串。 In C#, using its library functions, I could decode a hexadecimal value in to ASCII, Unicode, Big-endian Unicode, UTF8, UTF7, UTF32. 在C#中,使用其库函数,我可以将十六进制值解码为ASCII,Unicode,Big-endian Unicode,UTF8,UTF7,UTF32。 Can you please tell me how can I convert a hexadecimal string in to other decoding styles such as ROT13, UTF16, western European, HFS Plus, etc.. 能否请您告诉我如何将十六进制字符串转换为其他解码样式,例如ROT13,UTF16,西欧,HFS Plus等。
{
string hexString = "68656c6c6f2c206d79206e616d6520697320796f752e";
byte[] dBytes = StringToByteArray(hexString);
//To get ASCII value of the hex string.
string ASCIIresult = System.Text.Encoding.ASCII.GetString(dBytes);
MessageBox.Show(ASCIIresult, "Showing value in ASCII");
//To get the Unicode value of the hex string
string Unicoderesult = System.Text.Encoding.Unicode.GetString(dBytes);
MessageBox.Show(Unicoderesult, "Showing value in Unicode");
}
public static byte[] StringToByteArray(String hex)
{
int NumberChars = hex.Length / 2;
byte[] bytes = new byte[NumberChars];
using (var sr = new StringReader(hex))
{
for (int i = 0; i < NumberChars; i++)
bytes[i] =
Convert.ToByte(new string(new char[2] { (char)sr.Read(), (char)sr.Read() }), 16);
}
return bytes;
}
You can get other Encoding objects by the method Encoding.GetEncoding , which accepts codepage or encoding name. 您可以通过该方法获得其他编码对象Encoding.GetEncoding ,它接受代码页或编码名称。 eg 例如
//To get the UTF16 value of the hex string
string UTF16Result = System.Text.Encoding.GetEncoding("utf-16").GetString(dBytes);
MessageBox.Show(UTF16Result , "Showing value in UTF16");
By using GetEncoding() 通过使用GetEncoding()
string utf16string = Encoding.GetEncoding("UTF-16").GetString(dBytes);
MessageBox.Show(utf16string , "Showing value in UTF-16");
Check out possible Code Page Decoding Styles. 查看可能的代码页解码样式。
And use this fragment to convert string to byte[] 并使用此片段将字符串转换为byte []
public static byte[] StringToByteArray(String hexstring)
{
var bytes= new byte[hexstring.Length / 2];
for (int i = 0, j = 0; i < hexstring.Length; i += 2, j++)
bytes[j] = Convert.ToByte(hexstring.Substring(i, 2), 0x10);
return bytes;
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.