Character codes: Difference between revisions

Content added Content deleted
Line 333:
 
PureBasic allows compiling code so that it will use either Ascii or a Unicode (UCS-2) encoding for representing its string content. It also allows for the source code that is being compiled to be in either Ascii or UTF-8 encoding. A one-character string is used here to hold the character and a numerical character type is used to hold the character code. The character type is either one or two bytes in size, depending on whether compiling for Ascii or Unicode respectively.
<lang PureBasic>If OpenConsole()
;Results are the same when compiled for Ascii or Unicode
charCode.c = 97
Char.s = "a"
PrintN(Chr(charCode)) ;prints a
PrintN(Str(Asc(Char))) ;prints 97
 
Print(#CRLF$ + #CRLF$ + "Press ENTER to exit")
Repeat:Until Inkey() <> ""</lang>
Input()
CloseConsole()
EndIf</lang>
 
This version should be compiled with Unicode setting and the source code to be encoded using UTF-8.
<lang PureBasic>If OpenConsole()
;UTF-8 encoding compiled for Unicode (UCS-2)
charCode.c = 960
Char.s = "π"
PrintN(Chr(charCode)) ;prints π
PrintN(Str(Asc(Char))) ;prints 960
 
Print(#CRLF$ + #CRLF$ + "Press ENTER to exit")
Repeat:Until Inkey() <> ""</lang>
Input()
CloseConsole()
EndIf</lang>
 
=={{header|Python}}==