0

I am being passed a byte array representing a pdf as part of an xml node.

the byte array int the xml looks like this

<Document>
Xh0XQo+PgovVHlwZSAvDAwIG4gCjAwMDAxNTc0MjkgMDAwMDAgbiAKMDAwMDE1ODQ1NSAwMDAwMCBuIAowMDAwMTU5MzY1IDAwMDAwIG4gCjAwMDAxNTk2MjEgMDATg5MyAwMDAwMCBuIAowMDAwMTYwMTQzIDAwMDAwIG4gCjAwMMDE2MDYzNSAwMDAwMCBuIAowMDAwMTYwODk5IDAwMDAwIG4gCjAwMDAxNNTkgMDAwMDAgbiAwMDE2NDkxMiAwMDAwMCBuIAowMDAwMTY1MTwMDAwIG4gCjAwMDAxNjU0MzYgMDAwMDE2NTUyMyAwMDAwMCBuIAowMDAwMTY1NzA5IDAwMDAwIG4gCjAwMDAxNjU5MjcgMDAwMDAgbiAKMDA4MTg3OSAwMDAwMCBuIAowMMTgxOTc4IDAwMDAwIG4gCnRyYWlsZXIKPDwvU2l6ZSAxMTMxCi9Sb290IDEgMCBSCi9JbmZvIDMgMCBSCi9JRCBbPDgyMTQwQURDM0QwOTRCREZBODI2MjM4Q0VBM0YxODA3PiA8ODIxNDBBREMzRDA5NEJERkE4MjYyMzhDRUEzRjE4MDc+XQovRW5jcnlwdCA0IDAgUgo+PgpzdGFydHhyZWYKMTgyMDEzCiUlRU9GCg...........</Document>

So I first copy the bytearray into a string variable .

 string pdfbyte = GetNodeUsingXpath(xpath.....);

Now I would like to cast this pdfbyte into a byte array.

 byte[] output = (byte[])pdfbyte;
 byte[]  output = byte.parse(pdfbyte);

These dont work.

I have looked online but could not find a simple solution to cast a byte array stored in a string variable to a byte array. Any pointers would be helpful.

Basically, I would like to copy the bytearray that is being sent as part of the xml into a byte array variable.

3
  • 1
    How are the bytes delimited in the string? Commented Jun 27, 2014 at 18:53
  • "So I first copy the bytearray into a string variable" - that sounds like a bad start. What does GetNodeUsingXpath actually return here? Is the data base64-encoded, by any chance? Commented Jun 27, 2014 at 18:57
  • Yes I think so .. I just updated the xml node for the pdf that I see in the xml structure Commented Jun 27, 2014 at 19:00

1 Answer 1

4

If you've got binary data within an XML document, I would hope that it's base64-encoded. Text data and binary data are different, and you shouldn't be trying to store arbitrary binary data in a string directly.

If it is, you can just use:

string base64 = GetNodeUsingXpath(xpath.....); 
byte[] output = Convert.FromBase64String(base64);

You may find you need to trim the string first:

byte[] output = Convert.FromBase64String(base64.Trim());

If it's not base64, you'll need to look carefully at what the text looks like. It may be hex instead, although that's not terribly likely. If someone has just use Encoding.GetString(bytes) to start with, then that code will need to be fixed, and it's almost guaranteed to lose data.

EDIT: Now that we can see some of the data, it does indeed look like it's base64.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.