Home Tutorials Forums Articles Blogs Movies Library Employment Press
Old 02-09-2012, 01:25 PM   #1
Registered User
Join Date: Apr 2007
Posts: 10
Default how to save binary data in ByteArray in to xml

I would need to save some binary data (bitmap data or similar) as a part of XML file.

var objectXML:XML;
var ba:ByteArray = myLoader.contentLoaderInfo.bytes; // for example
objectXML = <OBJECT content = { ba } </OBJECT>;

I suppose I must serialize or encode "ba" (binary bytearray data) some way to string before writeing to XML and decode them back during the reading from XML, but I cannot find the correct way how to do it.

Last edited by sorrex; 02-09-2012 at 01:36 PM.
sorrex is offline   Reply With Quote
Old 02-09-2012, 01:35 PM   #2
Senior Member
Join Date: Sep 2010
Posts: 131

I think it's possible (with tostring or readUTF8 or something), but why would you want to do this?

If it's an image or something else fairly large it will be way too big for any normal xml-processing. There are, i think, just no advantages in that. You should just save the image (or something) itself somewhere and just save the url to that in the xml.

If it's smaller, like just text, why don't you just save the text to the xml?

Maybe there is a scenario I'm missing, but I cannot think of one where it would be smart to save binary data to xml.
SanderSoulwax is offline   Reply With Quote
Old 02-09-2012, 02:14 PM   #3
Registered User
Join Date: Apr 2007
Posts: 10

thanks Sander,

Im sure you are right and finaly I will be saving data in binary format as you suggested. But data structure that I need to save is pretty complicated, so during development of application I would prefere to save it in XML for better readability. After base development I will make final data structure for binary file.

But reading your post I found a possible solution
I will write one binary file that will contain XML string with referencies on the begining, followed by binary data. And whole stream I will write as bytearray.

than I will reload file back like byteaaray again, saparate and parse only XML string part.
sorrex is offline   Reply With Quote
Old 02-09-2012, 02:32 PM   #4
Senior Member
lordofduct's Avatar
Join Date: Feb 2008
Location: West Palm Beach, FL
Posts: 3,872

Base64 encoding...

You take the bytes in the array, pair out 6 bits at a time, write that number in Base64, and write that value into a string. Put this string into your XML (it's going to be very long).

some source code of this technique:

There is other techniques as well, this one is just commonly used for clear text transmission of data.

Some might ask why not just use the entire UTF-8 character space which would eliminate the 30% increase in size of the file... that's because the characters outside of these 64 characters are reserved characters for various things. Not even CDATA would fix this because control characters exist in those 256 characters that may act as a flag on a server or in a network, or even a random binary sequence could terminate your CDATA.

Of course you could devise an algorithm around this as well, and such clear-text encoding algorithms DO exist. Base64 merely exists as a long time common clear-text encoding technique, the algorithm being very simple, and nearly all computer systems out there can deal with it easily. And I suggest it for the ease of use.

Oh just so you know, I use B64 encoding for several things in a program I write for work. It's in .Net, but the algorithm is identical (and MS has the algorithm built into .Net). I just encoded a random jpeg I had laying around... The image I encoded was 604x453 pixels in size, with jpeg encoding. The resulting string once converted to B64 was only 61,340 characters long. YEs a very long string, but not impossibly long and could easily be stored in xml.

That's only 60kB in data, I've seen XML files far larger than 60kB.
www.lordofduct.com - come read my blog!

If you want to know how to program, take a math class, take a lot of math classes!

Last edited by lordofduct; 02-09-2012 at 02:57 PM.
lordofduct is offline   Reply With Quote

Thread Tools
Display Modes Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump

All times are GMT. The time now is 02:28 AM.

Follow actionscriptorg on Twitter


Powered by vBulletin® Version 3.8.5
Copyright ©2000 - 2014, Jelsoft Enterprises Ltd.
Ad Management plugin by RedTyger
Copyright 2000-2013 ActionScript.org. All Rights Reserved.
Your use of this site is subject to our Privacy Policy and Terms of Use.