2

In my C# application I am making a call to an SAP RFC, which returns the binary format of a pdf (the associated type in SAP is HRB2A_TAB_RAW255).

However, when I read the returned data in C#, it is a string data type - "255044462D312E330D0A25E2E3CF...etc".

Response.Binarywrite needs a byte array, not a string representation of binary data.

How can I use that block of binary data when it comes over as a string data type?

======================================

UPDATE 2: Finally, I got it to work.

Since each row is a 255 byte array, I use Array.Copy to create one single row where the number of elements = 255 * rows returned from SAP

IRfcFunction uploadFile = rfcrep.CreateFunction("ZTEST_BOL");
IRfcTable fileBin = uploadFile.GetTable("BIN");
uploadFile.SetValue("SHIPMENT_NO", "701324577");
uploadFile.Invoke(dest);

int len;
byte binary = new byte[255 * fileBin.RowCount + 1];

for (int i = 0; i <= fileBin.RowCount - 1; i++)
{
    fileBin.CurrentIndex = i;

    if(i==0)
    {
      Array.Copy(fileBin.CurrentRow.GetByteArray("CONTENT"), 0, binary, 0, 
        fileBin.CurrentRow.GetByteArray("CONTENT").Length);
      len = fileBin.CurrentRow.GetByteArray("CONTENT").Length;
    }
    else
    {
    Array.Copy(fileBin.CurrentRow.GetByteArray("CONTENT"), 0, binary, len, 
          fileBin.CurrentRow.GetByteArray("CONTENT").Length);
    len += fileBin.CurrentRow.GetByteArray("CONTENT").Length;
    }
}

System.IO.File.WriteAllBytes(@"C:\Projects\hello.pdf", binary);

Nick
  • 23
  • 3

2 Answers2

1

I think, if I understand you correctly, what you need is a:

System.Text.ASCIIEncoding.Default.GetBytes("255044462D312E330D0A25E2E3CF...etc")

It may not be Ascii what you have there, but System.Text has a few string encoders. You can use them to convert strings to bytes and back again.

Tod
  • 2,070
  • 21
  • 27
  • Thanks, I tried UTF8Encoding, but no luck. When I open the pdf file in Notepad, it contains only the binary format that came from SAP - "25504446..." Also, 0x25 0x50 0x44 0x46 denotes a pdf file, so whatever comes from SAP seems to be the pdf file. Just not sure what to do with that long string of binary pdf. – Nick Dec 17 '20 at 00:20
  • Seems like you need to figure out what SAP is supplying you in that string form. Is there any documentation? – Tod Dec 17 '20 at 11:58
1

Since HRB2A_TAB_RAW255 is a raw table where each line is a field of type HRB2A_RAW255, which is raw data, probably every row of this table is a byte array.

Consider this mapping table from SAP: RFC To .NET Data Type Mapping.

You can try something like this:

foreach (IRfcStructure row in sapTable)
        {
            DataTable loTable = new DataTable();
            for (int liElement = 0; liElement < sapTable.ElementCount; liElement++)
            {
                RfcElementMetadata metadata = sapTable.GetElementMetadata(liElement);

                byte[] binary = new byte[255];
                binary  = row.GetByte(metadata.Name);
                ldr[metadata.Name] = binary;
            }
            loTable.Rows.Add(ldr);
        }
        return adoTable;
    }

Don't put it literally, it's just a quick shot, I don't even know if it works. You may also check this.

Suncatcher
  • 10,355
  • 10
  • 52
  • 90
  • This is indeed the right direction. I updated my initial post with the code that gets me the data, but I still don't have a working pdf. – Nick Dec 18 '20 at 19:55