By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
455,255 Members | 1,264 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 455,255 IT Pros & Developers. It's quick & easy.

How do I convert 2 bytes to a Short.

P: n/a
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.
Nov 20 '05 #1
Share this Question
Share on Google+
8 Replies


P: n/a
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
"Ken Dopierala Jr." <kd*********@wi.rr.com> wrote in message
news:%2***************@TK2MSFTNGP10.phx.gbl...
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.

Nov 20 '05 #2

P: n/a
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.

"Ken Dopierala Jr." <kd*********@wi.rr.com> wrote in message
news:%2***************@TK2MSFTNGP10.phx.gbl...
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.

Nov 20 '05 #3

P: n/a
Hi Jay,
It works sweet! Thanks! Ken.

"Jay B. Harlow [MVP - Outlook]" <Ja********@email.msn.com> wrote in message
news:%2****************@TK2MSFTNGP09.phx.gbl...
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
"Ken Dopierala Jr." <kd*********@wi.rr.com> wrote in message
news:%2***************@TK2MSFTNGP10.phx.gbl...
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.


Nov 20 '05 #4

P: n/a
"Ken Dopierala Jr." <kd*********@wi.rr.com> wrote in
news:#R*************@TK2MSFTNGP10.phx.gbl:
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
Nov 20 '05 #5

P: n/a
Chris Dunaway <du******@lunchmeatsbcglobal.net> wrote in
news:Xn********************************@207.46.248 .16:

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

Nov 20 '05 #6

P: n/a
Hello,

"Ken Dopierala Jr." <kd*********@wi.rr.com> schrieb:
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.

--
Herfried K. Wagner
MVP VB Classic, VB.NET
http://www.mvps.org/dotnet
Nov 20 '05 #7

P: n/a
Chris Dunaway <du******@lunchmeatsbcglobal.net> wrote in
news:Xn**********************************@207.46.2 48.16:
Chris Dunaway <du******@lunchmeatsbcglobal.net> wrote in
news:Xn********************************@207.46.248 .16:
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
Nov 20 '05 #8

P: n/a
Thanks Chris!
That is very cool. Ken.

"Chris Dunaway" <du******@lunchmeatsbcglobal.net> wrote in message
news:Xn**********************************@207.46.2 48.16...
Chris Dunaway <du******@lunchmeatsbcglobal.net> wrote in
news:Xn**********************************@207.46.2 48.16:
Chris Dunaway <du******@lunchmeatsbcglobal.net> wrote in
news:Xn********************************@207.46.248 .16:
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

Nov 20 '05 #9

This discussion thread is closed

Replies have been disabled for this discussion.