none
Attempting to turn string back to byte array. RRS feed

  • Question

  • There is a field in our db that contains a report that I am trying to convert to a byte stream and open in a new browser window. It is being written to the Oracle db by a java system. The developer's explanation was that he reads a file as byte array, then builds a UTF-8 StringBuffer to encode it and writes the result to a CLOB.  the result string is below.

    VGhpcyBpcyBhIHRlc3QgYXR0YWNobWVudCBmcm9tIHdpbnBzZG5zREVWMzANCg0KDQpDcmVhdGVkIDIvMTkvMTMgYXQgMTowMCBwbQ0KDQoNCioqKioqKioqKioqKioqKioqKioqKioqKioqKioqKioqKioqKioqKioqKioqKioqKioqKioqKioqKioq

    I have been playing with converting the string to a char[] using .ToCharArray(), then looping converting each using the Convert.ToByte which didn't work.  I also tried

    System.Text.UTF8Encoding.UTF8.GetBytes(response.attachment.fileContent);

    to no avail.  I know the results should be this message

    This is a test attachment from winpsdnsDEV30

    I would appreciate any direction that someone could give me.  Thank you very much.


    Created 2/19/13 at 1:00 pm


    ***********************************************************


    • Edited by Cheese Bread Monday, March 4, 2013 6:23 PM cleaning up presentation
    Monday, March 4, 2013 6:12 PM

Answers

  • I managed to get the desired result by taking a part of what I knew to be the end result and attempting to create it then from there try to "undo" it.  I saw that the last 15 characters of the messasge were asteriks and I could see the end result in the db.  After a number attempts I discovered that the developer on the loading side was appending the bytes into a string first and encoding it in Base64String right before putting into the db.  At that point a simple Convert.FromBase64String did the trick.  More of a business communication problem than anything else.  Thanks to all though.

      string input = "***************";  //15

      char[] c = input.ToCharArray();

      byte[] b = new byte[input.Length];

      for (int i = 0; i < input.Length; i++)

      {

           b[i] = Convert.ToByte(input[i]);

      }

      System.Text.StringBuilder sb = new System.Text.StringBuilder();

      for (int i = 0; i < b.Length; i++)

      {

           sb.Append(b[i]);

      }

      string output = Convert.ToBase64String(b);

               

      string pause= "Check Written Value";

      byte[] o = Convert.FromBase64String(output);

      

      System.Text.StringBuilder sOutput = new System.Text.StringBuilder();

      for (int i = 0; i < o.Length; i++)

      {

           sOutput.Append(Convert.ToChar(o[i]));

      }

      string result = sOutput.ToString();

      string endTest = "Check End Result";

    • Marked as answer by Cheese Bread Monday, March 4, 2013 8:34 PM
    Monday, March 4, 2013 8:33 PM

All replies

  • There is difference between Java Array and .NET array. That the reason when you use GetBytes or GetString you don't get correct result . Following link contain explanation for it.

    http://blogs.msdn.com/b/dotnetinterop/archive/2005/03/01/java-byte-to-vb-net-array-of-bytes.aspx


    Gaurav Khanna | Microsoft VB.NET MVP | Microsoft Community Contributor

    Monday, March 4, 2013 6:49 PM
  • I managed to get the desired result by taking a part of what I knew to be the end result and attempting to create it then from there try to "undo" it.  I saw that the last 15 characters of the messasge were asteriks and I could see the end result in the db.  After a number attempts I discovered that the developer on the loading side was appending the bytes into a string first and encoding it in Base64String right before putting into the db.  At that point a simple Convert.FromBase64String did the trick.  More of a business communication problem than anything else.  Thanks to all though.

      string input = "***************";  //15

      char[] c = input.ToCharArray();

      byte[] b = new byte[input.Length];

      for (int i = 0; i < input.Length; i++)

      {

           b[i] = Convert.ToByte(input[i]);

      }

      System.Text.StringBuilder sb = new System.Text.StringBuilder();

      for (int i = 0; i < b.Length; i++)

      {

           sb.Append(b[i]);

      }

      string output = Convert.ToBase64String(b);

               

      string pause= "Check Written Value";

      byte[] o = Convert.FromBase64String(output);

      

      System.Text.StringBuilder sOutput = new System.Text.StringBuilder();

      for (int i = 0; i < o.Length; i++)

      {

           sOutput.Append(Convert.ToChar(o[i]));

      }

      string result = sOutput.ToString();

      string endTest = "Check End Result";

    • Marked as answer by Cheese Bread Monday, March 4, 2013 8:34 PM
    Monday, March 4, 2013 8:33 PM