Click here to Skip to main content
15,903,385 members
Please Sign up or sign in to vote.
5.00/5 (1 vote)
See more:
How do I convert a Byte Array into an array of short values in Vb.net? Note that I want to read each set of 2 byte values from the given byte array as a single short value. Please suggest.
Note that I need this for writing wav file from a textfile that contains wav samples as byte values (such as 255, 255, 0, 2, etc.).
Posted
Updated 19-May-12 20:12pm
v5
Comments
Jim Jos 18-May-12 2:40am    
Could you tell me you would like to create a new short array or read each valur from byte array as short?
Member 8244358 18-May-12 13:13pm    
i want to convert an array of byte values into an array of short. i know that 2 bytes = 1short. But i am not able to do it properly even after searching a lot on Google.
OriginalGriff 18-May-12 3:01am    
This is not a good question - we cannot work out from that little what you are trying to do.
Remember that we can't see your screen, access your HDD, or read your mind.
Use the "Improve question" widget to edit your question and provide better information.
Member 8244358 18-May-12 13:16pm    
hope u will understand my question better.

Use this:

VB
Dim bytes As Byte()
Dim shorts = Array.ConvertAll(bytes, Function(b) CShort(b))
 
Share this answer
 
Comments
Maciej Los 18-May-12 10:35am    
Good answer, my 5!
Shahin Khorshidnia 18-May-12 12:01pm    
Thank you very much losmac
Member 8244358 20-May-12 2:07am    
This code works but still I am not getting the exact values
It's not difficult: it just takes a little thinking!
The C# version:
C#
byte[] bytes = File.ReadAllBytes(@"D:\Temp\MyPic.jpg");
short[] shorts = new short[(bytes.Length & 1) == 0 ? bytes.Length / 2 : (bytes.Length / 2) + 1];
int j = 0;
for (int i = 0; i < bytes.Length; i += 2)
    {
    uint u1 = (uint)bytes[i] & 0xFF;
    uint u2 =  (uint)bytes[i + 1] & 0xFF;
    shorts[j++] = (short) (u1 | (u2 << 8));
    }
if ((bytes.Length & 1) != 0)
    {
    shorts[j] = (short)bytes[bytes.Length - 1];
    }

Or VB:
VB
Dim bytes As Byte() = File.ReadAllBytes("D:\Temp\MyPic.jpg")
Dim shorts As Short() = New Short(If((bytes.Length And 1) = 0, bytes.Length \ 2, (bytes.Length \ 2) + 1) - 1) {}
Dim j As Integer = 0
For i As Integer = 0 To bytes.Length - 1 Step 2
    Dim u1 As UInteger = CUInt(bytes(i)) And &Hff
    Dim u2 As UInteger = CUInt(bytes(i + 1)) And &Hff
    shorts(j) = CShort(u2 Or (u1 << 8))
    j = j + 1
Next
If (bytes.Length And 1) <> 0 Then
    shorts(j) = CShort(bytes(bytes.Length - 1))
End If
I've tested in in C# and auto converted it to VB.

You may want to reverse u1 and u2 in the line:
VB
shorts(j) = CShort(u1 Or (u2 << 8))
if you want big endian instead of little endian (i.e. if your data has the low byte second)
 
Share this answer
 
Comments
Member 8244358 18-May-12 23:39pm    
thanks for your code..but i am still not getting the exact values. yes, i want the values in Little Endian format. But actually i am working with wav files. I am reading a wav file into a byte array and want to create a copy of original wav file using the same byte array. Can you suggest me how do i do it? I am using the following code:

Public Function streamfile(ByVal filename As String) As Byte()

Dim fs As New FileStream(filename, FileMode.Open, FileAccess.Read)
Dim data(fs.Length) As Byte
fs.Read(data, 0, System.Convert.ToInt32(fs.Length))
fs.Close()
Return data

End Function

Dim bytes() As Byte = streamfile("C:\filename.doc")
fw.Write(bytes, 0, bytes.Length)
OriginalGriff 19-May-12 3:49am    
Do you have some strange objection to doing things the easy way? :laugh:

