473,386 Members | 1,997 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,386 software developers and data experts.

vb.net combine two byte arrays

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

Nov 19 '05 #1
8 4564
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" <ju*****@cboss.com> a écrit dans le message de
news:11**********************@g49g2000cwa.googlegr 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

Nov 19 '05 #2
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" <no****@nowhere.com> a écrit dans le message de
news:%2****************@TK2MSFTNGP12.phx.gbl...
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" <ju*****@cboss.com> a écrit dans le message de
news:11**********************@g49g2000cwa.googlegr 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


Nov 19 '05 #3
Finally you could use the CopyTo method...

Hope it helped...

--
Patrice

"Patrice" <no****@nowhere.com> a écrit dans le message de
news:%2****************@TK2MSFTNGP11.phx.gbl...
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" <no****@nowhere.com> a écrit dans le message de
news:%2****************@TK2MSFTNGP12.phx.gbl...
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" <ju*****@cboss.com> a écrit dans le message de
news:11**********************@g49g2000cwa.googlegr 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



Nov 19 '05 #4
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 ***
Nov 19 '05 #5
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

Nov 19 '05 #6
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

Nov 19 '05 #7
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

Nov 19 '05 #8
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" <an*******@devdex.com> wrote in message
news:%2****************@TK2MSFTNGP14.phx.gbl...
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 ***

Nov 19 '05 #9

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
by: Eric Hendriks | last post by:
// In an unmanaged DLL the following function must be called: // int VFGeneralize(const BYTE * const * features); // "features" parameter is supposed to be an array of byte arrays. // function is...
8
by: Ben Terry | last post by:
What's the most efficient way to transfer data from a byte to a struct? The struct is rather complex--contains other structs as well as byte members. I've tried to use Marshal.Copy and an IntPtr...
7
by: Joseph Lee | last post by:
Hi All, I am having problem when i am using hashtable to keep an array of bytes value as keys. Take a look at the code snippet below --------------------------------------------------- ...
2
by: TJO | last post by:
What is the best way to combine two byte? I am not finding one yet. Thanks
6
by: Dennis | last post by:
I was trying to determine the fastest way to build a byte array from components where the size of the individual components varied depending on the user's input. I tried three classes I built: (1)...
3
by: Vinny Vinn | last post by:
What is the simplest way to combine one byte with another? for example if i have byte header = Encoding.ASCII.GetBytes("this is the header"); and byte body = Encoding.ASCII.GetBytes("this is the...
3
by: Schroeder, AJ | last post by:
Hello group, I am a relative PHP newbie and I am trying to combine two arrays together, but I also need to keep the keys of one array intact. What I am doing is two SNMP walks against a Cisco...
2
by: chris | last post by:
I have a few byte arrays that I would like to combine into one array (order needs to be kept). What would be the most efficient way to do this? Thanks for your time, Chris
17
by: =?Utf-8?B?U2hhcm9u?= | last post by:
Hi Gurus, I need to transfer a jagged array of byte by reference to unmanaged function, The unmanaged code should changed the values of the array, and when the unmanaged function returns I need...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.