Jump to content

Problems with arrays


Recommended Posts

In my current project I have a part that should convert from string to array, and later on that array should be converted to a string again.

The first part works just fine and I get the array just as I want it, but on the way back I'm encountering some issues...

Func Encode($data0)

    ; works just fine, just here for clarification.
    ; That if-statement in the middle is just there that I'm always getting a string with 5 symbols
    ; $data0 = "A" in this example

    Local $data1 = ""
    Local $tmp = ""
    Local $arr = StringSplit($data0, "", 2)

    For $i = 0 To UBound($arr)-1

        $tmp = AscW($arr[$i])

        If $tmp = 0 Then
            $tmp = "00000" & $tmp
        ElseIf $tmp > 0 And $tmp < 10 Then
            $tmp = "0000" & $tmp
        ElseIf $tmp >= 10 And $tmp < 100 Then
            $tmp = "000" & $tmp
        ElseIf $tmp >= 100 And $tmp < 1000 Then
            $tmp = "00" & $tmp
        ElseIf $tmp >= 1000 And $tmp < 10000 Then
            $tmp = "0" & $tmp
        ElseIf $tmp >= 10000 And $tmp <= 65535 Then
            $tmp = $tmp
        EndIf

        $data1 = $data1 & $tmp

    Next

    Return $data1

EndFunc
Func Decode($data0)

    ; does NOT work
    ; $data0 = "00065"
    ; via MsgBox and ConsoleWrite I could find out that it fails already before StringSplit
    ; when it fails, no message is given and everything just disappears

    Local $data1 = ""
    Local $tmp = ""
    Local $arr = StringSplit(String($data0), "", 2)

    For $i = 0 To UBound($arr)-1 Step 5

        $tmp = $arr[$i] & $arr[$i+1] & $arr[$i+2] & $arr[$i+3] & $arr[$i+4] & $arr[$i+5]
        $tmp = ChrW($tmp)
        $data1 = $data1 & $tmp

    Next

    Return $data1

EndFunc

Sorry for dropping all the #include, the GUI, the login screen, etc... It's not necessary for this problem and I needn't give you a 613kB file full of plain text for finding an error in 3 lines of code.

I found out that $arr is always empty, so there must be an issue with StringSplit. I suspected "00065" might be seen as an integer and converted it to a string, but that doesn't make it work. I already played around quite a while with that issue now, I hope some of you can finally relieve me.

Thanks

Link to comment
Share on other sites

14 minutes ago, alien4u said:

Sorry you already mention it.

So:

$data0 = "A" in this example

Then StringSplit() here in the first step is working because is only one character and you use a null delimiter:

Local $arr = StringSplit($data0, "", 2)

What is happening is in your second stage you have a bigger string with no Delimiters so the StringSplit() is not working properly.

Regards
Alien.

Actually, using "ABC" in the first step is working too. 

Edit: Using delimiters, when there are none, is also a bit difficult :/

Edited by Wicked_Caty
Link to comment
Share on other sites

1 hour ago, Wicked_Caty said:

Actually, using "ABC" in the first step is working too. 

Edit: Using delimiters, when there are none, is also a bit difficult :/

You are right sorry I just miss read it.

You could try to use IsInt() or IsString function to know if you are right and I think you are.

Regards
Alien.

Link to comment
Share on other sites

5 minutes ago, AutoBert said:

in your decode func is the line comcatenating string wrong, you try using 1 element to much  (0,1,2,3,4,5=6 elements)m correct it to: 

For $i = 0 To UBound($arr)-1 Step 5

        $tmp = $arr[$i] & $arr[$i+1] & $arr[$i+2] & $arr[$i+3] & $arr[$i+4]
        $tmp = ChrW($tmp)
        $data1 = $data1 & $tmp

    Next

 

Thanks. I really didn't expect the problem there. I'm coding for almost 2 years now in several languages, but I still get confused by starting to count at zero xD

Link to comment
Share on other sites

If you're saying  $data0 fails before the string split(),  which is right at the beginning of decode (). I'd have to assume there is a problem with the variable in another part of the program. 

Or I could be completely misinterpreting what you mean by "fails"

Edited by markyrocks
Link to comment
Share on other sites

You could use some integrated String* funcs

$data0 = "ABC"

$test1 = Encode($data0)
Msgbox(0,"encode", $test1) 

$test2 =  Decode($test1)
Msgbox(0,"decode", $test2)


Func Encode($data)
    Local $data1 = ""
    Local $arr = StringSplit($data, "", 2)
    For $i = 0 To UBound($arr)-1
        $data1 &= StringFormat("%05s", AscW($arr[$i]))
    Next
    Return $data1
EndFunc

Func Decode($data)
    Local $data1 = ""
    Local $arr = StringRegExp($data, '.{5}', 3)
    For $i = 0 To UBound($arr)-1
        $data1 &= ChrW($arr[$i])
    Next
    Return $data1
EndFunc

 

Edit
For the fun only

$data0 = "ABC"

$test1 = Encode($data0)
Msgbox(0,"encode", $test1) 

$test2 =  Decode($test1)
Msgbox(0,"decode", $test2)


Func Encode($data)
   Return Execute('"' & StringRegExpReplace($data, '.', '" & StringFormat("%05s", AscW("$0")) & "') & '"')
EndFunc

Func Decode($data)
   Return Execute('"' & StringRegExpReplace($data, '.{5}', '" & ChrW("$0") & "') & '"')
EndFunc

 

Edited by mikell
Link to comment
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
 Share

×
×
  • Create New...