Dim bytes As Byte() = File.ReadAllBytes("D:\Temp\MyWavFile.wav")
File.WriteAllBytes("D:\Temp\Copy of MyWavFile.wav", bytes)

Or:

File.Copy("D:\Temp\MyWavFile.wav", "D:\Temp\Copy of MyWavFile.wav")
Member 8244358 20-May-12 2:11am    
Lol :D
Actually I am using byte values to create the copy of a wav file. As wav file data is in Little Endian format, my problem is that I am not getting how to create the wav file from byte values stored in a textfile (The textfile contains original wav samples as byte values and i am creating a copy of original wav file).
OriginalGriff 20-May-12 2:35am    
So what do the values in the text file look like? Are they human readable as you would expect for a text file?
Sorry - I don't know what your data looks like, so I can't tell if it is just a case of "write the bytes to a file with a .WAV extension" or if not what processing it needs - I assume it needs processing of some sort because if it was that trivial you would have done it by now! :laugh:
Member 8244358 20-May-12 2:43am    
Right now, my textfile contains the data like this:
255
255
2
0
254
255
2
0
254
255
1
0
0
0
0
0
0
0
0
0
255
255
2
0
253
255
4
0
251
255
5
0
253
255
No conversion needed now. I used memorystream to accomplish my task. Thanks to all for your comments, especially OriginalGriff.
 
Share this answer
 
C#
I write in C #, but I think in the VB.NET would be like in C#:

1) First way  is using the Array.ConvertAll:
byte[] bytes;
var shorts = Array.ConvertAll(bytes, b => (short)b);

2)Second way is using the Enumerable.Select:
byte[] bytes;
var shorts = bytes.Select(b => (short)b).ToArray();
 
Share this answer
 
Comments
Shahin Khorshidnia 18-May-12 7:06am    
Yes, it in here: http://stackoverflow.com/questions/1104599/convert-byte-array-to-short-array-in-c-sharp
You can write the below method to read byte array from file.

C#
public static int[] Converter(string fileName)
{
try
{
int bYte = 2;
byte[] buf = File.ReadAllBytes(fileName);
int bufPos = 0;
int[] data = new int[buf.Length/2];
byte[] bt = new byte[bYte];
for (int i = 0; i < buf.Length/2; i++)
{
Array.Copy(buf, bufPos, bt, 0, bYte);
bufPos += bYte;
Array.Reverse(bt);
data[i] = BitConverter.ToInt16(bt, 0);
}
return data;
}
catch
{
return null;
}
}
 
Share this answer
 
Comments
Shahin Khorshidnia 18-May-12 7:08am    
I didn't vote but I think it's not the answer!
Member 8244358 18-May-12 23:41pm    
i am able to read the fle into byte array, but not able to convert byte array into an array of Int16 values
C#
public class ByteTest {

    public static void main(String[] args) {
        byte[] result = int2ByteArray(12556);
        System.err.println("result = " + result[0] + " " + result[1] + " " + result[2] + " " + result[3]);
        int intResult = byteArray2Int(result);
        System.err.println(intResult);
    }

    /**
     * 字节数组的低位是整型的高字节位
     * @param target
     * @return
     */
    public static byte[] int2ByteArray(int target) {
        byte[] array = new byte[4];
        for (int i = 0; i < 4; i++) {
            int offSet = array.length -i -1;
            array[i] = (byte) (target >> 8 * offSet & 0xFF);
        }
        return array;
    }

    /**
     * 字节数组的低位是整型的高字节位
     * @param array
     * @return
     */
    public static int byteArray2Int(byte[] array) {
        int result = 0;
        byte loop;

        for (int i = 0; i < 4; i++) {
            loop = array[i];
            int offSet = array.length -i -1;
            result += (loop & 0xFF) << (8 * offSet);

        }
        return result;
    }
}
 
Share this answer
 
Comments
Shahin Khorshidnia 18-May-12 7:12am    
Thanks to: http://blog.csdn.net/gaomatrix/article/details/7034798

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900