Home > Blockchain >  Deflate floating point data bytes encoded as Base64
Deflate floating point data bytes encoded as Base64

Time:03-23

Good Day!

I would like ask for your help on decompressing String back to its original data.

Here's the document that was sent to me by the provider.

Data description

First part describes the threshold data.

enter image description here

All data are managed as Little Endian IEEE 754 single precision floating numbers. Their binary representation are (represented in hexadecimal data) :

enter image description here

Compressed data (zip) Threshold binary data are compressed using the ‘deflate’ algorithm. Each compression result is given here (represented in hexadecimal data) :

Thresholds: $63 00 03 05 47 24 DA 81 81 A1 C1 9E 81 61 01 98 06 00

Encoded data (base64) Threshold compressed data are encoded in ‘base64’ to be transmitted as ASCII characters. Each conversion results is given here (represented in hexadecimal data) :

Thresholds: $59 77 41 44 42 55 63 6B 32 6F 47 42 6F 63 47 65 67 57 45 42 6D 41 59 41

Here is the output frame (Manufacturer frame content) The thresholds data are then sent using their corresponding ASCII character Here is the resulting Histogram ASTM frame sent :

YwADBUck2oGBocGegWEBmAYA

As explained in above details, what I want to do is backwards.

The packets that we received is

YwADBUck2oGBocGegWEBmAYA

then from there convert it to Hex value Base64 which is the output is.

Thresholds: $59 77 41 44 42 55 63 6B 32 6F 47 42 6F 63 47 65 67 57 45 42 6D 41 59 41

This first part was already been implemented using this line of codes.

Function String2Base64(val_string As String) As String
    Dim hex As String
    For i As Integer = 0 To val_string.Length - 1
        hex &= Asc(val_string.Substring(i, 1)).ToString("x").ToUpper
    Next
    Return hex
End Function

However we are stuck in the next process which is deflate to floating numbers.

My question is how can we do this in VB.net so that we can get the output below values in blue font.

enter image description here

Thank you and god bless you all.

CodePudding user response:

Your input string is a base64 encoded array of bytes, representing a compressed (deflated) sequence of floating point values (float / Single).

  • You can use Convert.FromBase64String() to get the compressed bytes
  • Initialize a MemoryStream with this byte array. It's used as the input stream of a DeflateStream
  • Initialize a new MemoryStream to receive the deflated content from the DeflateStream.CopyTo() method
  • Get a series of 4 bytes from the decompressed array of bytes and reconstruct the original values.

An example:

Imports System.IO
Imports System.IO.Compression

Dim base64 = "YwADBUck2oGBocGegWEBmAYA"
Dim decodedBytes = Convert.FromBase64String(base64)

Dim outputData As New List(Of Single)

Using inputStream = New MemoryStream(decodedBytes),
    outputStream = New MemoryStream(),
    defStream = New DeflateStream(inputStream, CompressionMode.Decompress)
    defStream.CopyTo(outputStream)
    defStream.Flush()

    Dim dataArray = outputStream.ToArray()

    For pos As Integer = 0 To dataArray.Length - 1 Step 4
        ' .Net Framework
        ' outputData.Add(BitConverter.ToSingle(New ArraySegment(Of Byte)(dataArray, pos, 4).ToArray(), 0))
        ' .Net 5 
        outputData.Add(BitConverter.ToSingle(New ArraySegment(Of Byte)(dataArray, pos, 4)))
    Next
End Using

The content of outputData is now:

0
10
0
10
2
1
5
1
  • Related