vb.net combine two byte arrays

F

frekster

Hi.

I used to be able to do this easily in vb 6 via looping and preserving
the source array data/size etc.


How can I do this in vb.net? I've been trying for a while now and this
should be an easy task but it just isn't clicking.

For some reason, the preserve doesn't seem to be working.

I have a function where I pass it the source array of bytes and
destination array of bytes and it is to combine them.

CompletedFile is the array to hold the two combined byte arrays and is
used as the destination in the function call:

CompletedFile = Me.CombineByteArray(SourceFile, CompletedFile)

Private Function CombineByteArray(ByVal Source() As Byte, ByVal
Destination() As Byte) As Byte()
Dim intCurrPosition As Integer
Dim i As Integer

intCurrPosition = Destination.Length - 1

ReDim Preserve Destination(Destination.Length + Source.Length)
Try
For i = 0 To Source.Length - 1
Destination(intCurrPosition) = Source(i)
intCurrPosition = intCurrPosition + 1
Next
Return Destination
Catch ex As Exception

End Try


End Function


The above code is keeping the first byte array and not combining the
second byte array even though there are no errors.

Any ideas?

Justin
 
P

Patrice

Have you try to confirm Preserve doesn't work ?
What if you avoid catching errors and doing nothing ? (could it show some
hidden errors ?).
 
P

Patrice

Ok I just dumped a test array to see what is the behavior you have. It
essentially evolves around bounds :
- intCurPosition=Destination.Length so that you start filling after the
last element of the first array
- Redim uisng Distination.Length+Source.Length-1 so that the array has the
appropriate number of elements
- you could also reverse the process. IMO it would be more natural to have
Source elements and then Destination elements rather than the other way
round (i.e. in the same order than the arguments).
 
F

frekster

Hi.

Thanks for the post. I am trying a new approach using the .CopyTo method
of an array. I have two wav files I am trying to combine into a single
wav file called test.wav. The below code executes with no errors,
however, it only saves the first file into test.wav and not the second
one even though the code appears to be resizing correctly and saving the
byte array from the second file correctly. Can anyone help here? Why is
only the first file being saved to the test.wav file and not the second
one?

BTW the path to the files is stored in web.config and is correct.

Code follows:

' get binary data in file stream

fs = New
FileStream(Server.MapPath(CType(System.Configuration.ConfigurationSettin
gs.AppSettings.GetValues("PathToSounds")(0), String) & "capital.wav"),
FileMode.Open)

' how large is file ?

intFileSize = fs.Length - 1

' resize array

ReDim SourceFile(intFileSize)

' copy data to byte array now

fs.Read(SourceFile, 0, SourceFile.Length)

' now save this to Completed file array

fs.Close()


ReDim Preserve CompletedFile(SourceFile.Length)


SourceFile.CopyTo(CompletedFile, 0)



' now copy the byte data for the letter
fs = New
FileStream(Server.MapPath(CType(System.Configuration.ConfigurationSettin
gs.AppSettings.GetValues("PathToSounds")(0), String) & strLetter &
".wav"), FileMode.Open)

intFileSize = fs.Length - 1

' resize array
ReDim SourceFile(intFileSize)

' copy data to byte array now

fs.Read(SourceFile, 0, SourceFile.Length)

fs.Close()

Dim intSavedSpot = CompletedFile.Length - 1



ReDim Preserve CompletedFile(intSavedSpot +
SourceFile.Length)

SourceFile.CopyTo(CompletedFile, intSavedSpot)


' now save the file
fs = New
FileStream(Server.MapPath(CType(System.Configuration.ConfigurationSettin
gs.AppSettings.GetValues("PathToSounds")(0), String) & "test.wav"),
FileMode.CreateNew)



fs.Position = 0
fs.Write(CompletedFile, 0, CompletedFile.Length)

fs.Close()



Regards,
Justin
 
F

frekster

Hi Patrice

If you have a minute, can you please use two wav files from
c:\windows\media and try to combine them into a new file called
test.wav using my code using the .CopyTo method. You can copy/paste my
code in a windows application or asp.net application and run it/and see
what is going on. I cannot figure it out.

Thanks much for the help!

Justin
 
F

frekster

oh BTW if you double-click on test.wav, it will open up in media player
or winamp and you will see that only the first file is played in the
test.wav file even though both programmatically seem to be put in the
test.wav file.

Justin
 
F

frekster

ok, figured this out. I was using wav files and in the header of a wav
file is the length of the wav file. I cannot concatinate on wav files
one after another based on this structure. Well, I coudl but it isn't
worth the effort. Using mp3 files you can combine as many as you wish
into a single byte array adn dump it to a single mp3 file.

Justin
 
K

Kevin Spencer

Use this, from my personal utilities library (come back if you need help
translating it):

/// <summary>
/// Cancatenates 2 byte arrays together into one
/// </summary>
/// <param name="a">1st byte array</param>
/// <param name="b">2nd byte array</param>
/// <returns>Concatenated byte array</returns>
public static byte[] ConcatBytes(byte[] a, byte[] b)
{
byte[] bytes = new byte[a.Length + b.Length];
Array.Copy(a, bytes, a.Length);
Array.Copy(b, 0, bytes, a.Length, b.Length);
return bytes;
}

You will notice that it's static (Shared). This means it is re-usable in any
application you write, and does not require an instance of the class it is
contained in to use it.

--
HTH,

Kevin Spencer
Microsoft MVP
..Net Developer
I'd rather be a hammer than a nail.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,744
Messages
2,569,484
Members
44,903
Latest member
orderPeak8CBDGummies

Latest Threads

Top