Getting the ASCII code of a Unicode multibyte character
Jean Lalonde <[email protected]> Sat, 25 Jan 2025 11:40:49 -0800 (PST)
| Newsgroups | gmane.comp.lib.scintilla.devel |
|---|---|
| Message-ID | <[email protected]> |
Hi,
I needed to get the ASCII code of a multibyte character. The following code
(AutoHotkey v1.1) shows the ASCII code of the 3 characters in the Scintilla
editor. The second character is a multibyte character. The function
GetAsciiCode() gets the code by looking ahead in the editor stream when the
Asc() function returns an error.
Am I missing a built-in Scintilla command that would allow to do that
without the GetAsciiCode() workaround?
I don't like relying on the invalid character replacement character
Chr(65533) (see:
https://www.fileformat.info/info/unicode/char/fffd/index.htm). This may be
a marginal issue but the script will break if the editor really contains a
Chr(65533).
Thanks,
Jean
---
Gosub, InitScintilla
Gui 1:New, hWndMyGui
global o_Sci := New Scintilla(MyGui, 10, 10, 900, 700)
Gui Show, w920 h720
o_Sci.SETCODEPAGE(65001) ; Unicode (UTF-8)
o_Sci.SETTEXT("", "(" . Chr(8226) . ")", 1) ; Chr(8226) is a bullet
character
intEndOfText := o_Sci.GETTEXTLENGTH()
intPos := 0
intIncrement := 0 ; zero-based index
Loop
{
MsgBox, % "Pos: " . intPos . " ASCII code: " . GetAsciiCode(intPos,
intIncrement)
intPos := intPos + intIncrement ; increment position for multibyte
characters
if (intPos >= intEndOfText) ; this is the end of document
break
}
ExitApp
;---------------------------------------------------------
GetAsciiCode(intPos := 0, ByRef intNbBytes := 0)
;---------------------------------------------------------
{
loop, 4 ; maximum number of bytes per character is 4 according to RFC3629
{
str := GetTextRange(intPos, intPos + A_Index) ; A_Index is a one-based index
if Asc(str) != 65533
{
intNbBytes := A_Index
break
}
}
return Asc(str)
}
;---------------------------------------------------------
;---------------------------------------------------------
GetTextRange(intStartPos := 0, intEndPos := -1)
;---------------------------------------------------------
{
if (intEndPos = -1)
intEndPos := this.GetTextLength()
VarSetCapacity(strGetText, Abs(intStartPos - intEndPos) + 1, 0) ; Abs() in
case values are reversed (see Editor.ahk from Adventure-3.0.4)
VarSetCapacity(Sci_TextRange, 8 + A_PtrSize, 0) ; see
https://scintilla.org/ScintillaDoc.html#Sci_TextRange
NumPut(intStartPos, Sci_TextRange, 0, "UInt")
NumPut(intEndPos, Sci_TextRange, 4, "UInt")
NumPut(&strGetText, Sci_TextRange, 8, "Ptr")
o_Sci.GETTEXTRANGE(0, &Sci_TextRange)
strText := StrGet(&strGetText, "UTF-8")
return strText
}
;---------------------------------------------------------
--
You received this message because you are subscribed to the Google Groups "scintilla-interest" group.
To unsubscribe from this group and stop receiving emails from it, send an email to [email protected].
To view this discussion visit https://groups.google.com/d/msgid/scintilla-interest/09fdbaf2-6d28-4b46-896b-06a274dbc744n%40googlegroups.com.