Wicked_Caty Posted April 13, 2016 Posted April 13, 2016 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
alien4u Posted April 13, 2016 Posted April 13, 2016 (edited) Wrong Suggestion again I just miss read it. Regards Alien. Edited April 13, 2016 by alien4u Wrong Suggestion, see next 2 post.
Wicked_Caty Posted April 13, 2016 Author Posted April 13, 2016 1 minute ago, alien4u said: Could you provide example Input data? the one you make the first StringSplit? Regards Alien. Is in the comments I added at the beginning of the files. In Encode() it's "A" and in Decode() it's "00065"
Wicked_Caty Posted April 13, 2016 Author Posted April 13, 2016 (edited) 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 April 13, 2016 by Wicked_Caty
alien4u Posted April 13, 2016 Posted April 13, 2016 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.
Wicked_Caty Posted April 13, 2016 Author Posted April 13, 2016 2 minutes ago, alien4u said: You could try to use IsInt() or IsString function to know if you are right and I think you are. $data0 in the second function is a string indeed.
AutoBert Posted April 13, 2016 Posted April 13, 2016 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
Wicked_Caty Posted April 13, 2016 Author Posted April 13, 2016 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
markyrocks Posted April 13, 2016 Posted April 13, 2016 (edited) 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 April 13, 2016 by markyrocks Spoiler "I Believe array math to be potentially fatal, I may be dying from array math poisoning"
mikell Posted April 14, 2016 Posted April 14, 2016 (edited) 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 April 14, 2016 by mikell
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