ANSITOWIDE$ function

Purpose:

ANSITOWIDE$ function returns a wide-character (Unicode) string converted from a character string. ANSITOWIDE, UCODE$, and A2W$ are aliases for ANSITOWIDE$

Syntax:

RetPWSTR = ANSITOWIDE$(Multibyte AS STRING _
   [, CodePage AS INTEGER, dwFlags AS INTEGER, NULLTerm AS BOOL])

Return Value:

  • Data type: PWSTR
    RetPWSTR Returned pointer to translated wide-character string.

Parameters:

  • Data type: STRING
    Multibyte Points to the character string to be converted.
  • Data type: INTEGER
    CodePage [OPTIONAL]. Default is CP_ACP. Specifies the code page to be used to perform the conversion.
    👉 This value can be different on different computers, even on the same network. It can be changed on the same computer, leading to stored data becoming irrecoverably corrupted. This value is only intended for temporary use and permanent storage should use UTF-16 or UTF-8 if possible. See the CodePage section of the Microsoft MultiByteToWideChar function webpage for more information.
  • Data type: INTEGER
    dwFlags [OPTIONAL] Flag constants or integers specifying how to translate Multibyte. If flag constants are used for this parameter, the winnnls.h header, which contains the constants, must be specified. The default is MB_PRECOMPOSED. See the dwFlags section of the Microsoft MultiByteToWideChar function webpage for more information.
  • Data type: BOOL
    NULLTerm [OPTIONAL] When NULLTerm is TRUE, which is the default value, the entire Multibyte string is processed, including the terminating null character. Therefore, the resulting Unicode string has a terminating null character. See the cbMultiByte section of the Microsoft MultiByteToWideChar function webpage for more information.

Example:

DIM AS INTEGER RetVal
DIM AS STRING Aphorism
DIM AS STRING Caption
Caption = " ἹΠΠΟΚΡΑΤΟΥΣ "

$FILL Aphorism
  " Ὁ βίος βραχύς,"      & CRLF$ 
  " ἡ δὲ τέχνη μακρή,"   & CRLF$ 
  " ὁ δὲ καιρὸς ὀξύς,"   & CRLF$ 
  " ἡ δὲ πεῖρα σφαλερή," & CRLF$ 
  " ἡ δὲ κρίσις χαλεπή." & CRLF$ 
$FILL

DIM AS PWSTR Caption_UTF16
Caption_UTF16 = A2W$(Caption, 65001)

DIM AS PWSTR Aphorism_UTF16
Aphorism_UTF16 = A2W$(Aphorism, 65001)

RetVal = MessageBoxW(GetActiveWindow(), Aphorism_UTF16, Caption_UTF16, MB_SYSTEMMODAL)

Result:

This is an image produced by the above code.


WIDETOANSI$ function

Purpose:

WIDETOANSI$ function returns a character string converted from a wide-character (Unicode) string. WIDETOANSI, ACODE$, and W2A$ are aliases for WIDETOANSI$

Syntax:

RetStr = WIDETOANSI$(UnicodeStr AS LPWSTR _
 [, CodePage AS INTEGER, dwFlags AS INTEGER])

Return Value:

  • Data type: STRING
    RetStr Returned string.

Parameters:

  • Data type: LPWSTR
    UnicodeStr Points to the Unicode string to be converted.
  • Data type: INTEGER
    CodePage [OPTIONAL]. Default is CP_ACP. Specifies the code page to be used to perform the conversion.
    👉 This value can be different on different computers, even on the same network. It can be changed on the same computer, leading to stored data becoming irrecoverably corrupted. This value is only intended for temporary use and permanent storage should use UTF-16 or UTF-8 if possible. See the CodePage section of the Microsoft WideCharToMultiByte function webpage.
  • Data type: INTEGER
    dwFlags [OPTIONAL] Flag constants or integers specifying how to translate UnicodeStr. If flag constants are used for this parameter, the winnnls.h header, which contains the constants, must be specified. The default value is 0 (zero). See the dwFlags section of the Microsoft WideCharToMultiByte function webpage.

Example:

👉 The example below shows that data converted from UTF-8 and UTF-16 to non-Unicode encodings is subject to data loss, because a code page might not be able to represent every character used in the specific Unicode data. The Unicode glyphs, unrepresentable when converted to the Greek code page 1253, are substituted with "?".

DIM AS UINT OEMCodePage
OEMCodePage = GetOEMCP()
PRINT "Current OEM Code Page (OEMCP): ", STR$(OEMCodePage)

DIM AS UINT WindowsCodePage
WindowsCodePage = GetACP()
PRINT "Current Windows Code Page (ACP): ", STR$(WindowsCodePage)
?
DIM AS UINT OriginalConsoleOutputCodePage
OriginalConsoleOutputCodePage = GetConsoleOutputCP()
PRINT "Current Console Code Page: ", STR$(OriginalConsoleOutputCodePage)

DIM AS STRING ελληνικά

$FILL ελληνικά
  " Ὁ βίος βραχύς,"      & CRLF$ 
  " ἡ δὲ τέχνη μακρή,"   & CRLF$ 
  " ὁ δὲ καιρὸς ὀξύς,"   & CRLF$ 
  " ἡ δὲ πεῖρα σφαλερή," & CRLF$ 
  " ἡ δὲ κρίσις χαλεπή." & CRLF$ 
$FILL
PRINT ελληνικά

?

SetConsoleOutputCP(65001)
DIM AS UINT CurrentCodePage
CurrentCodePage = GetConsoleOutputCP()
PRINT "Current Console Code Page: ", STR$(CurrentCodePage)
PRINT ελληνικά

?

DIM AS PWSTR ελληνικά_UTF16
ελληνικά_UTF16 = A2W$(ελληνικά, 65001)
DIM AS STRING ελληνικά_1253
ελληνικά_1253 = W2A$(ελληνικά_UTF16, 1253)
SetConsoleOutputCP(1253)
CurrentCodePage = GetConsoleOutputCP()
PRINT "Current Console Code Page: ", STR$(CurrentCodePage)
PRINT ελληνικά_1253

PAUSE

SetConsoleOutputCP(OriginalConsoleOutputCodePage)

Result:

👉 Data converted from UTF-8 and UTF-16 to non-Unicode encodings is subject to data loss. The Unicode glyphs, unrepresentable when converted to the Greek code page 1253, are substituted with "?".

This is an image produced by the above code.