Sign in to follow this  
Followers 0
E1M1

what makes arg value 26 buggy?

19 posts in this topic

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

Share this post


Link to post
Share on other sites



#2 ·  Posted (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 by jaberwocky6669

Share this post


Link to post
Share on other sites

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.

Share this post


Link to post
Share on other sites

$s = Chr(Mod($i-1, 26) + 65) & $s makes 26 to AZ


edited

Share this post


Link to post
Share on other sites

#5 ·  Posted (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 by cembry90

AutoIt Stuff:

 

UDFs: {Grow}

Share this post


Link to post
Share on other sites

cembry90 if you test your function, you see that it also returns non A-Z chars


edited

Share this post


Link to post
Share on other sites

$s = Chr(Mod($i-1, 26) + 65) & $s makes 26 to AZ

Correct.

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 ...
 

Share this post


Link to post
Share on other sites

yheap, i know that already.


edited

Share this post


Link to post
Share on other sites

#9 ·  Posted (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 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 ...
 

Share this post


Link to post
Share on other sites

#11 ·  Posted (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 by MiserableLife

Share this post


Link to post
Share on other sites

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.

Share this post


Link to post
Share on other sites

#15 ·  Posted (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 by czardas

Share this post


Link to post
Share on other sites

#16 ·  Posted (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 by E1M1

edited

Share this post


Link to post
Share on other sites

I wonder what grade/education marting has..


edited

Share this post


Link to post
Share on other sites

I don't believe there is such a category. Anyway, I need to absorb this a little while.

Share this post


Link to post
Share on other sites

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 account

Sign in

Already have an account? Sign in here.


Sign In Now
Sign in to follow this  
Followers 0