How do I convert 2 bytes to a Short.

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.
 
J

Jay B. Harlow [MVP - Outlook]

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
 
K

Ken Dopierala Jr.

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.
 
K

Ken Dopierala Jr.

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
 
C

Chris Dunaway

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
 
C

Chris Dunaway

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?
 
H

Herfried K. Wagner [MVP]

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.
 
C

Chris Dunaway

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
 
K

Ken Dopierala Jr.

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
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top