Jump to content

Merge two or more binary parts, how?!


JScript
 Share

Recommended Posts

I've put the script in code tag and I lost access to the previous topic!

Can someone explain me how I do this?

Here's the code:

#Include <Memory.au3>

$bVar = StringToBinary("JScript")

$TcpRxLen = 256

$tMemB = DllStructCreate("byte[" & $TcpRxLen & "]") ; Create struct

DllStructSetData($tMemB, 1, $bVar)

$bOld = DllStructGetData($tMemB, 1)

DllStructSetData($tMemB, 1, $bOld & StringToBinary(" - João Carlos."))

MsgBox(4096, "Teste", BinaryToString(DllStructGetData($tMemB, 1)))

Thanks,

João Carlos.

http://forum.autoitbrasil.com/ (AutoIt v3 Brazil!!!)

Somewhere Out ThereJames Ingram

somewh10.png

dropbo10.pngDownload Dropbox - Simplify your life!
Your virtual HD wherever you go, anywhere!

Link to comment
Share on other sites

I don't know what do you mean by merge binary parts but I hope it will useful:

Easier way (not using DLLStructure):

$Name = StringToBinary("Ramzes")
$String = StringToBinary("abcdef")
$Val1 = StringToBinary("jscript")
MsgBox(32, "",  BinaryToString($Name) & @CR &  BinaryToString($String) & @CR &  BinaryToString($Val1))

Using DLLStructure :

Global $Array[4] = ["black", "red", "white", "yellow"]
$Str = ""
For $i = 0 To UBound($Array) - 1
$Str &= "byte[" & StringLen($Array[$i]) & "];"
Next
$Str = DllStructCreate($Str)
For $i = 0 To UBound($Array) - 1
DllStructSetData($Str, $i + 1, $Array[$i])
Next
MsgBox(32, "", BinaryToString(DllStructGetData($Str, 4)))

Sorry for my bad English but nobody is perfect. [font=arial, helvetica, sans-serif]Ramzes[/font]

Link to comment
Share on other sites

Link to comment
Share on other sites

@Ramzes,

Works as It should! Thanks!

@Yashied,

Great, but works with binary files jpg images?

João Carlos.

Edited by jscript

http://forum.autoitbrasil.com/ (AutoIt v3 Brazil!!!)

Somewhere Out ThereJames Ingram

somewh10.png

dropbo10.pngDownload Dropbox - Simplify your life!
Your virtual HD wherever you go, anywhere!

Link to comment
Share on other sites

@Ramzes,

No, assuming that I already have an image being transmitted over the network in binary format, on the other computer I'll have to join the binary parts to compose the picture again, but I was making use of Hex ($ bBmp) to join parties.

João Carlos.

http://forum.autoitbrasil.com/ (AutoIt v3 Brazil!!!)

Somewhere Out ThereJames Ingram

somewh10.png

dropbo10.pngDownload Dropbox - Simplify your life!
Your virtual HD wherever you go, anywhere!

Link to comment
Share on other sites

Wouldn't it be easier to open the files in binary mode and then you wouldn't need to convert?

Serial port communications UDF Includes functions for binary transmission and reception.printing UDF Useful for graphs, forms, labels, reports etc.Add User Call Tips to SciTE for functions in UDFs not included with AutoIt and for your own scripts.Functions with parameters in OnEvent mode and for Hot Keys One function replaces GuiSetOnEvent, GuiCtrlSetOnEvent and HotKeySet.UDF IsConnected2 for notification of status of connected state of many urls or IPs, without slowing the script.
Link to comment
Share on other sites

Wouldn't it be easier to open the files in binary mode and then you wouldn't need to convert?

OK, I do not want to convert, I want to know how to join the parts that come on the other computer to compose the picture again.

The only way I found was using:

;---------------------------------------------------------------------------------

If IsBinary($vRecv) Then ; The data received is bitmap variable.

$bBmp &= Hex($vRecv)

ContinueLoop

EndIf

;---------------------------------------------------------------------------------

In this way: $bBmp += $vRecv, not work!

João Carlos.

http://forum.autoitbrasil.com/ (AutoIt v3 Brazil!!!)

Somewhere Out ThereJames Ingram

somewh10.png

dropbo10.pngDownload Dropbox - Simplify your life!
Your virtual HD wherever you go, anywhere!

Link to comment
Share on other sites

$tBuffer = DllStructCreate('byte[8388608]') ; 8 MB
$pBuffer = DllStructGetPtr($tBuffer)
$iOffset = 0

...

