File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
The moose likes Java in General and the fly likes SVG binary to text Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Java in General
Bookmark "SVG binary to text" Watch "SVG binary to text" New topic

SVG binary to text

Bob Grossman
Ranch Hand

Joined: Dec 18, 2008
Posts: 69
Hi, I'm using a Java library that can generate SVG images, but it outputs them only in binary format (as a byte[]). I want to get the SVG image in ASCII String format so I can embed it in an HTML page as a data URI or as XML so that I don't need to write the image to disk and then refer to the image in the HTML. Is there a simple, always-reliable way to convert the byte[] into a text String that contains the appropriate SVG information?
Campbell Ritchie

Joined: Oct 13, 2005
Posts: 39784
Too difficult a question for “beginning”: moved.
Ulf Dittmer

Joined: Mar 22, 2005
Posts: 42596
What is an "SVG image"? SVG is an XML format, and thus character data.

Ping & DNS - my free Android networking tools app
Bob Grossman
Ranch Hand

Joined: Dec 18, 2008
Posts: 69
The issue (I think) is the binary format of the data (as byte[]), versus the format that I need to use to embed the data into a Web page. When it is binary, it can be written to disk and then referenced by an tag. But I want to embed it directly into the Web page.
Paul Clapham

Joined: Oct 14, 2005
Posts: 18882

I guess my approach to this question would be to look at the documentation for the library to see what to do with those bytes. But in the absence of any documentation I would try to convert them to a String (because that's what you want). There's a String constructor which will do that -- actually there's a couple. Since your data is XML you should use the constructor which takes an array of bytes and the name of an encoding; the array of bytes you have already, and the encoding... well, that's where the documentation would come in.
Bob Grossman
Ranch Hand

Joined: Dec 18, 2008
Posts: 69
Thanks for your advice. The documentation did not give the encoding of the bytes, but when I specified UTF-8, the byte[] was properly converted into a String. One other twist: The XML contained Unicode (non-ASCII) characters that did not display properly when I embedded the XML in a Web page, so I had to do a second conversion of Unicode characters to their character entity references before everything worked properly.
I agree. Here's the link:
subject: SVG binary to text