如何解码具有不同数据宽度的不同XML文件的cdata部分 [英] How do I decode cdata section of different XML files having different datawidth

查看:67
本文介绍了如何解码具有不同数据宽度的不同XML文件的cdata部分的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

<?xml version="1.0" encoding="UTF-8"?>
<TesterLog Version="1">
  <TestProperties>
    <Item name="IUT Name" value="Reference"/>
  </TestProperties>
  <SignalData SamplingPeriod="1000.000 ns" DataWidth="16 bit">
    <Signal>
      <Id>IUT_RX</Id>
      <InitState>1</InitState>
      <![CDATA[HQFPAVkBiwGVAZ8BqQHHAdEBAwINAjUCPwJxAnsCrQK3AsEC1QLzAv0CEQMbAzkDTQNrA3UDfwOJA7sDxQPtA/cDKQQzBEcEUQSDBI0EtQTJBN0E5wTxBAUFDwUZBS0FNwVBBUsFVQWHFZEVmxWlFa8VuRXDFc0V1xXhFesV9RX/FTEWOxZFFk8WgRaLFpUWnxapFscW0RbbFuUW7xYDFyEXPxdJF1MXGhgkGC4YTBhWGHQYfhiwGLoY2BjiGBQZHhkoGTIZUBlaGXgZghmgGaoZvhnbGeUZ9RwTHR0dTx1ZHYsdlR29Hccd+R0DHg0eFx5JHlMeZx6ZHsEe6R4lH5Qfsh+8H+4f+B8qIDQgXCBmIJggoiCsILYg6CDyIAYhOCFgIYghxCEzIlEiWyKNIpciySLTIvsiBSM3I0EjSyNVI4cjmyOlI9cj/yMTJB0kpyaxJrsmxSbPJgEnCyc9J0cnZSdvJ6EnqyfdJ+cnGSgjKC0oQShLKF8ocyiHKJsopSivKLkowyjWKOAo8jQkNS41YDVqNZw1pjXENc41ADYKNhQ2HjZGNlA2WjZkNm42eDaWNqo2tDbHNtE2uDd=]]>
    </Signal>
    <Signal>
      <Id>CCT_TX</Id>
      <InitState>1</InitState>
      <![CDATA[HQFPAVkBiwGVAZ8BqQHHAdEBAwINAjUCPwJxAnsCrQK3AsEC1QLzAv0CEQMbAzkDTQNrA3UDfwOJA7sDxQPtA/cDKQQzBEcEUQSDBI0EtQTJBN0E5wTxBAUFDwUZBS0FNwVBBUkXUxfbGeUZ1ijgKMc20Ta4N0==]]>
    </Signal>
//////////////////////////////////////////////////////////////////////////////////////////////////////
  </SignalData>
</TesterLog>



这是我的xml文件。我能够阅读这些信息。但我的代码非常静态。它只解码DataWidth为16位的那些xml文件。 DataWidth是Element SignalData的属性。我希望我的代码能够适用于每种类型的DataWidth。例如8,16,24,32,40,48 ...... 64等



我的尝试:



this is my xml file. i am able to read the the information. but my code is very static. it decodes only those xml files in which DataWidth are 16 bits. DataWidth is the attribute of Element SignalData. I want my code to be in such a way that it should work for every type of DataWidth. e.g 8,16,24,32,40,48....64 etc.

What I have tried:

byte[] data = Convert.FromBase64String(cData);




int oldResult = 1;




ushort[] arr16 = new ushort[data.Length];




for (int i = 0; i < arr16.Length; i++)
            {
                arr16[i] = data[i];
            }




for (int i = 0; i < arr16.Length; i += 2)
                {
                var resultant = (arr16[i]) | (arr16[i + 1] << 8); //285 is the transition state.. As in xml file the initial state is 1 give. so signal will remain 1 till 284. on 285 signal becomes 0 and it will remain 0 before we reach another transition state... this will keep on doing like that. 




 for (int j = oldResult; j < resultant; j++)
                   {
                       Console.Write(initialState); // printing signal value. it can be either 1 or 0
 
                        
                    }
               
                   if (initialState == 1)
                        initialState = 0;
                    else
                       initialState = 1;

                    oldResult = resultant;

               
                }
Console.WriteLine();

推荐答案

所以你有每个样本具有不同比特数的样本。为避免使用不同类型的输出数组,您可以将结果存储在支持的最大宽度(此处为64)。



未经测试的示例:

So you have samples with different number of bits per sample. To avoid using different types of output arrays, you can store the result in the largest supported width (here 64).

Untested example:
// byte[] data is the input
// dataWidth is bits per sample (must be multiple of 8 and not larger than 64)

// bytes per sample
int bytes_per_sample = dataWidth / 8;
// Optional supporting dataWidth not being a multiple of 8
//if (dataWidth % 8)
//    bytes_per_sample++;

// total number of samples
int num_samples = data.Length / bytes_per_sample;
UInt64[] samples = new UInt64[num_samples];
for (int i = 0; i < num_samples; i++)
{
    // Assign the lowest byte (the only with 8 bit samples)
    samples[i] = data[i * bytes_per_sample];
    // Add the remaining bytes each shifted left by multiples of 8
    for (int j = 1, uint shift = 8; j < bytes_per_sample; j++, shift += 8)
    {
        samples[i] |= data[i * bytes_per_sample + j] << shift;
    }
}

请注意,上面假设输入流中的第一个字节是较低的字节。如果这不适用,则必须相应地修改上面的代码。

Note that the above assumes the first bytes in the input stream are the lower bytes. If that does not apply, the above code has to be modified accordingly.


Quote:

怎么做我解码具有不同数据宽度的不同XML文件的cdata部分

How do I decode cdata section of different XML files having different datawidth



无论你的数据是什么,base 64编码都是在一个字节数组上完成的,所以base64解码只能给你一个数组字节数,无论其含义如何。

因此,一旦有字节,就可以按组读取它们,具体取决于DataWidth。


No matter what is your data, base 64 encoding is done on an array of bytes, so base64 decoding can only give you an array of bytes, no matter its meaning.
So, once you have bytes, you have read them by groups depending on DataWidth.


这篇关于如何解码具有不同数据宽度的不同XML文件的cdata部分的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