If IsBinary($vRecv) Then
    DllStructSetData(DllStructCreate('byte[' & BinaryLen($vRecv) & ']', $pBuffer + $iOffset), 1, $vRecv)
    $iOffset += BinaryLen($vRecv)
    ContinueLoop
EndIf

...

$bBmp = DllStructGetData(DllStructCreate('byte[' & $iOffset & ']', $pBuffer), 1)

Link to comment
Share on other sites

@Yashied,

Your code is more faster than this?:

If IsBinary($vRecv) Then ; The data received is bitmap variable.
     $bBmp &= Hex($vRecv)
      ContinueLoop
EndIf

João Carlos.

Edited by jscript

http://forum.autoitbrasil.com/ (AutoIt v3 Brazil!!!)

Somewhere Out ThereJames Ingram

somewh10.png

dropbo10.pngDownload Dropbox - Simplify your life!
Your virtual HD wherever you go, anywhere!

Link to comment
Share on other sites

OK, I do not want to convert, I want to know how to join the parts that come on the other computer to compose the picture again.

The only way I found was using:

;---------------------------------------------------------------------------------

If IsBinary($vRecv) Then ; The data received is bitmap variable.

$bBmp &= Hex($vRecv)

ContinueLoop

EndIf

;---------------------------------------------------------------------------------

In this way: $bBmp += $vRecv, not work!

João Carlos.

True, but since (I thought) you only want to create the file why not just write the $vRecv to the file using FileWrite which will append the data and not worry about adding things together or clever complicated concatenations. Then when everything is written you have recreated the original file.

So you read some binary data, send the binary data, receive binary data and write the binary data.

Serial port communications UDF Includes functions for binary transmission and reception.printing UDF Useful for graphs, forms, labels, reports etc.Add User Call Tips to SciTE for functions in UDFs not included with AutoIt and for your own scripts.Functions with parameters in OnEvent mode and for Hot Keys One function replaces GuiSetOnEvent, GuiCtrlSetOnEvent and HotKeySet.UDF IsConnected2 for notification of status of connected state of many urls or IPs, without slowing the script.
Link to comment
Share on other sites

True, but since (I thought) you only want to create the file why not just write the $vRecv to the file using FileWrite which will append the data and not worry about adding things together or clever complicated concatenations. Then when everything is written you have recreated the original file.

So you read some binary data, send the binary data, receive binary data and write the binary data.

That's the thing: I will not write to disk but in memory!

João Carlos.

http://forum.autoitbrasil.com/ (AutoIt v3 Brazil!!!)

Somewhere Out ThereJames Ingram

somewh10.png

dropbo10.pngDownload Dropbox - Simplify your life!
Your virtual HD wherever you go, anywhere!

Link to comment
Share on other sites

  • 1 month later...
  • 5 years later...

Resurrection...that's how I do it:

  • Create a first struct that fits all binary parts you want to merge.
  • Get a pointer to that struct
  • Repeat this for every binary:
    • Create a new struct that fits one binary in it, using the pointer (instead of allocating own memory)
    • Increase the pointer by the binary size
    • Write the binary to each struct
  • Read the first struct
Local $data1_size = 6
Local $data2_size = 6
Local $data_struct = DllStructCreate("char data[" & $data1_size + $data2_size & "]")
Local $data_lp = DllStructGetPtr($data_struct)
Local $data1_struct = DllStructCreate("char data[" & $data1_size & "]", $data_lp)
$data_lp += $data1_size
Local $data2_struct = DllStructCreate("char data[" & $data2_size & "]", $data_lp)
DllStructSetData($data1_struct, "data", "Hillo ")
DllStructSetData($data2_struct, "data", "World!")
DllStructSetData($data1_struct, "data", "e", 2)
ConsoleWrite(DllStructGetData($data_struct, "data"))

You can avoid the pointer increment if you have a predefined number of binary variables to merge, e.g. two:

Local $data1_size = 6
Local $data2_size = 6
Local $data12_struct = DllStructCreate("char data1[" & $data1_size & "];char data2[" & $data1_size & "]")
Local $data12_lp = DllStructGetPtr($data12_struct)
Local $data_struct = DllStructCreate("char data[" & $data1_size + $data2_size & "]", $data12_lp)
DllStructSetData($data12_struct, "data1", "Hillo ")
DllStructSetData($data12_struct, "data2", "World!")
DllStructSetData($data12_struct, "data1", "e", 2)
ConsoleWrite(DllStructGetData($data_struct, "data"))

As shown in the examples it works with strings, too.

Maze

Edited by MazeM
typo
Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...