Solved

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

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

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 32

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 32

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

Why You Should Analyze Threat Actor TTPs

After years of analyzing threat actor behavior, it’s become clear that at any given time there are specific tactics, techniques, and procedures (TTPs) that are particularly prevalent. By analyzing and understanding these TTPs, you can dramatically enhance your security program.

Join & Write a Comment

It’s quite interesting for me as I worked with Excel using vb.net for some time. Here are some topics which I know want to share with others whom this might help. First of all if you are working with Excel then you need to Download the Following …
Calculating holidays and working days is a function that is often needed yet it is not one found within the Framework. This article presents one approach to building a working-day calculator for use in .NET.
Excel styles will make formatting consistent and let you apply and change formatting faster. In this tutorial, you'll learn how to use Excel's built-in styles, how to modify styles, and how to create your own. You'll also learn how to use your custo…
This video explains how to create simple products associated to Magento configurable product and offers fast way of their generation with Store Manager for Magento tool.

747 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

9 Experts available now in Live!

Get 1:1 Help Now