converting a StringArray to a ByteArray

Cableguy

Expert
Licensed User
Longtime User
Hi guys...

I am stuck at this...
I have a crypto module to encrypt and decrypt some data...
The encrypt works fine and I get the encrypted data and encrypted key as an array and the save it as a single line string, seperating the values with commas.
My problem is that even Dimming the Array as Byte, when I load and seperate the values to the array, they are "read" as string and not as byte..
how can i Change the data type without changing the values?????

Example:

String="122,10,4,23,65,9,76,88" 'each individual value is a BYTE

Key()=StrPlit(String,",") ' EVEN if i declare this as a Byte array, it gets treated as strings...
 

Cableguy

Expert
Licensed User
Longtime User
Thanks EREL, I've already tried that but in a diferent way, so didn't work..
Your's does work like a charm...
Now I have a new problem...
I'm passing two strings that need to be converted to bytes, using your code..
The first on works good, and I trimmed it a bit...
The second one is giving me some troubles...
Here's my code:
B4X:
   CodeStr()=StrSplit(CodeString,",") 
   n=ArrayLen(codestr())
   Dim Decode(n) As Byte
   Msgbox(ArrayLen(Decode()))'This returns 0
'   For i = 0 To ArrayLen(Decode())-1
'   Decode(i) = DecodeStr(i)
'   Msgbox(Decodestr(i))
'   Next

I commented the line to troubleshoot...
My problem is that Decode is comming out empty.....Why????
 

Cableguy

Expert
Licensed User
Longtime User
IF I put This:

CodeStr()=StrSplit(CodeString,",")
For x=0 to ArrayLen(CodeStr())-1
MsgBox(CodeStr(x))
Next

All the values that I KNOW exist in the string are correctly shown..
Furthermore if I add:
n=ArrayLen(CodeStr())
MsgBox(n)

I get "16" that is the correct value, but that can be either bigger or smaller, depending on user input...
Now the program just crashes the IDE...
I'm trully Puzzled...

EDIT:
Further narowing...

B4X:
DecodeStr()=StrSplit(CodeString,",") 
   Dim Decode(ArrayLen(DecodeStr())) As Byte
   For a = 0 To ArrayLen(DecodeStr())-1
'   Decode(a) = DecodeStr(a)
   Msgbox(Decodestr(a))
   Next

Shows the correct values for the "DecodeStr" wich is a string array, but if I un-comment the "Decode(a)=DecodeStr(a)" line the IDE CRASHES...
 
Last edited:

agraham

Expert
Licensed User
Longtime User
"Decode(a)=DecodeStr(a)" line the IDE CRASHES...
Define "Crash", any error meassage? That fragment works for me as long as the values in DecodeStr are between 0 and 255 otherwise I get an error message that the value was too small or too large for an unsigned byte.
 

Cableguy

Expert
Licensed User
Longtime User
It Just crashes...see picture below...
The key is just this:
157,123,27,0,188,126,224,121,31,168,128,75,111,154,243,179
 
Last edited:

Cableguy

Expert
Licensed User
Longtime User
Again you are RIGHT!!!!!
Thanks Andrew....
Sometimes coming up with names that make sense is a bit tricky....
I was so puzzled about the situation that I didn't even remember to look at the vars names and lok for conflicts....
 
Last edited:
Top