PJ on Development
Thoughts and babbling about software development. Visual Basic, .NET platform, web, etc.
|Description:||Converting strings to byte arrays using an ANSI Encoding.|
|Environment:||VB.NET 2005 (Visual Studio 2005)|
The Software is provided "AS IS", without warranty of any kind, express or implied, including but not limited to the warranties of merchantability, fitness for a particular purpose and non-infringement. in no event shall the authors or copyright holders be liable for any claim, damages or other liability, whether in an action of contract, tort or otherwise, arising from, out of or in connection with the software or the use or other dealings in the software.
Many of us don't miss the old days when a character was represented by a single byte. Back then we need to worry about code pages, character translations, and the works...
Well, Unicode chars came to our rescue and saved us from having to worry about all these stuff.
But sometimes we need to represent a string as an array of bytes. The .NET Framework provides several encoding mechanisms: ASCII, UTF7, UTF8, etc.
Did you notice anything missing? The good old ANSI.
ANSI, to those who don't remember it, is a super set of ASCII where each char is represented by a single byte (range 0-255). ANSI Encoding is system dependent, but extremely efficient when handling strings as byte arrays.
This class fills this gap. It provides a fast way to convert from a string to an array of ANSI bytes, and vice-versa. Each char in the string (or char array) is converted to byte through the
Microsoft.VisualBasic.Asc function, and a byte array is converted to a char array using
Microsoft.VisualBasic.Chr function. Thus, I think it will be easy to understand.
It's multi threaded in order to achieve performance. In my tests I was able to convert an string about 1,000,000 chars long in less than 3 seconds.
The class inherits all its properties and methods of
System.Text.Encoding so it doesn't have any particular member.
Using the code
Just use it as you would normally use a