Reputation: 33
I can use UTF8 to convert But when I test Unicode, some characters will be lost, whether I have set errors
Please give me some advice
Chinese = "对我很有帮助" Lost display: "?我很有帮?"
static void Main(string[] args)
{
String hex2 = "F95B1162885F09672E5EA9522100";
String temp3 = Trans2(hex2);
Console.WriteLine(temp3);
Console.ReadLine();
}
public static string Trans(String input)
{
string temp1 = ConvertHexToString(input, System.Text.Encoding.UTF8);
return temp1;
}
private static string ConvertHexToString(String hexInput, System.Text.Encoding encoding)
{
int numberChars = hexInput.Length;
byte[] bytes = new byte[numberChars / 2];
for (int i = 0; i < numberChars; i += 2)
{
bytes[i / 2] = Convert.ToByte(hexInput.Substring(i, 2), 16);
}
return encoding.GetString(bytes);
}
Upvotes: 0
Views: 2089
Reputation: 835
I have tested your code but can't replicate your issue. It looks all fine on my environment. However, I do agree @grek40 about console encoding, or look into the font used in your console - is it capable to display these characters?
My test code is below, on a GUI application, probably you can try it out:
private static string ConvertHexToString(String hexInput, System.Text.Encoding encoding) {
int numberChars = hexInput.Length;
byte[] bytes = new byte[numberChars / 2];
for (int i = 0; i < numberChars; i += 2) {
bytes[i / 2] = Convert.ToByte(hexInput.Substring(i, 2), 16);
}
return encoding.GetString(bytes);
}
private static string ConvertStringToHex(String strInput, System.Text.Encoding encoding) {
return BitConverter.ToString(encoding.GetBytes(strInput)).Replace("-", String.Empty);
}
private void button1_Click(object sender, EventArgs e) {
string strTest = "对我很有帮助!";
Debug.Print(strTest);
string hex;
hex = ConvertStringToHex(strTest, Encoding.UTF8);
Debug.Print(hex);
Debug.Print(ConvertHexToString(hex, Encoding.UTF8));
hex = ConvertStringToHex(strTest, Encoding.Unicode);
Debug.Print(hex);
Debug.Print(ConvertHexToString(hex, Encoding.Unicode));
Debug.Print(ConvertHexToString("F95B1162885F09672E5EA9522100", Encoding.Unicode));
}
The result is as follows:
对我很有帮助!
E5AFB9E68891E5BE88E69C89E5B8AEE58AA921
对我很有帮助!
F95B1162885F09672E5EA9522100
对我很有帮助!
对我很有帮助!
Apparently your hex string in sample code is in Unicode format.
PS: If your input is read from file, you need to think about if the file is encoded in big/small endian and whether BOM is used.
Upvotes: 1