Windows has three built-in text formats for the clipboard:
CF_: UTF-16 text.UNICODEÂTEXT CF_: 8-bit text in ANSI code page.TEXT CF_: 8-bit text in OEM code page.OEMÂTEXT
If you don’t provide all three formats, then the system will synthesize the missing ones from the ones you have. How does this work?
Believe it or not, we’re going to spend the rest of the week on this topic.
One thing to note is that the synthesis is done on demand. It is only when somebody asks for, say, CF_ and the clipboard realizes that it has only CF_, that the clipboard creates the CF_ on the fly. Once done, the result is cached so that it doesn’t have to be converted again.
Today’s conversion is the one that looks easiest on the surface: Converting CF_ to CF_ and back.
To convert CF_ to CF_, Windows uses the AnsiToOem function. And to convert the other way, it uses the OemÂToÂAnsi function.
These are legacy function names; the modern names are CharÂToÂOem and OemÂToÂChar. But I used the legacy names because that’s what they were called in 16-bit Windows, and that’s how the conversion was done in 16-bit Windows.
This was anticlimactic, but we’re just getting started. And when we get to the end, we’ll see that what looks like a simple answer is actually quite complicated.
Back in the days of 16-bit Windows, ANSI text and OEM text were the only two clipboard text formats, so there were only two possible conversions (one in each direction). Things get more complicated with the introduction of CF_, which we’ll look at next time.
If UTF-8 is selected as the ANSI codepage, you get it from CF_TEXT. If your app uses UTF-8 everywhere then it probably knows how to convert from UTF-16 already.
(I have no idea why my replies don’t attach correctly to the person I’m replying to)
Hey Raymond, when will they be adding CF_UTF8TEXT?
Also, when will Microsoft Teams stop putting large images as giant Base64 encoded sausages on the clipboard?
A soul-crushing exercise: Try copying a large image from Microsoft Teams to another messenger app (Telegram, Viber, Signal, WhatsApp, etc), watch how it results in 1,000+ gibberish text messages when you paste it, and then have "fun" selecting and deleting them all.
IMO, that's total Clipboard API abuse and should be blocked on the OS level. The least that could be done is to give user the control of the size of text pasted to clipboard and also...
I agree that this is very inconvenient, and I'd consider it a bug in Teams (private clipboard formats were invented to solve this problem). But, how could this be controlled at the OS level without crippling the user? If a large amount of text is placed in the clipboard, probably it's the user who wanted to move it from one application to another (maybe they are copying a chapter of a book or something like that). The OS shouldn't restrict the clipboard just because some apps abuse its use.
And no, the solution isn't showing an alert interrupting the workflow, which...