Module

Encodings


Description

Returns the specified TextEncoding.

Usage

You can use the following syntax to easily get any TextEncoding object: myEncoding = Encodings.EncodingNamewhere EncodingName is one of the following

Methods

Name

Parameters

Returns

Shared

Count

Integer

GetFromCode

Code As Integer

TextEncoding

Item

Index As Integer

TextEncoding

Method descriptions


Encodings.Count

Count As Integer

Returns the number of Encodings in the built-in list of Encodings.


Encodings.GetFromCode

GetFromCode(Code As Integer) As TextEncoding

Returns the TextEncoding object corresponding to the code passed.

Codes can be obtained via declares or the Code method of TextEncoding objects.


Encodings.Item

Item(Index As Integer) As TextEncoding

Returns the specified element of the list of Encodings by its index. Index is zero-based.

Notes

The Encodings module makes it easy to obtain a specified TextEncoding. Any text encoding can be obtained via the Encodings module. Some of the most useful are UTF8, UTF16, ASCII, MacRoman, MacJapanese, and WindowsLatin1. It includes the UTF16BE (big endian) and UTF16LE (little endian) Encodings for reading and writing encoded text to files on computers that require either big or little endianness.

Use the Autocomplete feature of the Code Editor to view the complete list.

Sample code

Use the Chr method of the TextEncoding class to get a specific character in any encoding scheme. You use the Encodings function to first get the desired encoding. For example:

Var s As String
s = Encodings.UTF8.Chr(169)

When you read a string that was created outside your app, you should specify its encoding so that the byte string can be interpreted correctly. Use the Encodings function to get the encoding and pass it to the Encoding property of the TextInputStream class. This example specifies the MacCentralEurRoman encoding.

The Text file type was defined in the File Type Sets editor as one of the common file types.

Var f As FolderItem
Var t As TextInputStream
f = FolderItem.ShowOpenFileDialog(FileTypes1.Text)
If f <> Nil then
  t = TextInputStream.Open(f)
  t.Encoding = Encodings.MacCentralEurRoman
  TextField1.Text = t.ReadAll
  t.Close
End If

You can also specify the encoding of text using the optional parameter of the Read, ReadLine, or ReadAll methods.

To get an encoding by its String name, you can use a method like this:

Public Function EncodingFromName(value As String) as TextEncoding
  Var enc As TextEncoding
  For i As Integer = 0 To Encodings.Count - 1
    enc = Encodings.Item(i)
    If enc.internetName = value Then
      Return enc
    End If
  Next

  Raise New InvalidArgumentException("Encoding for name is not known.")
End Function

Compatibility

All project types on all supported operating systems.