Byte[] to string - Kinda...

G

Guest

Hi guys. I'm parsing an XML document with the Chilkat XML.net parser. I am trying to take the easy way out since I'd rather not have to write my own. The problem is that the Chilkat parser automatically performs a whitespace trim on a node's content when it returns the value with the .Content property. To get around this, I have tried using the .GetBinaryContent() to just give me everything. The problem I am having is that the method returns a byte array, and I am having trouble getting the string back out of this. I've tried System.Text.Encoding.[EVERYTHING!].GetString([byte array]) with no luck. Any suggestions? Thanks in advance

Josh Usovsky
 
T

Tian Min Huang

Hello Josh,

Thanks for your post. As I understand, you want to convert Byte arrary to
string. I suggest that you can check ASCIIEncoding.GetString method, please
refer to the following MSDN article:

ASCIIEncoding.GetString Method
http://msdn.microsoft.com/library/default.asp?url=/library/en-us/cpref/html/
frlrfsystemtextasciiencodingclassgetstringtopic.asp

Hope this helps.

Regards,

HuangTM
Microsoft Online Partner Support
MCSE/MCSD

Get Secure! -- www.microsoft.com/security
This posting is provided "as is" with no warranties and confers no rights.
 
J

Jon Skeet [C# MVP]

Hi Tim, thanks for the follow up. I found that the content of the XML
node was being returned as a base-64 byte array. I?m not quite surte
of how to go about getting this back into an ascii string.

What *exactly* do you mean by "a base-64 byte array"? Base64 is a text
encoding of binary, so if something is base64 encoded the result should
be text, rather than a byte array. Do you mean that it's gone something
like:

Binary data (byte[])
to
Base 64 encoding (string)
to
Binary data (byte[]) via a normal encoding

?

If so, use the appropriate encoding to get back to the Base64 version,
and then Convert.FromBase64String to get the original binary. (If the
original binary was itself an encoded form of a text string, you've got
one more call to Encoding.GetString to make - again, with whatever
encoding was originally used for that step.)
 
G

Guest

Thank you for the reply, Jon. The original data is just ASCII text from an xml file. The parser that I am using automatically trims white space from the node content. I need the white space, however, so I am using the parser object's getBinaryContent() method to return the content in binary form. It is *expecting* the content of the node to be base64-encoded in the file, and returns a byte[] containing the content. I have tried several converts and I still am not able to get back to the original string

Josh
 
J

Jon Skeet [C# MVP]

Thank you for the reply, Jon. The original data is just ASCII text
from an xml file. The parser that I am using automatically trims
white space from the node content. I need the white space, however,
so I am using the parser object's getBinaryContent() method to return
the content in binary form. It is *expecting* the content of the node
to be base64-encoded in the file, and returns a byte[] containing the
content. I have tried several converts and I still am notable to get
back to the original string.

It sounds like you're basically trying to use the
getBinaryContentMethod in the wrong situation then. Why are you using
this particular parser?
 
T

Tian Min Huang

Hi Josh,

Could you post some code snippet which is able to demonstrate the problem?

Regards,

HuangTM
Microsoft Online Partner Support
MCSE/MCSD

Get Secure! -- www.microsoft.com/security
This posting is provided "as is" with no warranties and confers no rights.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top