Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to Split Byte Values into Multiple Smaller Byte Values?

I'm learning to program in VB.NET, and I'm trying to manipulate an executable

My project asks me to cut a byte variable (which I got thanks to File.ReadAllytes) in two parts, to then be able to "stick" them in another part of the code.

I thought about converting my byte array to string, then split (using .Split), and finally converting it to a byte array, but my executable no longer works: converting to a string kills some character in my executable, making it obsolete.

I found this post: Split a byte array at a delimiter

..but the problem is that he works in C# and I feel at my level of real difficulty to convert its code into vb.net.

In summary, here are the steps of my program:

  • Read all bytes with File.ReadAllytes
  • Split this byte array on a regular basis. The seperator will not be a chain, but the half of the array.
  • Group the channels and execute

I have try this for split my executable in two byte variable, but i block:

Bytes_Executable = IO.File.ReadAllBytes(File1)
Dim Separator As Integer = Bytes_Executable.Length / 2
MsgBox(Separator)
Dim Sortie = {}
Dim Sortie2 = {}
Array.Copy(Bytes_Executable, 0, Sortie, 0, Separator)
Array.Copy(Bytes_Executable, Separator, Sortie2, 0, Bytes_Executable.Length)

Indeed, I've got this error: The destination table is not long enough. Check destIndex and the length, as well as the lower limits of the array.

This error points this line:

Array.Copy(Bytes_Executable, Separator, Sortie2, 0, Bytes_Executable.Length)

Thank you in advance!

like image 431
Corentin Mar Avatar asked Aug 27 '26 23:08

Corentin Mar


1 Answers

Your code has a few problems:

  1. The two arrays you have (Sortie and Sortie2) are not initialized (their length is zero). So, when you try to copy to them, the Copy method fails because "the array is not long enough". To set the length of the array, we use Dim someVariable(length - 1) As SomeType. For example, Dim arr(9) As Byte is a Byte array with a length of 10.

  2. Bytes_Executable.Length / 2:

    This doesn't handle the case where the number of bytes is odd.

  3. Array.Copy(Bytes_Executable, Separator, Sortie2, 0, Bytes_Executable.Length):

    Here you're using the full length of Bytes_Executable to fill the second array. So, even if you set the length of the array correctly (point #1), this will still fail because the length of the second array will be only half the length of the original array.

You may use something like this:

Dim filePath = "The\Path\to\your\file.exe"
Dim exeBytes = IO.File.ReadAllBytes(filePath)
Dim len1 As Integer = CInt(Math.Ceiling(exeBytes.Length / 2))
Dim len2 As Integer = exeBytes.Length - len1

Dim firstHalf(len1 - 1) As Byte
Dim secondHalf(len2 - 1) As Byte
Array.Copy(exeBytes, 0, firstHalf, 0, len1)
Array.Copy(exeBytes, len1, secondHalf, 0, len2)

As you can see, I used Math.Ceiling() to get around the second problem. Math.Ceiling will return 3 when you pass 2.5, for example.

like image 120
41686d6564 stands w. Palestine Avatar answered Aug 30 '26 14:08

41686d6564 stands w. Palestine