Page 1 of 1

utf8_encode() Function

Posted: 18 Nov 2021, 11:07
by pizzapizze
I made a UTF-8 encode function with AutoHotkey. :cookie:

Code: Select all

utf8_encode(char) {
    charNum := Asc(char)
    if (charNum < 0x80)
        return "%" ((StrLen(Format("{:x}", charNum)) = 1) ? ("0" Format("{:x}", charNum)) : Format("{:x}", charNum))
    Else
    {
        utf8encodeStart := 0xC280
        skipNum := 0xC0
        maxCount := 0x40
        
        editedCharNum := charNum - 0x80
        multpiplier := Format("{:d}", editedCharNum / maxCount)
        editedCharNum := editedCharNum + (multpiplier * skipNum)

        encodedCharNum := editedCharNum + utf8encodeStart

        return "%" SubStr(Format("{:x}", encodedCharNum), 1, 2) "%" SubStr(Format("{:x}", encodedCharNum), 3, 2)
    }
}