【问题标题】:How to parse string which represent a tree-like data structure如何解析代表树状数据结构的字符串
【发布时间】:2018-10-18 09:00:00
【问题描述】:

我需要将文本中包含的每个标记解析为三个成员的结构,例如

struct token {
    string id;
    int length;
    string value;
};

可以使用第一个文本(长度为 2)(从 00 到 99)来识别每个令牌,这是它的 id。 然后 id 后跟一个数值,表示值的长度,即下一项后跟长度。

这里的问题是一些id(token)也代表了一个token的集合,它们的每个id都从00开始......我试图用这种方式解决它......

tlv* Decoder::parsetlv(std::string data)
{
    tlv* root = new tlv();

    tlv* tlv_list = root;
    tlv* temp = nullptr;


    for (size_t index = 0; index < data.length(); temp = tlv_list, tlv_list = tlv_list->next) {

        if (!tlv_list) {
            temp->next = new tlv();
            tlv_list = temp->next;
        }


        tlv_list->Id = data.substr(index, 2);
        auto tempId = tlv_list->Id;
        index = index + 2;

        tlv_list->length = data.substr(index, 2);
        index = index + 2;

        int length = atoi(tlv_list->length.c_str());
        tlv_list->value = data.substr(index, length);
        if (any_of(_parentTagsIdentifiers, 72, tlv_list->Id)) {
            //place of horror
            tlv_list->child = tlv_list;

        }
        index = index + length;

    }

    return root;
}

我从我的实现中发现的问题是子 id 被混淆为父 id,因为它们具有相同的 id,不同的是父 id 在所谓的根 id 之下,而子 id 在(随后的)另一个id,在这种情况下称为模板id。

【问题讨论】:

  • 请使用您的示例输入并告诉我们预期的输出是什么。
  • 哦,这是个错误,谢谢
  • 现在当我看到二维码解析时,很明显这是xy problem,还有一点是。我建议再次编辑问题来描述乞讨的问题。从那你有一些来自二维码的数据。这个数据的确切格式是什么,这个二维码代表什么,那么你期望什么,你有什么。
  • @OgunleyeAyowalePius 您提供的示例数据不正确。(例如数据长度无效)。你能提供一个基本的例子吗?还要编辑您的问题以更清楚。
  • @MarekR 不,这个问题与 Qr 没有任何关系,它只是提供的代码中的一个命名空间,尽管我计划在项目中生成 Qr 代码,但主要目标是使用此规范解析此类文本。 github.com/apisit/emv-qr-code-generator/blob/master/Documents/…

标签: c++ parsing emv


【解决方案1】:

以下是将数据分解为 TLV 格式的示例代码。 See it working here:

#include <iostream>
#include <vector>
#include <string>
#include <cstdio>
using namespace std;

class TLV
{
    public:

        string tag;
        unsigned int length;
        string value;

        TLV(string data)
        {
            value = "";
            //If there is enough data
            if(data.length() >= 4)
            {
                tag = data.substr(0,2);
                sscanf(data.substr(2,4).c_str(),"%2x",&length);

                //If there is enough data
                if(data.length() >= 4 + length*2) value = data.substr(4,length*2);
            }
        }

        static void parseTLV(string data, vector<TLV*> &res)
        {
            while(data.length() >= 4)
            {
                TLV *t = new TLV(data);
                if(t->value == "") break;
                res.push_back(t);
                data = data.substr(4+(t->length+t->length));
            }

            if(data.length() != 0)
            {
                //Whole data is not in TLV format. Can throw some error
                cout<<"ERROR [1] :: ["<<data<<"]\n";
            }
        }
};


int main()
{
    string data = "0007AAAAAAAAAAAAAA010FAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA0220AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA";
    vector<TLV*> res;
    TLV::parseTLV(data, res);
    for(TLV *t:res)
    {
        printf("%s | %02X | %s |\n",t->tag.c_str(),t->length,t->value.c_str());
    }
    return 0;
}

【讨论】:

    猜你喜欢
    • 2011-04-19
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-10-20
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多