0% found this document useful (0 votes)
54 views

Converting Strings To Bytes and Vice Versa

This document discusses converting strings to bytes and vice versa in VB6/VBA, VB.NET, and C#. It explains that StrConv in VB6/VBA and System.Text.Encoding in VB.NET and C# can be used to convert between strings and bytes arrays. The default encoding may use the system code page like Windows-1252, but other encodings like UTF-8 and ISO-8859-1 can also be specified.
Copyright
© © All Rights Reserved
Available Formats
Download as DOC, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
54 views

Converting Strings To Bytes and Vice Versa

This document discusses converting strings to bytes and vice versa in VB6/VBA, VB.NET, and C#. It explains that StrConv in VB6/VBA and System.Text.Encoding in VB.NET and C# can be used to convert between strings and bytes arrays. The default encoding may use the system code page like Windows-1252, but other encodings like UTF-8 and ISO-8859-1 can also be specified.
Copyright
© © All Rights Reserved
Available Formats
Download as DOC, PDF, TXT or read online on Scribd
You are on page 1/ 2

Converting strings to bytes and vice versa

Use these functions to convert a string of text to an unambiguous array of bytes and vice
versa.

In VB6/VBA, use the StrConv function.

Dim abData() As Byte


Dim Str As String
Dim i As Long
Str = "Hello world!"
' Convert string to bytes
abData = StrConv(Str, vbFromUnicode)
For i = 0 To UBound(abData)
Debug.Print Hex(abData(i)); "='" & Chr(abData(i)) & "'"
Next
' Convert bytes to string
Str = StrConv(abData, vbUnicode)
Debug.Print "'" & Str & "'"
48='H'
65='e'
6C='l'
6C='l'
6F='o'
20=' '
77='w'
6F='o'
72='r'
6C='l'
64='d'
21='!'
'Hello world!'

VB6 stores its strings internally in "Unicode" format, two bytes per character, but the
StrConv function will convert to an array of bytes encoded in "ANSI" format using your
default code page.

In VB.NET use System.Text.Encoding.

Dim abData() As Byte


Dim Str As String
Dim i As Long
Str = "Hello world!"
' Convert string to bytes
abData = System.Text.Encoding.Default.GetBytes(Str)
For i = 0 To UBound(abData)
Console.WriteLine(Hex(abData(i)) & "='" & Chr(abData(i)) & "'")
Next
' Convert bytes to string
Str = System.Text.Encoding.Default.GetString(abData)
Console.WriteLine("'" & Str & "'")

In .NET strings are stored internally in "Unicode" format (UTF-16) and the GetBytes
method can extract an array of bytes in any encoding you want.
The .Default encoding uses the default code page on your system which is usually
1252 (Western European) but may be different on your setup. If you want ISO-8859-1
(Latin-1) you can replace .Default with .GetEncoding(28591) (code page 28591 is
ISO-8859-1 which is identical to Windows-1252 except for characters in the range 0x80
to 0x9F). Alternatively use System.Text.Encoding.GetEncoding("iso-8859-
1").GetBytes(Str). If you want UTF-8-encoded bytes, use
System.Text.Encoding.UTF8.GetBytes(Str).

In C#, use System.Text.Encoding, which has identical behaviour to the function in


VB.NET.

byte[] abData;
string Str;
int i;
Str = "Hello world!";
// Convert string to bytes
abData = System.Text.Encoding.Default.GetBytes(Str);
for (i = 0; i < abData.Length; i++)
{
Console.WriteLine("{0:X}", abData[i]);
}
// Convert bytes to string
Str = System.Text.Encoding.Default.GetString(abData);
Console.WriteLine("'{0}'", Str);

You might also like