decode base64 and unpack binary data.

32 visualizzazioni (ultimi 30 giorni)
Enrique
Enrique il 5 Mar 2014
Modificato: Jan il 5 Mar 2014
Hi, I have an XML file which contains packed data in base64 format. I was able to decode it and unpack it using python, but now I need to write a function in matlab to perform this. I was able to decode the base64 data into a vector of class uint8; this vector contains several elements, each element represents 1 byte (number below 255). I have to unpack this vector of data to get the right decimal numbers/ the real data. Each real data number represents 4 elements in my vector (each number is 4 bytes), because the unpacked data should be floats. Attached my code, please look at the end of it when I tried using fread.
clc
clear all
close all
file = 'C:\Users\Iris\Desktop\cam5-20140227T061344Z.xml';
fid = fopen(file);
if fid == -1
disp('file open not succesful')
else
while feof(fid) == 0
line = fgetl(fid);
if isempty(strfind(line,'data')) == 0
rawdata = line;
rawdata = rawdata(1:length(rawdata)-7); % This line NEEDS to
%be tested with other files. If it doesn't work a string find
%'</data>' can be used.
end
end
end
closeresult = fclose(fid);
if closeresult == 0
disp('file close succesful')
else
disp('file did not close correctly')
end
packed_data = base64decode(rawdata); %these are decimals
save('rawdata.mat','packed_data');
fid = fopen('rawdata.mat');
data = fread(fid,length(packed_data),'uint8=>float','b');
closed = fclose(fid);
if closed == 0;
disp('file closed')
else
fisp('file not close')
end

Risposte (1)

Jan
Jan il 5 Mar 2014
Modificato: Jan il 5 Mar 2014
The indirection of using a file is always a bad idea. What about this:
packed_data = base64decode(rawdata);
float_data = typecast(packed_data, 'single');
But if you really want to udes fread, try 'float=>float' instead of 'uint8=>float'

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by