converting a StringArray to a ByteArray

Discussion in 'Questions (Windows Mobile)' started by Cableguy, Jan 3, 2009.

  1. Cableguy

    Cableguy Expert Licensed 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...
     
  2. Erel

    Erel Administrator Staff Member Licensed User

    StrSplit returns an array of strings.
    You will need to use something like:
    Code:
    Sub Globals
     
    dim str(0as String
     
    dim key(0as byte
    End Sub
    ...
    str() = StrSplit(...)
    dim key(ArrayLen(str()) as Byte
    For i = 0 to ArrayLen(str()) - 1
     key(i) = str(i)
    Next
     
  3. Cableguy

    Cableguy Expert Licensed 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:
    Code:
    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????
     
  4. Erel

    Erel Administrator Staff Member Licensed User

    I think that the problem is that n = 0.
    What is the value of CodeString?
     
  5. Cableguy

    Cableguy Expert Licensed 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...

    Code:
    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: Jan 3, 2009
  6. agraham

    agraham Expert Licensed User

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

    Cableguy Expert Licensed 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: Jan 3, 2009
  8. agraham

    agraham Expert Licensed User

    That picture shows that you have both a Sub and an Array called "Decode". That's probably the problem :(
     
  9. Cableguy

    Cableguy Expert Licensed 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: Jan 3, 2009
  10. Erel

    Erel Administrator Staff Member Licensed User

    I will file it as a bug. There is no reason for the IDE to crash because of this conflict.
     
Loading...
  1. This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
    By continuing to use this site, you are consenting to our use of cookies.
    Dismiss Notice