vb.net combine two byte arrays

Discussion in 'ASP .Net' started by frekster, Sep 26, 2005.

  1. frekster

    frekster Guest

    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
     
    frekster, Sep 26, 2005
    #1
    1. Advertising

  2. frekster

    Patrice Guest

    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" <> a écrit dans le message de
    news:...
    > 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
    >
     
    Patrice, Sep 26, 2005
    #2
    1. Advertising

  3. frekster

    Patrice Guest

    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" <> a écrit dans le message de
    news:%...
    > 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" <> a écrit dans le message de
    > news:...
    > > 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
    > >

    >
    >
     
    Patrice, Sep 26, 2005
    #3
  4. frekster

    Patrice Guest

    Finally you could use the CopyTo method...

    Hope it helped...

    --
    Patrice

    "Patrice" <> a écrit dans le message de
    news:%...
    > 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" <> a écrit dans le message de
    > news:%...
    > > 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" <> a écrit dans le message de
    > > news:...
    > > > 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
    > > >

    > >
    > >

    >
    >
     
    Patrice, Sep 26, 2005
    #4
  5. frekster

    frekster Guest

    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

    *** Sent via Developersdex http://www.developersdex.com ***
     
    frekster, Sep 26, 2005
    #5
  6. frekster

    frekster Guest

    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
     
    frekster, Sep 26, 2005
    #6
  7. frekster

    frekster Guest

    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
     
    frekster, Sep 26, 2005
    #7
  8. frekster

    frekster Guest

    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
     
    frekster, Sep 27, 2005
    #8
  9. 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" <> wrote in message
    news:%...
    > 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
    >
    > *** Sent via Developersdex http://www.developersdex.com ***
     
    Kevin Spencer, Sep 27, 2005
    #9
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Imran Aziz

    Combine results of two Dataset in memory

    Imran Aziz, Feb 28, 2006, in forum: ASP .Net
    Replies:
    3
    Views:
    3,256
    Imran Aziz
    Mar 1, 2006
  2. wapsiii
    Replies:
    0
    Views:
    526
    wapsiii
    Apr 10, 2006
  3. Philipp
    Replies:
    21
    Views:
    1,183
    Philipp
    Jan 20, 2009
  4. gelonida
    Replies:
    1
    Views:
    789
    Gabriel Genellina
    May 6, 2010
  5. Kev Jackson
    Replies:
    2
    Views:
    141
Loading...

Share This Page