How do I convert 2 bytes to a Short.

  • Thread starter Thread starter Ken Dopierala Jr.
  • Start date Start date
K

Ken Dopierala Jr.

Hi,
I'm reading the header file of a PCX image and I need to convert 2 bytes
to a short. How would I go about doing this? I know that they are bytes 8
& 9 in my byte array. I'm not sure how to take those two and convert them
into a short though. In C I would just use a union and assign them
accordingly. Thanks! Ken.
 
Ken,
You can use System.BitConverter.ToInt16 to convert two bytes to a short.

The System.BitConverter supports converting a byte array to and from most of
the normal built-in types.

Something like:

Dim s As Short
Dim bytes() As Byte

s = BitConverter.ToInt16(bytes, 8)

Remember that the starting index is based 0, so the above may actually need
7.

Hope this helps
Jay
 
As a follow up, I can do this with API calls but I was wondering if there
was a .Net way to turn 2 bytes into a short, 4 bytes into an int and etcs.
Visa versa I can do it using ASCii encoding. Thanks. Ken.
 
Hi Jay,
It works sweet! Thanks! Ken.

Jay B. Harlow said:
Ken,
You can use System.BitConverter.ToInt16 to convert two bytes to a short.

The System.BitConverter supports converting a byte array to and from most of
the normal built-in types.

Something like:

Dim s As Short
Dim bytes() As Byte

s = BitConverter.ToInt16(bytes, 8)

Remember that the starting index is based 0, so the above may actually need
7.

Hope this helps
Jay


bytes
 
Hi,
I'm reading the header file of a PCX image and I need to convert 2
bytes
to a short. How would I go about doing this? I know that they are
bytes 8 & 9 in my byte array. I'm not sure how to take those two and
convert them into a short though. In C I would just use a union and
assign them accordingly. Thanks! Ken.

Why don't you just define a structure that is the same as the PCX header
and just import the whole thing in one shot?

'/////

'Define the PCX header structure
Public Structure PcxHeader
Public Mfg As Byte
Public Version As Byte
Public RLE As Byte
Public Bpp As Byte
Public XMin As Short
Public YMin As Short
Public XMax As Short
Public YMax As Short
Public HDpi As Short
Public VDpi As Short
<VBFixedString(48)> Public Palette As String
Public RFU1 As Byte
Public BitPlanes As Byte
Public VMem As Short
Public PaletteType As Short
Public HScreen As Short
Public VScreen As Short
<VBFixedString(54)> Public RFU As String
End Structure

'This function reads it in a returns it.
Private Function GetPcxHeader(ByVal vFile As String) As PcxHeader
Dim pheader As PcxHeader
Dim fHandle As Integer = FreeFile()

FileOpen(fHandle, vFile, OpenMode.Random, OpenAccess.Read,
OpenShare.Shared)
FileGet(fHandle, pheader)
FileClose(fHandle)

Return pheader
End Function

'////////

Chris
 
In my example that I posted, I used a fixed length string for the Palette
and the reserved bytes. Is there anyway to use a byte array instead?

I tried adding Public Palette(47) As Byte to the structure but it does
not allow that. I can create a parameterized constructor to initialize
the arrays, but that seems messy.

Is there any other way to specify a range of bytes in a structure?
 
Hello,

Ken Dopierala Jr. said:
As a follow up, I can do this with API calls but I was wondering
if there was a .Net way to turn 2 bytes into a short, 4 bytes into
an int and etcs.

Have a look at the 'BitConverter' class.
 
Is there any other way to specify a range of bytes in a structure?

Ok, I figured it out for my self. Just use the VBFixedArray attribute
instead:

Public Structure PcxHeader
Public Mfg As Byte
Public Version As Byte
Public RLE As Byte
Public Bpp As Byte
Public XMin As Short
Public YMin As Short
Public XMax As Short
Public YMax As Short
Public HDpi As Short
Public VDpi As Short
<VBFixedArray(47)> Public Palette() As Byte
Public RFU1 As Byte
Public BitPlanes As Byte
Public VMem As Short
Public PaletteType As Short
Public HScreen As Short
Public VScreen As Short
<VBFixedArray(53)> Public RFU() As Byte
End Structure
 
Thanks Chris!
That is very cool. Ken.

Chris Dunaway said:
Ok, I figured it out for my self. Just use the VBFixedArray attribute
instead:

Public Structure PcxHeader
Public Mfg As Byte
Public Version As Byte
Public RLE As Byte
Public Bpp As Byte
Public XMin As Short
Public YMin As Short
Public XMax As Short
Public YMax As Short
Public HDpi As Short
Public VDpi As Short
<VBFixedArray(47)> Public Palette() As Byte
Public RFU1 As Byte
Public BitPlanes As Byte
Public VMem As Short
Public PaletteType As Short
Public HScreen As Short
Public VScreen As Short
<VBFixedArray(53)> Public RFU() As Byte
End Structure
 
Back
Top