Converting unicode characters (C#) Testing

六月ゝ 毕业季﹏ 提交于 2021-02-08 10:18:39

问题


I have the following problem

I am using an SDK that returns values form a database.

The value i need is 4, 6, 7 but the SDK returns "\u0004","\u0006","\u0007" I was wondering if there is a way to check if it is "\u0004","\u0006","\u0007" or any way of doing this?

I Have the following code (C#):

Line_Type = Line_Type.Replace(@"\u000", "");
if (Line_Type == "4")
{
    Line_Type = "4";
}
else if (Line_Type == "6")
{
    Line_Type = "6";
}
else if (Line_Type == "7")
{
    Line_Type = "7";
}

I have tried multiple ways to get the done but can't find a right way to get this done.

I Have google but can't find anything.


回答1:


From your question I understood that SDK returns values in unicoded style. Then you can use the following code to convert unicode values to respective decimal value.

char uniCodeValue = char.Parse(Line_Type);//  '\u0004' unicode values
int decimalValue = uniCodeValue;
Console.Write(decimalValue.ToString()); // prints 4

Hope your problem got solved!!




回答2:


In your replace call, "\u" is considered as an escape char. if you use double \, you get the string "\u0009"; check the difference between these two:

Console.WriteLine ("\u0009");
Console.WriteLine ("\\u0009");

the second print the string you are trying to replace with an empty string.



来源:https://stackoverflow.com/questions/33866445/converting-unicode-characters-c-testing

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!