SDL_UCS4ToUTF8(3)

✨ SDL_UCS4ToUTF8(3) ✨

okay bestie so this one is a little function from the SDL3 FUNCTIONS crew and honestly? it's giving "tiny but mighty" πŸ’…


🌸 NAME 🌸

SDL_UCS4ToUTF8 β€” convert a single Unicode codepoint to UTF-8. one codepoint in, up to four UTF-8 bytes out. that's the whole gig, and she does it flawlessly. periodt. ✨


πŸŽ€ SYNOPSIS πŸŽ€

#include <SDL3/SDL_stdinc.h>

char * SDL_UCS4ToUTF8(Uint32 codepoint, char *dst);

πŸ¦‹ DESCRIPTION πŸ¦‹

so the buffer that dst points at? it must be at least 4 bytes long, no discussion, because this function may generate anywhere between 1 and 4 bytes of output. like, a tiny little codepoint might only need one byte, but some of them need the whole four. she's unpredictable in the cutest way πŸ’–

this function returns the first byte after the newly-written UTF-8 sequence. and the way that is SO useful, okay? it means you can encode a bunch of codepoints in a loop and just keep feeding the return value back in, or you know exactly where to drop a NULL-terminator to end your string. in either of those cases, plan to have a buffer of more than 4 bytes!! like, 4 is the minimum for one call, not for a whole loop, bestie, don't do that to yourself 😭

if codepoint is an invalid value (outside the Unicode range, or a UTF-16 surrogate value, etc.), this will use U+FFFD (REPLACEMENT CHARACTER) for the codepoint instead, and it will not set an error. she's not mad, she's just gonna quietly hand you the little οΏ½ and move on πŸ™ˆ

if dst is NULL, this returns NULL immediately without writing to the pointer and without setting an error. no drama, no side effects, she just leaves. icon behavior honestly πŸ‘‘


πŸ’œ FUNCTION PARAMETERS πŸ’œ

  • codepoint β€” a Unicode codepoint to convert to UTF-8. the little number you want turned into bytes πŸ“
  • dst β€” the location to write the encoded UTF-8. must point to at least 4 bytes! like actually actually. don't skip this part. 😀

🌈 RETURN VALUE 🌈

returns the first byte past the newly-written UTF-8 sequence. that's your little bookmark for "next write goes here" 🫢


⭐ THREAD SAFETY ⭐

it is safe to call this function from any thread. no cap, go wild, every thread gets a turn πŸ’…


πŸ‘‘ AVAILABILITY πŸ‘‘

this function is available since SDL 3.2.0. if you're on anything older she simply does not exist yet, sorry bestie 🌸


ok that's the whole page, mwah πŸ’– go encode something cute and remember the 4-byte minimum, i'm begging you 😭

Simple Directmedia Layer β€’ SDL 3.4.18 β€’ SDL_UCS4ToUTF8(3) β€” that's the source, the version, and the page name all wrapped up in a lil sign-off, love u, bye! πŸŽ€βœ¨πŸ¦‹

πŸ“¦ where this page comes from