BYTE in a UNICODE world
I am reading a binary file into a byte array. I need to spawn a process via ProcessStartInfo class and pass this byte array to this process via redirecting its stdin to my program. So far no problems. Unfortunately, the StreamWriter that is used to send input to the spawned process (the write () function of Process.StardardInput) is via a string and/or char which are both unicode (it actually has 20 overloaded functions for all kinds of data types but the two applicable to me are string and char []).
The text I am tryinig to send to the program is decrypted messages in binary format which when translated from byte to unicode string or char[] is losing some of its high ASCII values (> 128).
Is there a way to send a byte array (or anything else non-unicode) to the stream input of the spawned process? Or, Is there a way to convert a byte array into a non-unicode char [] or string?
Thank you.
To convert any byte array to string and the string back to the original byte array you must use Convert.ToBase64String and Convert.FromBase64String.
The encodings (Ascii, UTF7, UTF8, UTF32, Unicode) doesn't support this roundtrip for any byte array.
Thanks for your responses. What I am trying to do is not converting a byte array to a string and then back to byte array. What I am trying to do is to read the 8 bits unsigned characters in the binary file and place them in a non-unicode string.
The actual application I am trying to do is read a gpg binary encrypted file and spawn the gpg application with the exact stream of bytes using the Process class and StartInfo.
Most shortly I think it might be good to investigate Process.StandardInput.BaseStream
This should return the underlying stream to which you could simply write the byte array raw and untransformed. In fact such a method might provide better performance for reading in larger files as you could write the data as read in smaller chunks rather than allocating some large byte array then encoding it as a string only for it to get deencoded again.
Lastly to address your original question the System.Text.Encoding.Ascii.GetString method may be able to help you but in all honesty the internal representation in .NET is UTF16. And for reasons beyond me the Process and ProcessStartInfo classes have support for specifying the StandardOutput and StandardError encodings but nothing for stdin. Oh well, I still think your best bet is to try that BaseStream and pay the StreamReader doesn't prefix encoding markers.
You're welcome! At least someone got helped today. Typically when one constructs a StreamWriter they have the option of specifying the encoding with which the contents will be written. Sadly the Process class creates the StreamWriter it returns to you and so you have no ability to decide that. It's an oddly overlooked functionality that must have some credible explaination though google suggests you're actually the first person to know or care.
As a consolation you can still technically layer another StreamWriter using your prefered encoding on top of that base stream but it would most likely be wasted resources to decode bytes into strings just to reencode them into bytes if you aren't gonna do any processing. Just make sure you flush the StreamWriter or Stream when you're done writing your data if you get any odd application bugs.
I really wish the .NET BCL devs could explain why they overlooked ProcessStartInfo.StandardInputEncoding