-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[]?

Sandra Rossi
  • 11,934
  • 5
  • 22
  • 48
FranciscoFJM
  • 119
  • 1
  • 10
  • Check this one: https://stackoverflow.com/a/140861/10646316 – Svyatoslav Danyliv Jun 30 '21 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?) – Sandra Rossi Jun 30 '21 at 14:27

1 Answers1

0

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();
FranciscoFJM
  • 119
  • 1
  • 10