Solved

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

Posted on 2016-09-12
5
40 Views
Last Modified: 2016-10-02
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.
0
Comment
Question by:Member_2_6470879
5 Comments
 
LVL 45

Expert Comment

by:aikimark
ID: 41795798
What do/should your inputs and outputs look like?
0
 
LVL 33

Expert Comment

by:ste5an
ID: 41795810
What encoding (code page) to you need for your file? See also Encoding. And use it with StreamWriter Constructor (Stream, Encoding).
0
 
LVL 33

Expert Comment

by:sarabande
ID: 41796033
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.

Sara
0
 
LVL 1

Accepted Solution

by:
Member_2_6470879 earned 500 total points (awarded by participants)
ID: 41796746
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.
0
 
LVL 33

Expert Comment

by:sarabande
ID: 41825219
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.

Sara
0

Featured Post

Networking for the Cloud Era

Join Microsoft and Riverbed for a discussion and demonstration of enhancements to SteelConnect:
-One-click orchestration and cloud connectivity in Azure environments
-Tight integration of SD-WAN and WAN optimization capabilities
-Scalability and resiliency equal to a data center

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

1.0 - Introduction Converting Visual Basic 6.0 (VB6) to Visual Basic 2008+ (VB.NET). If ever there was a subject full of murkiness and bad decisions, it is this one!   The first problem seems to be that people considering this task of converting…
It was really hard time for me to get the understanding of Delegates in C#. I went through many websites and articles but I found them very clumsy. After going through those sites, I noted down the points in a easy way so here I am sharing that unde…
Although Jacob Bernoulli (1654-1705) has been credited as the creator of "Binomial Distribution Table", Gottfried Leibniz (1646-1716) did his dissertation on the subject in 1666; Leibniz you may recall is the co-inventor of "Calculus" and beat Isaac…

860 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question