convert to UPPER/lowercase

Put simple Tips and Tricks that are not entire Tutorials in this forum
partof
Posts: 110
Joined: 16 Jan 2016, 08:38

convert to UPPER/lowercase

10 Oct 2017, 04:30

I did not find a way to make it work with UTF-8 (any idea?) but here is a working code for Asc characters

Code: Select all

; Alt(!) CTRL(^) shift(+) win(#) win(e) 
#p::  ; convert to UPPER/LOWERCASE

Convert_Inv()
RETURN
Convert_Inv()
{
 Clipboard=  ; set clipboard to ctrl+c
 Send ^c         ; copy highlighted text to clipboard
 ClipWait
 ;MsgBox, %Clipboard%
 Inv_Char_Out:= ""          ; clear variable that will hold output string
 Loop % Strlen(Clipboard) {   ; loop for each character in the clipboard
    Inv_Char:= Substr(Clipboard, A_Index, 1)                 ; isolate the character
    if Inv_Char is upper                                     ; if upper case
       Inv_Char_Out:= Inv_Char_Out Chr(Asc(Inv_Char) + 32) ; convert to lower case
    else if Inv_Char is lower                                ; if lower case
       Inv_Char_Out:= Inv_Char_Out Chr(Asc(Inv_Char) - 32)   ; convert to upper case
    else
       Inv_Char_Out:= Inv_Char_Out Inv_Char                  ; copy character to output var unchanged
 }
 Clipboard=%Inv_Char_Out%
 ClipWait
 Send ^v
Credit: taken here https://autohotkey.com/board/topic/2443 ... -inverted/ and slightly modified)
Last edited by partof on 10 Oct 2017, 05:22, edited 2 times in total.
User avatar
jNizM
Posts: 3183
Joined: 30 Sep 2013, 01:33
Contact:

Re: convert to UPPER/lowercase

10 Oct 2017, 04:34

[AHK] v2.0.5 | [WIN] 11 Pro (Version 22H2) | [GitHub] Profile
User avatar
Exaskryz
Posts: 2882
Joined: 17 Oct 2015, 20:28

Re: convert to UPPER/lowercase

11 Oct 2017, 00:31

For a crude way on shifting to all uppercase, all lowercase, or inverting, you could make an extensive array? Here's sample code for just Upper and Lowercasing; not sure how you'd best approach an Invert. Probably compare if the original character is the lowercase or uppercase with If (Inv_char=characters[Inv_Char]["upper"]) or ["lower"] then assign to the converted string the opposite.

No idea if Format as jNizM links to would work on characters outside the standard 26 (52) character English alphabet.

Code: Select all

global characters:={}
characters:={"é":{lower:"é",upper:"É"},"í":{lower:"í",upper:"Í"}}
string:="éí"
newstring:=Convert(string,"Upper")
MsgBox % newstring
return

Convert(string,case){
Loop % StrLen(string) {
Inv_Char:=SubStr(string,A_Index,1)
If (case="Upper")
converted.=characters[Inv_Char]["upper"]
else If (case="Lower")
converted.=characters[Inv_Char]["lower"]
}
return converted
}
Edit: This doesn't work if you use string:="ÉÍ", because there is no É or Í in the first array; anyone have a better idea regarding the array management? I kind of feel like this would be appropriate, but it's kind of backwards and would eat a lot of time.

Code: Select all

^1::
global characters:=["éÉ","íÍ"]
string:="ÉÍ"
newstring:=Convert(string,"lower")
MsgBox % newstring
newerstring:=Convert(newstring,"upper")
MsgBox % newerstring
return

Convert(string,case){
Loop % StrLen(string) {
Inv_Char:=SubStr(string,A_Index,1)
For k, v in characters
	If pos:=InStr(v,Inv_Char)
	{
    converted.=SubStr(v, case="upper"?2:1 ,1) ; and you could invert by doing pos=1?2:1
	Break
	}
}
return converted
}
User avatar
Xtra
Posts: 2750
Joined: 02 Oct 2015, 12:15

Re: convert to UPPER/lowercase

11 Oct 2017, 02:18

Exaskryz wrote:No idea if Format as jNizM links to would work on characters outside the standard 26 (52) character English alphabet.

Code: Select all

chars := "éÉé"
String := Format("{:U}", chars)
MsgBox,,Upper, % chars . " converted to: " .  string

chars := "ÉéÉ"
String := Format("{:L}", chars)
MsgBox,,Lower, % chars . " converted to: " . string
ExitApp
User avatar
jeeswg
Posts: 6902
Joined: 19 Dec 2016, 01:58
Location: UK

Re: convert to UPPER/lowercase

11 Oct 2017, 07:43

I'm not sure exactly what you're looking for. This example shows how you can take text stored as what I call 'UTF-8 bytes', convert it to UTF-16, make it uppercase, and convert it back to 'UTF-8 bytes'.

The aim of the script is to start with vText2 and end up with vText4, but I start off with vText, to make it clear what vText2 contains.

Code: Select all

q:: ;UTF-8 bytes stored as UTF-16 (lowercase to uppercase)
vText := "áéíóú"

;to UTF-8 bytes stored as UTF-16
vSize := StrPut(vText, "UTF-8")
VarSetCapacity(vTemp, vSize)
StrPut(vText, &vTemp, "UTF-8")
vText2 := StrGet(&vTemp, "CP0")
MsgBox, % vText2

;to UTF-16
vSize := StrPut(vText2, "CP0")
VarSetCapacity(vTemp, vSize)
StrPut(vText2, &vTemp, "CP0")
vText3 := StrGet(&vTemp, "UTF-8")
MsgBox, % vText3

;to uppercase
vText3 := Format("{:U}", vText3)
MsgBox, % vText3

;to UTF-8 bytes stored as UTF-16
vSize := StrPut(vText3, "UTF-8")
VarSetCapacity(vTemp, vSize)
StrPut(vText3, &vTemp, "UTF-8")
vText4 := StrGet(&vTemp, "CP0")
MsgBox, % vText4

MsgBox, % vText2 "`r`n" vText4
return
homepage | tutorials | wish list | fun threads | donate
WARNING: copy your posts/messages before hitting Submit as you may lose them due to CAPTCHA
User avatar
Exaskryz
Posts: 2882
Joined: 17 Oct 2015, 20:28

Re: convert to UPPER/lowercase

11 Oct 2017, 09:19

Xtra wrote:
Exaskryz wrote:No idea if Format as jNizM links to would work on characters outside the standard 26 (52) character English alphabet.

Code: Select all

chars := "éÉé"
String := Format("{:U}", chars)
MsgBox,,Upper, % chars . " converted to: " .  string

chars := "ÉéÉ"
String := Format("{:L}", chars)
MsgBox,,Lower, % chars . " converted to: " . string
ExitApp
Well sweet, it even works on Greek letters: chars:=ΔδΔ. If anyone knows other alphabets, would be cool to see how those are handled.
ozzii
Posts: 481
Joined: 30 Oct 2013, 06:04

Re: convert to UPPER/lowercase

12 Oct 2017, 02:25

The Serbian Cyrillic and Latin is also converted.

Return to “Tips and Tricks (v1)”

Who is online

Users browsing this forum: No registered users and 15 guests