In your .Net application most data will be passed around using the
string data type. In some cases, you may want this as a byte array for
example, to compute a hash or to serialize.
The standard representation strings that people are used to is a sequence of bytes that represent ASCII characters. This is good for simple applications but is limited because it cannot represent the many of the additional characters found in Unicode. String variables can contain any Unicode characters which require more than one byte for storage. The only way to preserve these non-ASCII characters is to use a method of character encoding that will support these characters. The best (I reckon) is UTF-8 which is also known as code page 65001.
Hang on, what's UTF-8?
Many people smarter than me have defined this, so here's a link to a good explanation: http://en.wikipedia.org/wiki/UTF8
The standard representation strings that people are used to is a sequence of bytes that represent ASCII characters. This is good for simple applications but is limited because it cannot represent the many of the additional characters found in Unicode. String variables can contain any Unicode characters which require more than one byte for storage. The only way to preserve these non-ASCII characters is to use a method of character encoding that will support these characters. The best (I reckon) is UTF-8 which is also known as code page 65001.
Hang on, what's UTF-8?
Many people smarter than me have defined this, so here's a link to a good explanation: http://en.wikipedia.org/wiki/UTF8
Here's a code snippet to perform the conversion. For a Visual Basic 6 version of this functionality, check out: VB6: How To Convert UTF-8 Byte Arrays into Unicode Strings (and vice versa)
Its very simple, and in .Net can be done via the Text.Encoding object.
Imports System.Text
...
Public Function StringAsUtf8Bytes(ByVal strData As String) As Byte()
Dim bytes() As Byte
' get unicode string as bytes
bytes = Encoding.UTF8.GetBytes(strData)
' return byte data
Return bytes
End Function
324 comments,
VB.Net, Thursday, August 5, 2004 16:18


