Wish Add B4RSerializator.ConvertBytesToArray with number of bytes to convert

Discussion in 'B4R Bugs & Wishlist' started by miker2069, Sep 16, 2017.

  1. miker2069

    miker2069 Active Member Licensed User

    This is a nice to have request. For example, if you have a 1024 byte array (used as buffer/temp array), but you've only read 32 bytes into it say from an SPIFFS file. The file contained 32 bytes was originally serialized from an array of objects. You then want to convert the 32 bytes back to an object array, the way to do that now is something like this:

    Code:
    Private fs As ESP8266FileSystem
    Public ser As B4RSerializator
    Public bc As ByteConverter

    Dim b(1024As  Byte 'buffer array
    dim filename as string = "/info"

    fs.OpenRead(filename)
    Dim size As ULong = fs.CurrentFile.Size

    Dim readsize As Int = fs.Stream.ReadBytes(b, 0, size)
    'the data that's read into byte array b is 32  bytes in length, it was a previously serialized object array

    'to convert back to object array properly
    Dim buf(3As Object
    Dim obj_a() As Object

    obj_a = ser.ConvertBytesToArray(bc.SubString2(b,
    0,readsize),buf) '<-convert back to object array
    Would be nice to have a version of ConvertBytesToArray, say ConvertBytesToArray2 that you can pass in the number of bytes to read to perform the conversion:

    ConvertBytesToArray2 (Bytes() As Byte, readsize as UInt, ObjectsBuffer() As Object) As Object()

    Code:
    obj_a = ser.ConvertBytesToArray2(b,readsize, buf)
     
  2. Erel

    Erel Administrator Staff Member Licensed User

    The correct way to do it is with bc.SubString2 as you did it.

    In this case you know the correct size. It will be fs.CurrentFile.Size = size = readsize. Just declare the b array after you know the size.
     
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