Difference between revisions of "ConvertEncoding"

From Xojo Documentation

m
 
Line 7: Line 7:
 
|text = Provides a quick way to convert a string of known encoding to some other encoding, without having to create a [[TextConverter]] object. }}
 
|text = Provides a quick way to convert a string of known encoding to some other encoding, without having to create a [[TextConverter]] object. }}
  
==Syntax==
+
== Usage ==
 
''result'' = '''ConvertEncoding'''(''Str'', ''NewEncoding'')<br />
 
''result'' = '''ConvertEncoding'''(''Str'', ''NewEncoding'')<br />
 
'''OR'''<br />
 
'''OR'''<br />
 
''result'' = ''Str''.'''ConvertEncoding'''(''NewEncoding'')
 
''result'' = ''Str''.'''ConvertEncoding'''(''NewEncoding'')
 
{| class="genericTable"
 
{| class="genericTable"
 
 
! width=15% | Part
 
! width=15% | Part
 
 
! width=15% | Type
 
! width=15% | Type
 
 
! width=55% | Description
 
! width=55% | Description
 
|-
 
|-
 
|result
 
|result
 
 
|[[String]]
 
|[[String]]
 
|The converted string using the ''NewEncoding'' TextEncoding.
 
|The converted string using the ''NewEncoding'' TextEncoding.
 
 
|-
 
|-
 
|str
 
|str
 
 
|[[String]]
 
|[[String]]
 
|The string to be converted.
 
|The string to be converted.
 
 
|-
 
|-
 
|newEncoding
 
|newEncoding
 
 
|[[TextEncoding]]
 
|[[TextEncoding]]
 
|The encoding to be used in the conversion.
 
|The encoding to be used in the conversion.
 
 
|-
 
|-
 
|}
 
|}
Line 41: Line 32:
 
==Notes==
 
==Notes==
 
When you need to write text to a file that will be opened by another application that expects a particular encoding, use '''ConvertEncoding''' to convert the text to that encoding before you call the Write method. Here is an example that converts the text in a [[TextField]] to the MacRoman encoding.
 
When you need to write text to a file that will be opened by another application that expects a particular encoding, use '''ConvertEncoding''' to convert the text to that encoding before you call the Write method. Here is an example that converts the text in a [[TextField]] to the MacRoman encoding.
 
 
<rbcode>
 
<rbcode>
 
Dim f As FolderItem
 
Dim f As FolderItem
Line 53: Line 43:
 
</rbcode>
 
</rbcode>
  
==Example==
+
== Sample Code ==
The following example use the [[Encodings]] module to convert the text in a [[TextField]] to the ANSI encoding:
+
The following code use the [[Encodings]] module to convert the text in a [[TextField]] to the ANSI encoding:
 
 
 
<rbcode>
 
<rbcode>
 
Dim s As String
 
Dim s As String

Latest revision as of 21:19, 22 January 2019

Method

Provides a quick way to convert a string of known encoding to some other encoding, without having to create a TextConverter object.

Usage

result = ConvertEncoding(Str, NewEncoding)
OR
result = Str.ConvertEncoding(NewEncoding)

Part Type Description
result String The converted string using the NewEncoding TextEncoding.
str String The string to be converted.
newEncoding TextEncoding The encoding to be used in the conversion.

Notes

When you need to write text to a file that will be opened by another application that expects a particular encoding, use ConvertEncoding to convert the text to that encoding before you call the Write method. Here is an example that converts the text in a TextField to the MacRoman encoding.

Dim f As FolderItem
Dim fileStream As TextOutputStream
file = GetSaveFolderItem(FileTypes1.Text, "My Info.txt")
If f <> Nil Then
fileStream = TextOutputStream.Create(f)
fileStream.Write(ConvertEncoding(nameField.Text, Encodings.MacRoman))
fileStream.Close
End If

Sample Code

The following code use the Encodings module to convert the text in a TextField to the ANSI encoding:

Dim s As String
s = ConvertEncoding(TextField1.Text, Encodings.WindowsANSI)

See Also

TextConverter, TextEncoding, TextOutputStream classes; DefineEncoding, Encoding functions; Encodings module.