Home > Archive > Visual Basic > April 2006 > Conveting byte array to int array ???
You are viewing an archived Text-only version of the thread.
To view this thread in it's original format and/or if you want to reply to
this thread please [click here]
| Author |
Conveting byte array to int array ???
|
|
| Sanjay 2006-04-20, 3:56 am |
| Hi
Please tell me
I have ..array of byte byteAudio(4096)
AND I HAVE TO Convert it into array of integer..
i m trying like this ..
for i=0 to 4096
intAudio[i]=CINT(byteAudio[i])
next
...
...
...
Another way
CopyMemort intAudio(0),byteAudio(0),4096
Please tell which is the correect and better way
thanks
| |
| J French 2006-04-20, 3:56 am |
| On 19 Apr 2006 22:19:31 -0700, "Sanjay" <Sanjayrvyas@gmail.com> wrote:
>Hi
> Please tell me
> I have ..array of byte byteAudio(4096)
> AND I HAVE TO Convert it into array of integer..
> i m trying like this ..
>
> for i=0 to 4096
> intAudio[i]=CINT(byteAudio[i])
> next
That is 4097 bytes
You are making 4097 Integers, each integer has the same value as the
corresponding byte
>..
>..
>Another way
>CopyMemort intAudio(0),byteAudio(0),4096
Here you are copying 4096 bytes into the memory space 2048 Integers
>Please tell which is the correect and better way
It depends what you want to do
- they both do totally different things
| |
| Mike Williams 2006-04-20, 3:56 am |
| "Sanjay" <Sanjayrvyas@gmail.com> wrote in message
news:1145510371.697432.36490@i39g2000cwa.googlegroups.com...
> I have ..array of byte byteAudio(4096)
> AND I HAVE TO Convert it into array of integer..
> for i=0 to 4096
> intAudio[i]=CINT(byteAudio[i])
> next
> Another way?
> CopyMemort intAudio(0),byteAudio(0),4096
Personally I would carry on doing it just as you are. Nothing wrong with
that method, and in a native code compiled exe it will be very fast (on my
machine it converts bytes to integers at the rate of over a hundred million
bytes to integers per second). However, if you really do want to do it
another way then you can. CopyMemory on its own will not do it for you
because it simply copies one area of memory to another. You can, however,
use the VB strConv function to convert your byte array to a VB string. Vb
strings are normally held using two bytes per character, and if you specify
vbUnicode as the parameter in the StrConv function then VB will
automatically "stretch out" the byte data to a "two bytes per character"
string for you. It should then be possible to copy the data from that string
to the memory area of a similarly sized Integer array and the job will be
done for you. It's a bit early in the morning here (and I haven't had my
breakfast yet!) and I can't quite get the code right (probably using the
wrong function and getting hold of a pointer to the string descriptor
instead of the actual string data) but I'm sure it can be done. In fact, an
alternative to CopyMemory would be to use the VB Lset statement, which can
copy the data area of one UDT into the data area of another. It isn't as
flexible as CopyMemopry of course, but it does show that the task can
actually be done. Maybe when I've had my breakfast I might come up with a
better answer! Anyway, assuming that you're really dealing with 4096 bytes
(and not 4097 as your code implies) here is one way of doing it. It's just a
bit of raw test bed code at the moment, but it does work and it does show
you that the job is definitely "doable".
Mike
Option Explicit
Private Type myString
data As String * 4096
End Type
Private Type myIntegers
data(1 To 4096) As Integer
End Type
Private b(1 To 4096) As Byte
Private Sub Command1_Click()
'
b(1) = 33: b(2) = 66: b(4096) = 99 ' just some test values
'
Dim stringData As myString, integerData As myIntegers
stringData.data = StrConv(b, vbUnicode)
LSet integerData = stringData
' check the results
Print b(1), integerData.data(1)
Print b(2), integerData.data(2)
Print b(4096), integerData.data(4096)
End Sub
| |
| Mike Williams 2006-04-20, 3:56 am |
| "Sanjay" <Sanjayrvyas@gmail.com> wrote in message
news:1145510371.697432.36490@i39g2000cwa.googlegroups.com...
> I have ..array of byte byteAudio(4096)
> AND I HAVE TO Convert it into array of integer..
By the way, in my previous answer I "took you at your word" and showed you
one way on converting an array of Bytes into an array of Integers. However,
I assumed that was *exactly what you wanted to do*, and I took it that you
fully understand that these Integers (each of which consists of two data
bytes of course) would each end up with a lo byte equal to the appropriate
byte data and a hi byte of zero, so that if you printed the contents on an
Integer you would get exactly the same value as its equivalent Byte in the
original byte array (that is what your own existing For . . . Next loop code
was achieving, and so that it what my own posted code also achieved). Such
conversions are probably often required for audio data conversions, and it
is quite probably what you actually want. However (not another however! I
should stop saying that all the time!), if you really just want for some
reason to shift the data from an array of 4096 bytes exactly as it stands
into an array of 2048 Integers then you can simple use the CopyMemory API
(or the alternative VB Lset method) directly, without the prior conversion
of the StrConv function that I have used in my previous code.
Anyway, time for my breakfast now. Luckily I don't have to make it myself,
but from the aromas coming from the kitchen it smells like Maureen is making
bacon, eggs, tomatoes and fried mushrooms. I wonder if there's gonna be any
fried bread as well. Mmmmm. Yummy. Almost ten o' clock in the morning and
we're just casually having our breakfast. Isn't it great being retired ;-)
Mike
| |
| J French 2006-04-20, 3:56 am |
| On Thu, 20 Apr 2006 09:44:43 +0100, "Mike Williams"
<Mike@WhiskyAndCoke.com> wrote:
>"Sanjay" <Sanjayrvyas@gmail.com> wrote in message
>news:1145510371.697432.36490@i39g2000cwa.googlegroups.com...
>
>
>By the way, in my previous answer I "took you at your word" and showed you
>one way on converting an array of Bytes into an array of Integers. However,
>I assumed that was *exactly what you wanted to do*, and I took it that you
>fully understand that these Integers (each of which consists of two data
>bytes of course) would each end up with a lo byte equal to the appropriate
>byte data and a hi byte of zero, so that if you printed the contents on an
>Integer you would get exactly the same value as its equivalent Byte in the
>original byte array (that is what your own existing For . . . Next loop code
>was achieving, and so that it what my own posted code also achieved).
I wondered whether he was getting a stream of Integers coming in as
Bytes from WinSock ...
| |
| Mike D Sutton 2006-04-20, 7:56 am |
| > I wondered whether he was getting a stream of Integers coming in as
> Bytes from WinSock ...
In which case the easiest solution is to simply give WinSock the integer array buffer directly and avoid the need to
convert the data. If the WinSock functions are actually returning the array themselves then you can 'point' an integer
array at the byte array by messing with a SAFEARRAY if speed is critical, however for a 4kb buffer it hardly seems worth
it really.
Mike
- Microsoft Visual Basic MVP -
E-Mail: EDais@mvps.org
WWW: Http://EDais.mvps.org/
| |
| J French 2006-04-20, 7:56 am |
| On Thu, 20 Apr 2006 12:22:00 +0100, "Mike D Sutton" <EDais@mvps.org>
wrote:
>
>In which case the easiest solution is to simply give WinSock the integer array buffer directly and avoid the need to
>convert the data. If the WinSock functions are actually returning the array themselves then you can 'point' an integer
>array at the byte array by messing with a SAFEARRAY if speed is critical, however for a 4kb buffer it hardly seems worth
>it really.
Agreed
- I'm just trying to shake a little more info out of the tree
Actually ... in this case LSet might suffice ( the 'Winsock' might be
just a Byte stream )
| |
| Larry Serflaten 2006-04-20, 7:56 am |
|
"Mike Williams" <Mike@WhiskyAndCoke.com> wrote
> However, if you really do want to do it
> another way then you can. CopyMemory on its own will not do it for you
> because it simply copies one area of memory to another. You can, however,
> use the VB strConv function to convert your byte array to a VB string.
The string is not needed, StrConv will accept the Byte array as a destination:
Private Declare Sub CopyMemory Lib "kernel32" _
Alias "RtlMoveMemory" _
(xDest As Any, _
xSource As Any, _
ByVal nBytes As Long)
Private Sub Form_Load()
Dim b() As Byte
Dim i() As Integer
Dim idx As Long
ReDim b(1 To 5)
For idx = 1 To 5
b(idx) = idx
Next
b = StrConv(b, vbUnicode)
ReDim i(0 To UBound(b) \ 2)
CopyMemory i(0), b(0), UBound(b) + 1
For idx = 0 To UBound(i)
Debug.Print i(idx)
Next
End Sub
| |
| Mike Williams 2006-04-20, 6:56 pm |
| "Larry Serflaten" <serflaten@usinternet.com> wrote in message
news:OibFAnHZGHA.4620@TK2MSFTNGP04.phx.gbl...
> The string is not needed, StrConv will accept the Byte
> array as a destination:
I did tell you that I hadn't had my breakfast ;-)
Mike
| |
| Sanjay 2006-04-26, 3:56 am |
| Hi Mike
I know winsock ca ngive me integer value ....but .....first of all i
have read my Audio Packet in Byte........then eextract Audio buffer
..from Packet..then need to convert to interger for Audio Mixing
Process... that why ..
Any way Thanks
| |
| Sanjay 2006-04-26, 3:56 am |
| Hi Mike
I hope i will get my answer .... Thanks for your positive response.
Please enjoy ur breakfast :)
Sanjay vyas
| |
| Sanjay 2006-04-26, 3:56 am |
| Hi
I hope this code will be benefitted to me .... Thanks for your
positive response.
Bye
Sanjay vyas
| |
| Mike D Sutton 2006-04-26, 7:56 am |
| > I know winsock ca ngive me integer value ....but .....first of all i
> have read my Audio Packet in Byte........then eextract Audio buffer
> .from Packet..then need to convert to interger for Audio Mixing
> Process... that why ..
If you can get a pointer to the first byte of audio data then you can fill
out a SAFEARRAY structure and hijack a VB integer array into pointing at
that data without moving the data at all. If you have a large chunk of data
then this is by far the most efficient method, and a commonplace technique
in any other language with pointer support.
If you want to find out more then have a look in the archives for SAFEARRAY
and VarPtrArr, you'll be sure to come up with plenty of examples.
Hope this helps,
Mike
- Microsoft Visual Basic MVP -
E-Mail: EDais@mvps.org
WWW: Http://EDais.mvps.org/
|
|
|
|
|