473,769 Members | 2,100 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 16395
Ken,
You can use System.BitConve rter.ToInt16 to convert two bytes to a short.

The System.BitConve rter 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.To Int16(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******** *******@TK2MSFT NGP10.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******** *******@TK2MSFT NGP10.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********@ema il.msn.com> wrote in message
news:%2******** ********@TK2MSF TNGP09.phx.gbl. ..
Ken,
You can use System.BitConve rter.ToInt16 to convert two bytes to a short.

The System.BitConve rter 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.To Int16(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******** *******@TK2MSFT NGP10.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******** *****@TK2MSFTNG P10.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(By Val vFile As String) As PcxHeader
Dim pheader As PcxHeader
Dim fHandle As Integer = FreeFile()

FileOpen(fHandl e, vFile, OpenMode.Random , OpenAccess.Read ,
OpenShare.Share d)
FileGet(fHandle , pheader)
FileClose(fHand le)

Return pheader
End Function

'////////

Chris
Nov 20 '05 #5
Chris Dunaway <du******@lunch meatsbcglobal.n et> wrote in
news:Xn******** *************** *********@207.4 6.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******@lunch meatsbcglobal.n et> wrote in
news:Xn******** *************** ***********@207 .46.248.16:
Chris Dunaway <du******@lunch meatsbcglobal.n et> wrote in
news:Xn******** *************** *********@207.4 6.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(4 7)> 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(5 3)> Public RFU() As Byte
End Structure
Nov 20 '05 #8
Thanks Chris!
That is very cool. Ken.

"Chris Dunaway" <du******@lunch meatsbcglobal.n et> wrote in message
news:Xn******** *************** ***********@207 .46.248.16...
Chris Dunaway <du******@lunch meatsbcglobal.n et> wrote in
news:Xn******** *************** ***********@207 .46.248.16:
Chris Dunaway <du******@lunch meatsbcglobal.n et> wrote in
news:Xn******** *************** *********@207.4 6.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(4 7)> 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(5 3)> 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
11043
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 an integer - now I know that in C I'd just cast a pointer to the offset to a short, and that python doesn't cast, so how can I extract the value from a stream of bytes. I've looked at python.org/Doc/current (I'm using 2.3b1), but can't find...
12
2321
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 AA(170)
13
7924
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" and "short A". T has an array of six elements. I desire to capture first element and second element as two bytes into word as short. The problem is that "A" captures only one element instead of two elements. I have looked at machine language...
5
64466
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
18831
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
10364
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, etc. I need: short = byte+byte, short = byte+byte, etc.
7
13360
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 * speech = pMediaSample->GetActualDataLength(); va_g729a_encoder(short *speech, unsigned char *bitstream );
4
4563
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 Dim bfType As Short
22
11782
by: xiao | last post by:
Can I fullfill this task? Using fred and fwrite?
0
10215
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10049
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
9996
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
9865
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
8872
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, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
7410
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6674
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 then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5307
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 last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
5447
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?

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.