473,221 Members | 1,845 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,221 software developers and data experts.

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 16289
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
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
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
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
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
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
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
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
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
by: xiao | last post by:
Can I fullfill this task? Using fred and fwrite?
1
isladogs
by: isladogs | last post by:
The next online meeting of the Access Europe User Group will be on Wednesday 6 Dec 2023 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, Mike...
0
by: VivesProcSPL | last post by:
Obviously, one of the original purposes of SQL is to make data query processing easy. The language uses many English-like terms and syntax in an effort to make it easy to learn, particularly for...
3
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 3 Jan 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). For other local times, please check World Time Buddy In...
0
by: jianzs | last post by:
Introduction Cloud-native applications are conventionally identified as those designed and nurtured on cloud infrastructure. Such applications, rooted in cloud technologies, skillfully benefit from...
2
by: jimatqsi | last post by:
The boss wants the word "CONFIDENTIAL" overlaying certain reports. He wants it large, slanted across the page, on every page, very light gray, outlined letters, not block letters. I thought Word Art...
0
by: fareedcanada | last post by:
Hello I am trying to split number on their count. suppose i have 121314151617 (12cnt) then number should be split like 12,13,14,15,16,17 and if 11314151617 (11cnt) then should be split like...
0
by: stefan129 | last post by:
Hey forum members, I'm exploring options for SSL certificates for multiple domains. Has anyone had experience with multi-domain SSL certificates? Any recommendations on reliable providers or specific...
0
Git
by: egorbl4 | last post by:
Скачал я git, хотел начать настройку, а там вылезло вот это Что это? Что мне с этим делать? ...
0
by: MeoLessi9 | last post by:
I have VirtualBox installed on Windows 11 and now I would like to install Kali on a virtual machine. However, on the official website, I see two options: "Installer images" and "Virtual machines"....

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.