Solved

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

Posted on 2016-09-12
5
37 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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

This tutorial demonstrates one way to create an application that runs without any Forms but still has a GUI presence via an Icon in the System Tray. The magic lies in Inheriting from the ApplicationContext Class and passing that to Application.Ru…
Creating an analog clock UserControl seems fairly straight forward.  It is, after all, essentially just a circle with several lines in it!  Two common approaches for rendering an analog clock typically involve either manually calculating points with…
With the power of JIRA, there's an unlimited number of ways you can customize it, use it and benefit from it. With that in mind, there's bound to be things that I wasn't able to cover in this course. With this summary we'll look at some places to go…
As a trusted technology advisor to your customers you are likely getting the daily question of, ‘should I put this in the cloud?’ As customer demands for cloud services increases, companies will see a shift from traditional buying patterns to new…

896 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

Need Help in Real-Time?

Connect with top rated Experts

15 Experts available now in Live!

Get 1:1 Help Now