Android Code Snippet String from object, object from string

Discussion in 'Code Snippets' started by AndOrNot, Feb 10, 2017.

Thread Status:
Not open for further replies.
  1. AndOrNot

    AndOrNot Well-Known Member Licensed User

    Hi all, i want to share the method that i use to get string from object and vice versa

    How to use:
    Code:
    Sub Activity_Create(FirstTime As Boolean)
        
    Dim list1 As List
        list1.Initialize
        
    For i = 1 To 100
            list1.Add(
    "List item n° " & i)
        
    Next
       
       
        
    Dim s As String
        s = GetStringFromObject(
    "Yourpsw",list1)
       
        list1 = GetObjectFromString(
    "Yourpsw",s)
        
    Log(list1)
       
    End Sub
    Code:
    Sub GetStringFromObject (psw As String, obj As Object) As String
        
    Dim raf As RandomAccessFile
        raf.Initialize(
    File.DirInternalCache, "temp.dat"False)
        raf.WriteEncryptedObject(obj, psw, raf.CurrentPosition)
        raf.Close
        
    Dim su As StringUtils
        
    Dim b64 As String = su.EncodeBase64(Bit.InputStreamToBytes(File.OpenInput(File.DirInternalCache, "temp.dat")))
        
    File.Delete(File.DirInternalCache,"temp.dat")
        
    Return b64
    End Sub
    Code:
    Sub GetObjectFromString (psw As String, str As StringAs Object
        
    Dim su As StringUtils
        
    Dim raf As RandomAccessFile
        raf.Initialize3(su.DecodeBase64(str),
    False)
        
    Dim obj As Object = raf.ReadEncryptedObject(psw, raf.CurrentPosition)
        raf.Close
        
    Return obj
    End Sub
     
    Myr0n likes this.
  2. Erel

    Erel Administrator Staff Member Licensed User

    Why are you using WriteEncryptedObject / ReadEncryptedObject?

    You should use B4XSerializator instead.

    It is faster, cross platform and also can also be used asynchronously.
    Code:
    Sub ObjectToString(obj As Object) As String
       
    Dim ser As B4XSerializator
       
    Dim su As StringUtils
       
    Return su.EncodeBase64(ser.ConvertObjectToBytes(obj))
    End Sub

    Sub StringToObject(s As StringAs Object
       
    Dim ser As B4XSerializator
       
    Dim su As StringUtils
       
    Return ser.ConvertBytesToObject(su.DecodeBase64(s))
    End Sub
     
    AndOrNot and MarcoRome like this.
  3. AndOrNot

    AndOrNot Well-Known Member Licensed User

    Yeah, i use it only because i can encrypt the object
     
  4. Erel

    Erel Administrator Staff Member Licensed User

    If you need to encrypt the data then you should use B4XCipher to encrypt the raw bytes.
     
  5. dpann

    dpann Member Licensed User

    Hi All,
    I tried to convert a String to an Object with almost the same code as above :
    (StringUtils Library is enabled)

    Dim seri As B4XSerializator
    Dim my_name = "name" As String
    Dim su As StringUtils
    Dim obj As Object
    obj= seri.ConvertBytesToObject(su.DecodeBase64(my_name))

    The last line throws an exception :
    Caused by: java.util.zip.DataFormatException: incorrect header check

    Any explanation ?
    I already spent hours to make it work without any success

    Thanks for any answer
     
  6. OliverA

    OliverA Expert Licensed User

    1) B4XSerializator's ConvertBytesToObject should be used for an array of bytes that was created by B4XSerializator's ConvertObjectToBytes. Since you did not do that (you are feeding it an array of bytes generated by StringUtils' DecodeBase64), you are getting that error message.

    2) Additionally: DecodeBase64 expects a Base64 encode string. You're just feeding it a plain string which will result in garbage.
     
  7. dpann

    dpann Member Licensed User

    Thanks a lot for your help.
    You are totally right ! Therefore, I modified the code as :

    Dim seri As B4XSerializator
    Dim my_name = "name" As String
    Dim buffer() As Byte
    Dim obj As Object
    buffer= seri.ConvertObjectToBytes(my_name) ' Convert plain string to byte()
    obj= seri.ConvertBytesToObject(buffer) ' Convert byte() to Object

    and it works !!!!!!

    Maybe there is a more simple solution to convert plain string to object, but at least this one is working.
    I am now wandering if there is a quick solution to encode a plain string to Base 64 format ? (I am very new with B4A and learning a lot ...)

    Anyway you solved my problem and you deserve a lot of congratulations for that
     
  8. OliverA

    OliverA Expert Licensed User

    That's a new question for a new thread
     
Thread Status:
Not open for further replies.
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