3

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!

2 Answers 2

2

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.

Sign up to request clarification or add additional context in comments.

2 Comments

Thank you for your explanations. For the first part, I did not know that it was necessary to define the length of the array before being able to use it. I thought that this length was flexible, without a fixed declaration. Anyway, thank you very much, you explain well! :)
You're welcome! Let me just clarify something... you don't always need to set the length of the array. For example, if you'll be assigning another value to it (e.g., Dim arr As Integer() = someListOfInteger.ToArray() or = SomeFunctionThatReturnsAnArray()). You do, however, need to set the length if you're going to fill it (Array.Copy() is one way to fill an array). Good luck :)
1

Use integer division (\) to calculate the size of the first part

Dim Separator As Integer = Bytes_Executable.Length \ 2

The destination arrays must be created with the right length before calling Array.Copy.

Dim Sortie(Separator - 1) As Byte
Dim Sortie2(Bytes_Executable.Length - Separator - 1)  As Byte

Note that since in VB we specify the upper bound of the array index and not the length of the array, we must subtract 1.

You must specify the length to be copied as last parameter (i.e., the remaining length)

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

1 Comment

Thank you so much. Indeed, I made a lot of mistakes on this project, in particular the declaration of the two variables Sortie . I understand a lot better. And error on my part for the last parameter; indeed, I misunderstood the documentation at this about

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.