Click here to Skip to main content
15,312,535 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
<?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>

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();
Posted
Updated 11-Jun-18 9:34am
v2

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:
C#
// 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.
   
Comments
hamid18 12-Jun-18 7:06am
   
thanks. it worked for me.
Quote:
How do I decode cdata section of different XML files having different 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.
   
Comments
hamid18 12-Jun-18 7:07am
   
thanks..

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900