E1M1 Posted October 22, 2010 Posted October 22, 2010 Ok Goel is get number to string using logic 1 = A; 2 = B; 3 = C; ... 26 = Z; 27 = AA; 28 = AB... Problem: 26 gets AZ, not Z; Also 27 returns AA as expected. Only have problem with 26... and maybe some other nr that i didnt test yet. ConsoleWrite(ToBase26(26) & @CRLF) Func ToBase26($i) Local $s = "" Do $s = Chr(Mod($i, 26) + 64) & $s $i = Int($i / 26) Until $i = 0 Return StringReplace($s, "@", "Z") EndFunc ;==>ToBase26 Replacing $i = Int($i / 26) with $i = Int($i / 27) fixes problem with 26, but it causes problems with other ints (randomly). Testing with 1000000000 must return CFDGSXL; testing with 321272407 must return AAAAAAA. edited
jaberwacky Posted October 22, 2010 Posted October 22, 2010 (edited) The problem lies here: Chr(Mod($i, 26) + 64) When $i = 26 then Mod(26, 26) will equal 1 and then add 64 to that and you get 65 all over again Edited October 22, 2010 by jaberwocky6669 Helpful Posts and Websites: AutoIt3 Variables and Function Parameters MHz | AutoIt Wiki | Using the GUIToolTip UDF BrewManNH | Can't find what you're looking for on the Forum?
martin Posted October 22, 2010 Posted October 22, 2010 The problem lies here: Chr(Mod($i, 26) + 64) When $i = 26 then Mod(26, 26) will equal 1 and then add 64 to that and you get 65 all over again Not quite because Mod($n,$n) = 0, but mod can be confusing. I sometimes want to use mod to get a number from 1 to 4 say from any number. Maybe when I'm building rows of things for example. Mod(1,4) = 1 as wanted, but Mod(4,4) = 0 but I want 4. This is how I do it $result = Mod($i-1,4) + 1 or in your case $result = Mod($i-1,26) + 1 ie Chr(Mod($1-1,26) + 1 + Asci('A') - 1) -> Chr(Mod($i - 1, 26) + 65) Serial port communications UDF Includes functions for binary transmission and reception.printing UDF Useful for graphs, forms, labels, reports etc.Add User Call Tips to SciTE for functions in UDFs not included with AutoIt and for your own scripts.Functions with parameters in OnEvent mode and for Hot Keys One function replaces GuiSetOnEvent, GuiCtrlSetOnEvent and HotKeySet.UDF IsConnected2 for notification of status of connected state of many urls or IPs, without slowing the script.
E1M1 Posted October 22, 2010 Author Posted October 22, 2010 $s = Chr(Mod($i-1, 26) + 65) & $s makes 26 to AZ edited
cembry90 Posted October 22, 2010 Posted October 22, 2010 (edited) Ahh, I see that you're trying to use more than one letter in a number. Nevermind. Simple, but gets the job done. $num = 26 MsgBox(262144, "", numToBase26($num)) Func numToBase26($num) Return Chr($num+64) EndFunc Edited October 22, 2010 by cembry90 AutoIt Stuff: UDFs: {Grow}
E1M1 Posted October 22, 2010 Author Posted October 22, 2010 cembry90 if you test your function, you see that it also returns non A-Z chars edited
MvGulik Posted October 22, 2010 Posted October 22, 2010 $s = Chr(Mod($i-1, 26) + 65) & $s makes 26 to AZCorrect.Don't know a fix. But the reason is that "int(26/26) = 1 -> 'A' & $s(Z) "Straight_and_Crooked_Thinking" : A "classic guide to ferreting out untruths, half-truths, and other distortions of facts in political and social discussions.""The Secrets of Quantum Physics" : New and excellent 2 part documentary on Quantum Physics by Jim Al-Khalili. (Dec 2014) "Believing what you know ain't so" ... Knock Knock ...
MvGulik Posted October 22, 2010 Posted October 22, 2010 (edited) If StringLeft($s,1)=='Z' Then $i -= 1 --- Func ToBase26($i) If $i < 1 then Return SetError(1) Local $s = "" Do $s = Chr(Mod($i - 1, 26) + 1 + 64) & $s $i = Int($i/26) - Number(Mod($i, 26)=0) Until $i = 0 Return $s EndFunc ;==>ToBase26 Edited October 22, 2010 by MvGulik "Straight_and_Crooked_Thinking" : A "classic guide to ferreting out untruths, half-truths, and other distortions of facts in political and social discussions.""The Secrets of Quantum Physics" : New and excellent 2 part documentary on Quantum Physics by Jim Al-Khalili. (Dec 2014) "Believing what you know ain't so" ... Knock Knock ...
E1M1 Posted October 22, 2010 Author Posted October 22, 2010 didn't help. Func ToBase26($i) Local $s = "" Do $s = Chr(Mod($i, 26) + 64) & $s If StringLeft($s,1)=='Z' Then $i -= 1 $i = Int($i / 26) Until $i = 0 Return StringReplace($s, "@", "Z") EndFunc ;==>ToBase26 edited
MiserableLife Posted October 22, 2010 Posted October 22, 2010 (edited) Does this help?Mod ( value1, value2 )Return Value Success: Returns the remainder when value1 is divided by value2. Failure: Returns -1.#IND if the divisor is zero.If Mod( 26, 26) Then it = ( 26 / 26 = 1 + remains nothing ). So Mod(26,26) = 0 is correct ???? Then Chr(0 + 64) = @ ?????$s = "" $count = 0 $i = 321272407 ConsoleWrite('$i = ' & $i & @CRLF & @CRLF) Do $count += 1 ConsoleWrite('Loop ' & $count & @CRLF) If $i = 26 Then $mod = 26 Else $mod = Mod($i, 26) EndIf ConsoleWrite('Mod($i, 26) = ' & $mod & @CRLF) $ASCII = $mod + 64 ConsoleWrite('$mod + 64 = ' & $ASCII & @CRLF) $chr = Chr($ASCII) ConsoleWrite('Chr($ASCII) = ' & $chr & @CRLF) $s = $chr & $s ConsoleWrite('$chr & $s = ' & $s & @CRLF) $mod2 = Mod($i, 26) ConsoleWrite('Mod($i, 26) = ' & $mod & @CRLF) $number = Number($mod2 = 0) ConsoleWrite('Number($mod2 = 0) = ' & $number & @CRLF) $int = Int($i/26) ConsoleWrite('Int($i/26) = ' & $mod & @CRLF) $i = $int - $number ConsoleWrite('$int - $number = ' & $i & @CRLF) ConsoleWrite(@CRLF) Until $i = 0 ConsoleWrite('$s = ' & $s & @CRLF)ConsoleWrite( ToBase26(26) & @CRLF) Func ToBase26($i) Local $s = "" Do If $i = 26 Then $mod = 26 Else $mod = Mod($i, 26) EndIf $s = Chr($mod + 64) & $s $i = Int($i/26) - Number(Mod($i, 26) = 0) Until $i = 0 Return $s EndFunc ;==>ToBase26 Edited October 22, 2010 by MiserableLife
czardas Posted October 22, 2010 Posted October 22, 2010 For $j = 1 To 100 ConsoleWrite(ToBase26($j) & @CRLF) Next Func ToBase26($i) Local $s = "" Do $s = Chr(Mod($i, 26) + 64) & $s $i = Int(($i -1) / 26) Until $i = 0 Return StringReplace($s, "@", "Z") EndFunc ;==>ToBase26 Lol! operator64 ArrayWorkshop
jaberwacky Posted October 22, 2010 Posted October 22, 2010 The mouse runs under the cats nose. Helpful Posts and Websites: AutoIt3 Variables and Function Parameters MHz | AutoIt Wiki | Using the GUIToolTip UDF BrewManNH | Can't find what you're looking for on the Forum?
martin Posted October 22, 2010 Posted October 22, 2010 For $j = 1 To 100 ConsoleWrite(ToBase26($j) & @CRLF) Next Func ToBase26($i) Local $s = "" Do $s = Chr(Mod($i, 26) + 64) & $s $i = Int(($i -1) / 26) Until $i = 0 Return StringReplace($s, "@", "Z") EndFunc ;==>ToBase26 Lol! Maybe this is a little bit simpler For $j = 1 To 100 ConsoleWrite(ToBase26($j) & @CRLF) Next Func ToBase26($i) if $i = 0 then return "" return ToBase26(int(($i-1)/26)) & Chr(Mod($i - 1, 26) + 65) EndFunc ;==>ToBase26 Serial port communications UDF Includes functions for binary transmission and reception.printing UDF Useful for graphs, forms, labels, reports etc.Add User Call Tips to SciTE for functions in UDFs not included with AutoIt and for your own scripts.Functions with parameters in OnEvent mode and for Hot Keys One function replaces GuiSetOnEvent, GuiCtrlSetOnEvent and HotKeySet.UDF IsConnected2 for notification of status of connected state of many urls or IPs, without slowing the script.
czardas Posted October 22, 2010 Posted October 22, 2010 (edited) Well I didn't think the method was ideal, and just as I was about to post this, Martin sorted everything out. Very nice example too Martin. Edited October 22, 2010 by czardas operator64 ArrayWorkshop
E1M1 Posted October 22, 2010 Author Posted October 22, 2010 (edited) Thanks alot, You guys rock. That shortened version by martin looks really cool way. Didn't thing such ching could be dont with out any loops at all. Edit: I guess martin has extraterrestrial intelligence. Edited October 22, 2010 by E1M1 edited
czardas Posted October 22, 2010 Posted October 22, 2010 Indeed, it could do with some study. operator64 ArrayWorkshop
E1M1 Posted October 22, 2010 Author Posted October 22, 2010 I wonder what grade/education marting has.. edited
czardas Posted October 22, 2010 Posted October 22, 2010 I don't believe there is such a category. Anyway, I need to absorb this a little while. operator64 ArrayWorkshop
Recommended Posts
Create an account or sign in to comment
You need to be a member in order to leave a comment
Create an account
Sign up for a new account in our community. It's easy!
Register a new accountSign in
Already have an account? Sign in here.
Sign In Now