• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

SVG binary to text

 
Bob Grossman
Ranch Hand
Posts: 78
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Sheriff
Posts: 48652
56
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Too difficult a question for “beginning”: moved.
 
Ulf Dittmer
Rancher
Posts: 42967
73
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
What is an "SVG image"? SVG is an XML format, and thus character data.
 
Bob Grossman
Ranch Hand
Posts: 78
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Sheriff
Posts: 20989
31
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 78
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic