我如何使用Unicode从字符串中拆分数据?

问题描述 投票:-2回答:3

早上好,我有一个问题。例如,我需要从Unicode中恢复数据

“\ u001f \ u0001 \ u0013FERREIRA RAMOS MUZI \ u001f \ u0002 \ 0 \ u001f \ u0003 \ aRICARDO \ u001f \ u0004 \ u0003URY \ u001f \ u0005 \ b09031979 \ u001f \ u0006 \ u000eMONTEVIDEO / URY \ u001f \ a \ b34946682 \ u001f \ b \ u0004 \“\ a \ u0016 \ u001f \ t \ b22072026 \ u001f \ n \ 0”

字节中的字符串

1F011346455252454952412052414D4F53204D555A491F02001F03075249434152444F1F04035552591F050830393033313937391F060E4D4F4E5445564944454F2F5552591F070833343934363638321F0804220720161F090832323037323032361F0A00

例如,我需要在ArrayList或Array String中恢复Name,Last Name等

string [] array = {“Stephen”,“King”,“11301958”,“NewYork / Usa”}

如果我使用我的问题

System.Text.Encoding.UTF8.GetString(ByteArray);

获取数据,我只获得姓名和姓氏,没有日期或来自。

我怎么能从这个字符串中得到它?

c# unicode-string converters
3个回答
1
投票

您可能需要创建自定义解析器:

byte [] bytes = // Your data here....
// Parser
List<string> words = new List<string>();
for (var i = 0; i < bytes.Length; i++) {
    if (0x1F == bytes[i]) {
        int index = bytes[i+1]; // Ignoring this
        int len = bytes[i+2];
        // Convert bytes to string
        words.Add(System.Text.Encoding.UTF8.GetString(bytes, i+3, len));
        i += len + 2;
    }
}
Console.WriteLine(String.Join("\n", words.ToArray()));

输出:

FERREIRA RAMOS MUZI

RICARDO
URY
09031979
MONTEVIDEO/URY
34946682
"           - some non-printable chars here
22072026

看起来某些字段需要特殊的解析。


0
投票

我的解决方案

仅检测字母a-zA-Z和具有正则表达式的数字如果正则表达式失败或是白色空格,则Word为Complet,然后将其添加到List,最后我有一个List,其中包含所有单词和数字。

1-将Byte []数据转换为字符串

// Convert utf-8 bytes to a string.
s_unicode2 = System.Text.Encoding.UTF8.GetString(apduRsp.Data);

List<string> test = new List<string>();
if (s_unicode2.Length > 0)
{
   test = GetWords(s_unicode2);
}

2-使用Byte []转换的字符串调用GetWords()

private List<string> GetWords(string text)
    {
        Regex reg = new Regex("[a-zA-Z0-9]");
        string Word = "";
        char[] ca = text.ToCharArray();
        List<string> characters = new List<string>();
        for (int i = 0; i < ca.Length; i++)
        {
            char c = ca[i];
            if (c > 65535)
            {
                continue;
            }
            if (char.IsHighSurrogate(c))
            {
                i++;
                characters.Add(new string(new[] { c, ca[i] }));
            }
            else
            {
                if (reg.Match(c.ToString()).Success || c.ToString() == "/")
                {
                    Word = Word + c.ToString();
                    //characters.Add(new string(new[] { c }));
                }
                else if(c.ToString() == " ")
                {
                    if(Word.Length > 0)
                        characters.Add(Word);
                    Word = "";
                }
                else
                {
                    if(Word.Length > 0)
                        characters.Add(Word);
                    Word = "";
                }

            }

        }
        return characters;
    }

3- GetWords()的结果

List<string> values returned

目前我的解决方案很好,但是有些人有2个名字,这在展示时就是一个小问题。


-1
投票

看起来它是二进制数据与字符串的组合。有一个行数。所以这段代码可能有帮助

            string input = "\u001f\u0001\u0013FERREIRA RAMOS MUZI\u001f\u0002\0\u001f\u0003\aRICARDO\u001f\u0004\u0003URY\u001f\u0005\b09031979\u001f\u0006\u000eMONTEVIDEO/URY\u001f\a\b34946682\u001f\b\u0004\"\a \u0016\u001f\t\b22072026\u001f\n\0";
            string output = System.Net.WebUtility.HtmlDecode(input);
            string[] lines = output.Split(new char[] { '\u001f' });
© www.soinside.com 2019 - 2024. All rights reserved.