Binary Data in String RRS feed

  • Question

  • Hi,

    Working with .NET 2.0 Win Forms...

    This might be an odd request, but I was wondering if it's possible to load binary data (from a graphic file) into a string variable? Please note that I do NOT want to convert the binary data to ASCII or anything else (must stay in original form). This is needed for storing images onto a barcode printer. Typically, a text file is created that contains both ASCII text commands (at the beginning of the file), then the binary image data after that. This file is then downloaded to the printer. I am hopeful to to eliminate the need to create this file, and send a string (containing binary data) directly to the printer.



    Sunday, April 4, 2010 11:50 PM


All replies

    • Proposed as answer by jgalley Monday, April 5, 2010 5:27 PM
    • Marked as answer by myNameIsRon Tuesday, April 6, 2010 5:07 AM
    Monday, April 5, 2010 10:56 AM
  • Thanks Ramesh,

    It seems that Base64String does not provide the correct results. I think I need to update my post. I do need to convert the binary, but I'm not sure which format I need. I'm looking to emulate how a standard TXT file converts the data. Here is a sample of the end result:

    Text File representation of binary:  ÒÿÁàÁ ÒÿÁàÁ ÁàÑ ÁàÁ ÁÀÑ `Á €Ñ `Á €Ñ  Á €Ñ


    I was hoping this would work, but it only returns a few characters.

    byte[] byteArray = File.ReadAllBytes(filePath);
    string s = System.Text.Encoding.Default.GetString(byteArray);






    Monday, April 5, 2010 6:03 PM
  • byte[] byteArray = File.ReadAllBytes(filePath);
    StringBuilder sb = new StringBuilder(byteArray.Length/2);
    for(int i=0; i<byteArray.Length; i+=2)
        sb.Append(BitConverter.ToChar(byteArray, i));
    string s = sb.ToString(); 
    • Marked as answer by myNameIsRon Tuesday, April 6, 2010 5:07 AM
    Tuesday, April 6, 2010 1:01 AM
  • Thanks Louis and Ramesh,

    With your help, I've been able to get it done.




    Tuesday, April 6, 2010 5:07 AM