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">
    <Item name="IUT Name" value="Reference"/>
  <SignalData SamplingPeriod="1000.000 ns" DataWidth="16 bit">

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;
                       initialState = 1;

                    oldResult = resultant;

Updated 11-Jun-18 9:34am

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.
hamid18 12-Jun-18 7:06am
thanks. it worked for me.
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.
hamid18 12-Jun-18 7:07am

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