Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > ASP .Net > vb.net combine two byte arrays

Reply
Thread Tools

vb.net combine two byte arrays

 
 
frekster
Guest
Posts: n/a
 
      09-26-2005
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

 
Reply With Quote
 
 
 
 
Patrice
Guest
Posts: n/a
 
      09-26-2005
Have you try to confirm Preserve doesn't work ?
What if you avoid catching errors and doing nothing ? (could it show some
hidden errors ?).
--
Patrice

"frekster" <(E-Mail Removed)> a écrit dans le message de
news:(E-Mail Removed) oups.com...
> 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
>



 
Reply With Quote
 
 
 
 
Patrice
Guest
Posts: n/a
 
      09-26-2005
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).

--
Patrice

"Patrice" <(E-Mail Removed)> a écrit dans le message de
news:%(E-Mail Removed)...
> Have you try to confirm Preserve doesn't work ?
> What if you avoid catching errors and doing nothing ? (could it show some
> hidden errors ?).
> --
> Patrice
>
> "frekster" <(E-Mail Removed)> a écrit dans le message de
> news:(E-Mail Removed) oups.com...
> > 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
> >

>
>



 
Reply With Quote
 
Patrice
Guest
Posts: n/a
 
      09-26-2005
Finally you could use the CopyTo method...

Hope it helped...

--
Patrice

"Patrice" <(E-Mail Removed)> a écrit dans le message de
news:%(E-Mail Removed)...
> 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).
>
> --
> Patrice
>
> "Patrice" <(E-Mail Removed)> a écrit dans le message de
> news:%(E-Mail Removed)...
> > Have you try to confirm Preserve doesn't work ?
> > What if you avoid catching errors and doing nothing ? (could it show

some
> > hidden errors ?).
> > --
> > Patrice
> >
> > "frekster" <(E-Mail Removed)> a écrit dans le message de
> > news:(E-Mail Removed) oups.com...
> > > 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
> > >

> >
> >

>
>



 
Reply With Quote
 
frekster
Guest
Posts: n/a
 
      09-26-2005
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.Configurati on.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.Configurati on.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.Configurati on.ConfigurationSettin
gs.AppSettings.GetValues("PathToSounds")(0), String) & "test.wav"),
FileMode.CreateNew)



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

fs.Close()



Regards,
Justin

*** Sent via Developersdex http://www.developersdex.com ***
 
Reply With Quote
 
frekster
Guest
Posts: n/a
 
      09-26-2005
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

 
Reply With Quote
 
frekster
Guest
Posts: n/a
 
      09-26-2005
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

 
Reply With Quote
 
frekster
Guest
Posts: n/a
 
      09-26-2005
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

 
Reply With Quote
 
Kevin Spencer
Guest
Posts: n/a
 
      09-27-2005
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.

"frekster" <(E-Mail Removed)> wrote in message
news:%(E-Mail Removed)...
> 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.Configurati on.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.Configurati on.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.Configurati on.ConfigurationSettin
> gs.AppSettings.GetValues("PathToSounds")(0), String) & "test.wav"),
> FileMode.CreateNew)
>
>
>
> fs.Position = 0
> fs.Write(CompletedFile, 0, CompletedFile.Length)
>
> fs.Close()
>
>
>
> Regards,
> Justin
>
> *** Sent via Developersdex http://www.developersdex.com ***



 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
FAQ 4.43 How do I compute the difference of two arrays? How do I compute the intersection of two arrays? PerlFAQ Server Perl Misc 0 02-02-2011 05:00 AM
find out whther byte two .pyc files contain the same byte code. gelonida Python 1 05-05-2010 11:04 PM
Multidimensional arrays and arrays of arrays Philipp Java 21 01-20-2009 08:33 AM
Two arrays: return only values that exist in both arrays? Joshua Muheim Ruby 8 11-23-2007 07:04 PM
two arrays problem (although different from the other two arrays) Kev Jackson Ruby 2 03-29-2006 03:58 PM



Advertisments