473,507 Members | 13,917 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

How do I convert 2 bytes to a Short.

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
8 16356
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
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
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
"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
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
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
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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
11026
by: Graham Nicholls | last post by:
Hi, I'm trying to size a jpeg file. The file size is held in a short (2 byte integer) at a certain offset. Once I've found these two bytes (they're in MSB,LSB order), I need to convert them to...
12
2292
by: DanSteph | last post by:
hello all, I have a problem, I have a DOUBLE value like that 4065 4000 0000 0000 and I want to convert it to a DWORD like that: 4065 4000 if I do result=(DWORD)MyDouble it end with...
13
7873
by: Bryan Parkoff | last post by:
I have two variables: "char A" and "short B". I can be able to convert from A to B using explicit case conversion with no problem like "B = short (A);". Right now, I have two variables: "char T"...
5
64442
by: babylon | last post by:
I can't find any functions in system.convert to convert a 4-byte integer to 4 bytes array... pls help
5
18806
by: kevinniu | last post by:
Hi everyone, In c#, what is the fastest way(include unsafe) to convert a array of bytes(which really contains the bytes of a double array) to a arry of double. thanks
3
10345
by: MuZZy | last post by:
Hi, I just wonder if someone can help me wit this - i have a byte array and need to convert it to short array, creating short numbers by combining bytes by pairs: My array: byte, byte, byte,...
7
13320
by: LaMoRt | last post by:
Hi I want to ask how to convert from long to short since i have some parameter problem with it? The problem is like below : short *speech; // Holds length of any given sample *...
4
4554
by: Joao Tomas | last post by:
Please, can someone help me to translate this single function to VB.NET ? ' DECLARATIONS Private Const BMP_MAGIC_COOKIE As Short = 19778 Private Structure BITMAPFILEHEADER '14 bytes...
22
11723
by: xiao | last post by:
Can I fullfill this task? Using fred and fwrite?
0
7221
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
7372
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
1
7029
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
7481
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
5619
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
4702
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
3190
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
1
758
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
411
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.