Jump to content

Sky Slate Blueberry Blackcurrant Watermelon Strawberry Orange Banana Apple Emerald Chocolate
Photo

sample function to read unicode clipboard to plain ansi


  • Please log in to reply
1 reply to this topic
wOxxOm
  • Members
  • 371 posts
  • Last active: Feb 20 2015 12:10 PM
  • Joined: 09 Feb 2006
I use this function only to get unicode text from clipboard so it doesn't have a wrapper to save clipboard text which would be useful for conversion anywhere in your code. Example is coded using info from eggheads.experts. Extremely fast and reliable. Tested with cyrillic (russian) names only.

clipAnsi()
{
   StringLen,L,Clipboard
   L:=(L+1)*4
   transform,ca_Clip,unicode ; get clipboard text in UTF-8
   varSetCapacity(ca_WideText,L,0) ; allocate buffer for 2-byte-char string
   varSetCapacity(ca_AnsiText,L,0) ; alloc for resulting ansi string
   ; Convert UTF-8 to UTF-16.   CP_UTF8=65001
   if dllCall("MultiByteToWideChar",uint,65001, uint,0, str,ca_Clip
              , unit,-1, str,ca_WideText, uint,L/2)
      dllCall("WideCharToMultiByte",uint,0, uint,0, str,ca_WideText
              , unit,-1, str,ca_AnsiText, uint,L/2, uint,0, uint,0)
      ; Convert UTF-16 to ANSI.  CP_ACP=0
   return ca_AnsiText
}

Another function is to set clipboard in unicode (through UTF-8 conversion) to an ansi string containing non-western characters from current system codepage.
clipSetUnicode(cu_AnsiText) ; copy ansi string to clipboard in unicode mode 
{
   StringLen,L,cu_AnsiText
   L:=(L+1)*4
   varSetCapacity(cu_WideText,L,0)
   varSetCapacity(cu_UTFtext,L,0)
   ; ANSI to UTF-16.   CP_ACP=0
   if dllCall("MultiByteToWideChar",uint,0, uint,0, str,cu_AnsiText
              , uint,-1, str,cu_WideText, uint,L/2)
      dllCall("WideCharToMultiByte",uint,65001, uint,0, str,cu_WideText
              , uint,-1, str,cu_UTFtext, uint,L/2, uint,0, uint,0)
      ; Convert UTF-16 to UTF-8.  CP_UTF8=65001
   transform,clipboard,unicode,%cu_UTFtext%
}


aarondellis
  • Members
  • 57 posts
  • Last active: Feb 11 2013 02:18 PM
  • Joined: 15 Aug 2005
Thanks! I might have a use for this only in reverse. I have clipboard contents in ASCII and need to change to Unicode.

Thanks for sharing!