QB64 Phoenix Edition
UChr$ - Printable Version

+- QB64 Phoenix Edition (https://qb64phoenix.com/forum)
+-- Forum: QB64 Rising (https://qb64phoenix.com/forum/forumdisplay.php?fid=1)
+--- Forum: Prolific Programmers (https://qb64phoenix.com/forum/forumdisplay.php?fid=26)
+---- Forum: SMcNeill (https://qb64phoenix.com/forum/forumdisplay.php?fid=29)
+---- Thread: UChr$ (/showthread.php?tid=2831)



UChr$ - SMcNeill - 06-27-2024

Code: (Select All)
SCREEN _NEWIMAGE(640, 480, 32)
f = _LOADFONT("arial.ttf", 16)
_FONT f

_MAPUNICODE 937 TO 3

omega$ = UChr$(937, 0): PRINT omega$
omega2$ = UChr$(937, 8): _UPRINTSTRING (0, 30), omega2$, , 8
omega3$ = UChr$(937, 16): _UPRINTSTRING (0, 60), omega3$, , 16
omega4$ = UChr$(937, 32): _UPRINTSTRING (0, 90), omega4$, , 32

FUNCTION UChr$ (codepoint AS LONG, encoding AS LONG)
    SELECT CASE encoding
        CASE 0: UChr = CPtoASCII(codepoint)
        CASE 8: UChr = CPto8(codepoint)
        CASE 16: UChr = CPto16(codepoint)
        CASE 32: UChr = CPto32(codepoint)
    END SELECT
END FUNCTION

FUNCTION CPto16$ (codepoint AS LONG)
    SELECT CASE codepoint
        CASE IS <= &HFFFF&& 'use a single codepoint
            temp$ = MKI$(codepoint)
        CASE ELSE 'uses 2 code points
            DIM highSurrogate AS LONG, lowSurrogate AS LONG
            highSurrogate = &HD800 + ((codepoint - &H10000) \ &H400)
            lowSurrogate = &HDC00 + ((codepoint - &H10000) MOD &H400)
            temp$ = MKI$(highSurrogate) + MKI$(lowSurrogate)
    END SELECT
    CPto16 = temp$
END FUNCTION



FUNCTION CPto8$ (codepoint AS LONG)
    SELECT CASE codepoint
        CASE IS <= &H7F&&
            temp$ = CHR$(codepoint)
        CASE IS <= &H7FF&&
            temp$ = CHR$(&HC0 OR _SHR(codepoint, 6))
            temp$ = temp$ + CHR$(&H80 + (codepoint AND &H3F))
        CASE IS <= &HFFFF&&
            temp$ = CHR$(&HE0 OR _SHR(codepoint, 12))
            temp$ = temp$ + CHR$(&H80 OR (_SHR(codepoint, 6) AND &H3F))
            temp$ = temp$ + CHR$(&H80 + (codepoint AND &H3F))
        CASE IS <= &H10FFFF&&
            temp$ = CHR$(&HF0 OR _SHR(codepoint, 18))
            temp$ = temp$ + CHR$(&H80 OR _SHR(codepoint, 12) AND &H3F)
            temp$ = temp$ + CHR$(&H80 OR (_SHR(codepoint, 6) AND &H3F))
            temp$ = temp$ + CHR$(&H80 + (codepoint AND &H3F))
    END SELECT
    CPto8 = temp$
END FUNCTION

FUNCTION CPto32$ (codepoint AS LONG)
    CPto32$ = MKL$(codepoint)
END FUNCTION

FUNCTION CPtoASCII$ (codepoint AS LONG)
    FOR i = 0 TO 255
        IF _MAPUNICODE(i) = codepoint THEN CPtoASCII$ = CHR$(i): EXIT FUNCTION
    NEXT
END FUNCTION

The above is a simple routine to allow one to use unicode values in their programs.  As long as you know the code value (there's unicode charts, just as there's ASCII charts which give this information), you can insert that symbol into your own strings and print it.

Note:  This will only return the ASCII character to you, if that unicode symbol is currently part of your ASCII character set.  Otherwise you'll just get back a blank string as a response ("").

Note2:  You have to specify what type of formatting you're using, so it can convert that numeric value to the proper encoded format for you.