2

I am reading a base64 string from xml file. The base64 string written to xml file is generated from QTc++ program. I have The Qtc++ program which parses the xml file and able to generate the data from base64 string.I want to do the same with python i am unable to figure how to read the base64 as byte array similar to QTc++. My Qt c++ code which parses the xml and decode the data file looks like below. For simplicity i am just including a part of encoded base64 string.

          datastring='AAAAAAAAAAA/YGJN0vGp/D9wYk3S8an8P3iTdLxqfvo/'            

          QByteArray xByteArray =QByteArray::fromBase64('datastring')
          QDataStream xInStream(xByteArray);
            while (!xInStream.atEnd())
            {
              double d;
              xInStream >> d;
              qDebug()<<d;
              pPlotCurve->addXAxisValue(d);
            }

I am able to decode the data using QTc++ the output looks like [0.0, 0.002 0.004,......]

I am trying to decode the same base64 string in python, i am getting scrambled data. My Python Code is

            import base64
            # I parse the xml file and retrieve the data below
            datastring = 'AAAAAAAAAAA/YGJN0vGp/D9wYk3S8an8P3iTdLxqfvo/'
            out = base64.b64decode(datastring)
            print out

When i decode this the above code i am getting scrambled data, how can i perform the same operation like the above QTc++ program and get the same output. I tried lot of methods but nothing worked.

1
  • sorry it should be datastring Commented Nov 17, 2015 at 16:20

1 Answer 1

1

base64.b64decode will take your base64 string and return bytes. Those bytes are just a pile of bits with no meaning. You need to tell Python how to turn those bits into meaningful data.

Use the struct module to tell Python how to interpret your binary data:

import base64
import struct
data_base64 = 'AAAAAAAAAAA/YGJN0vGp/D9wYk3S8an8P3iTdLxqfvo/'
data_bytes = base64.b64decode(data_base64)
# '>ddddx' big endian, 4 8-byte floating point plus 1 extra byte
struct.unpack('>ddddx', data_bytes) 
# gives (0.0, 0.002, 0.004, 0.006)

To unpack an arbitrary number of doubles do:

[struct.unpack_from('>d', data_bytes, offset)[0] for offset in range(0, len(data_bytes), 8)]
# gives [0.0, 0.002, 0.004, 0.006]

Note that your example data is 33 bytes long. If you use this second method make sure the length of your bytes is a multiple of 8, otherwise you will get struct.error: unpack_from requires a buffer of at least 8 bytes.

Sign up to request clarification or add additional context in comments.

4 Comments

Ok can you tell me what is that '>ddddx' means when unpacking
@arun: The comment explains it. Also the linked documentation gives more details.
Now if i decode my whole base64 string .I am getting this ERROR: unpack requires a string argument of length 33
Thank you very very much it worked, I will now look into this struct package and learn more

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.