-2

I use a web service that returns an XSTRING from a SAP system encoded as hexadecimal in the Web service (characters 0 to 9 and A to F). I capture that XSTRING as a string in my code, but now I need to convert it to an array of bytes in order to later convert that array of bytes to a .pdf file.

How do you exactly make that initial conversion from hexadecimal to byte[]?

2
  • Check this one: stackoverflow.com/a/140861/10646316 Commented Jun 30, 2021 at 13:24
  • That doesn't mean a lot to say that a Web service returns a XSTRING. What do you actually receive? (either characters from base64 0-9a-zA-Z+/=, or hexadecimal characters 0-9A-F, or something else?) Commented Jun 30, 2021 at 14:27

1 Answer 1

1

I found a solution that actually worked for me, so i'm sharing it here:

byte[] byteArray = Enumerable.Range(0, PDF_XSTRING.Length)
                 .Where(x => x % 2 == 0)
                 .Select(x => Convert.ToByte(PDF_XSTRING.Substring(x, 2), 16))
                 .ToArray();
Sign up to request clarification or add additional context in comments.

2 Comments

So it means that in your case the Web service returned hexadecimal characters 0-9A-F (note that SAP default way of encoding XSTRING is base64).
Yes it does, but i cannot mark it as a correct answer until 3 days have passed.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.