SocketTools .NET Edition

SocketWrench.CodePage Property

Gets and sets the code page used when reading and writing text.

[Visual Basic]
Public Property CodePage As Integer
[C#]
public int CodePage {get; set;}

Property Value

An integer value which specifies the current code page. A value of zero specifies the default code page for the current locale should be used. To preserve the original Unicode text, you can use code page 65001 which specifies UTF-8 character encoding.

Remarks

All data which is exchanged over a socket is sent and received as 8-bit bytes, typically referred to as "octets" in networking terminology. However, strings in .NET are Unicode where each character is represented by 16 bits. To send and receive data using strings, these Unicode strings are converted to a stream of bytes.

By default, strings are converted to an array of bytes using the code page for the current locale, mapping the 16-bit Unicode characters to bytes. Similarly, when reading data from the socket into a string buffer, the stream of bytes received from the remote host are converted to Unicode before they are returned to your application.

If you are exchanging text with another system and it appears to corrupted or characters are being replaced with question marks or other symbols, it is likely the system is sending text which is using a different character encoding. Most services use UTF-8 encoding to represent non-ASCII characters and selecting the UTF-8 code page will typically resolve the issue.

Strings are only guaranteed to be safe when sending and receiving text. Using a string data type is not recommended when reading or writing binary data to a socket. If possible, you should always use a byte array as the buffer parameter for the Read and Write methods whenever you are exchanging binary data.

For backwards compatibility, this class defaults to using the code page for the current locale. This property value directly corresponds to Windows code page identifiers, and will accept any valid code page supported by the .NET Framework. Setting this property to an invalid code page will generate an exception.

See Also

SocketWrench Class | SocketTools Namespace