C# C语言中的解码风格#
通过使用以下代码,我成功地解码了给定的十六进制字符串。在C#中,使用其库函数,我可以将十六进制值解码为ASCII、Unicode、大端Unicode、UTF8、UTF7、UTF32。请告诉我如何将十六进制字符串转换为其他解码样式,如ROT13、UTF16、西欧、HFS Plus等C# C语言中的解码风格#,c#,decoding,C#,Decoding,通过使用以下代码,我成功地解码了给定的十六进制字符串。在C#中,使用其库函数,我可以将十六进制值解码为ASCII、Unicode、大端Unicode、UTF8、UTF7、UTF32。请告诉我如何将十六进制字符串转换为其他解码样式,如ROT13、UTF16、西欧、HFS Plus等 { string hexString = "68656c6c6f2c206d79206e616d6520697320796f752e"; byte[] dBytes = StringToByteArra
{
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;
}
{
字符串hexString=“68656C6F2C206D79206E616D6520697320796F752E”;
字节[]dBytes=StringToByteArray(十六进制字符串);
//获取十六进制字符串的ASCII值。
字符串ASCIIresult=System.Text.Encoding.ASCII.GetString(dBytes);
Show(ASCIIresult,“以ASCII格式显示值”);
//获取十六进制字符串的Unicode值
string unicodesult=System.Text.Encoding.Unicode.GetString(dBytes);
Show(unicodesult,“以Unicode显示值”);
}
公共静态字节[]StringToByteArray(字符串十六进制)
{
int numbercars=十六进制长度/2;
byte[]bytes=新字节[numbercars];
使用(var sr=新的StringReader(十六进制))
{
for(int i=0;i
您可以通过接受代码页或编码名称的方法获取其他编码对象。e、 g
//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");
利用
检查可能的解码风格
并使用此片段将字符串转换为字节[]
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;
}
公共静态字节[]StringToByteArray(字符串hexstring)
{
var bytes=新字节[hexstring.Length/2];
for(inti=0,j=0;i
感谢您的所有回答,它们非常有用。
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;
}