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?
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.
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.
Joined: Dec 18, 2008
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.