AutoHotkey Homepage AutoHotkey Community
Let's help each other out
 
 FAQFAQ   SearchSearch   MemberlistMemberlist   RegisterRegister 
 ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

sample function to read unicode clipboard to plain ansi

 
Post new topic   Reply to topic    AutoHotkey Community Forum Index -> Scripts & Functions
View previous topic :: View next topic  
Author Message
wOxxOm



Joined: 09 Feb 2006
Posts: 319

PostPosted: Thu Mar 02, 2006 2:48 pm    Post subject: sample function to read unicode clipboard to plain ansi Reply with quote

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.

Code:
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.
Code:
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%
}


Last edited by wOxxOm on Tue Jan 01, 2008 7:16 am; edited 2 times in total
Back to top
View user's profile Send private message Send e-mail Visit poster's website
aarondellis



Joined: 15 Aug 2005
Posts: 57

PostPosted: Thu Mar 02, 2006 3:00 pm    Post subject: thanks for sharing Reply with quote

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!
Back to top
View user's profile Send private message
Display posts from previous:   
Post new topic   Reply to topic    AutoHotkey Community Forum Index -> Scripts & Functions All times are GMT
Page 1 of 1

 
Jump to:  
You can post new topics in this forum
You can reply to topics in this forum


Powered by phpBB © 2001, 2005 phpBB Group