Storing ASCII charactors greater than 127 as a one byte string in .NET

Working with a .NET web page that needs to write data retrieved from a data base to a local file where numbers are converted to a two byte string and then written to a file.  Basically the old MKI$ function.  Seems like any number higher than 127 will be written out as a two byte string due to the way high ASCII numbers are handled now.  Is there any function or string definition that will still handle high ASCII number up to 255 and let the one byte string be written to a file?  Thanks very much for any help.
Who is Participating?
Member_2_6470879Author Commented:
Thanks everyone.  Adding Encoding.Default to the StreamWriter Constructor method worked.  I missed the fact that the Default would actually do anything so didn't try that.  But it is exactly what I needed to specify.  Now the two byte string for 128 for instance is stored as hex 80 00 in the disk file rather than the three byte string I was getting before.
What do/should your inputs and outputs look like?
ste5anSenior DeveloperCommented:
What encoding (code page) to you need for your file? See also Encoding. And use it with StreamWriter Constructor (Stream, Encoding).
Seems like any number higher than 127 will be written out as a two byte string due to the way high ASCII numbers are handled now.  

that is not true. double-byte strings are called UNICODE by MS (and actually is UTF16 encoding what is only the first layer of full 32-bit Unicode) and contain character codes up to 65535 (256x256 - 1).

one-byte strings normally (in US and most Europe countries) use ANSI codepage (codes 0 to 255). the ANSI codepage contains  ASCII as the first half of the code page.

in .NET all strings were handled as double-byte strings. that also includes strings with ASCII (7-bit character codes) texts and ANSI texts (8-bit character codes). if you look at the strings with a hex editor, you see that the high byte of each double-byte character is 0. so conversion of MS UNICODE strings to one-byte strings is easy: take-over any second byte to a byte array or cast each double-byte character to one byte char. there are appropriate functions for both conversions available.

Actually the Asker found a working solution themselves. None of the comments added by Experts - including the one I added myself - could claim for assistance without doubts.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.