473,548 Members | 2,598 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Howto convert a bitsfield to an int16

Hi all,

I have a bitsfield

Structure MyBits
a as boolean
b as boolean
c as boolean
d as boolean
end structure

I wish to convert it into an Int16 from the binary value of my structure.

Thanks for your help

Nov 21 '05 #1
3 2296
Try this instead . . . There's maybe a slicker way to do this, but this may
get you out of trouble !

Class MyBits
Private i16 As BitArray

Public Sub New()
i16 = New BitArray(16, False)
End Sub

Default Public Property bits(ByVal bitIndex As Int16) As Boolean
Get
Return i16.Item(bitInd ex)
End Get
Set(ByVal Value As Boolean)
i16.Set(bitInde x, Value)
End Set
End Property

Public ReadOnly Property int16Value() As Int16
Get
Dim val As Int16
Dim i As Int16
If i16(0) Then val = 1
For i = 1 To i16.Length - 1
If i16.Item(i) Then
val += 2 ^ (i - 1)
End If
Next
Return val

End Get
End Property

End Class

--
OHM ( Terry Burns )

http://TrainingOn.net


"QuocSi" <pq*@pqsweb.com > wrote in message
news:42******** **************@ news.free.fr...
Hi all,

I have a bitsfield

Structure MyBits
a as boolean
b as boolean
c as boolean
d as boolean
end structure

I wish to convert it into an Int16 from the binary value of my structure.

Thanks for your help

Nov 21 '05 #2
QuocSi,
As OHM suggests I would consider either a BitArray or a BitVector32.

http://msdn.microsoft.com/library/de...classtopic.asp

http://msdn.microsoft.com/library/de...classtopic.asp

OHM showed the BitArray, here is a BetVector32 sample:

Public Structure MyBits

Private Enum Bit
A
B
C
D
End Enum

Private m_value As BitVector32

Public Sub New(ByVal data As Int32)
m_value = New BitVector32(dat a)
End Sub

Public Property a() As Boolean
Get
Return m_value.Item(Bi t.A)
End Get
Set(ByVal value As Boolean)
m_value.Item(Bi t.A) = value
End Set
End Property

Public Property b() As Boolean
Get
Return m_value.Item(Bi t.B)
End Get
Set(ByVal value As Boolean)
m_value.Item(Bi t.B) = value
End Set
End Property

Public Property c() As Boolean
Get
Return m_value.Item(Bi t.C)
End Get
Set(ByVal value As Boolean)
m_value.Item(Bi t.C) = value
End Set
End Property

Public Property d() As Boolean
Get
Return m_value.Item(Bi t.D)
End Get
Set(ByVal value As Boolean)
m_value.Item(Bi t.D) = value
End Set
End Property

Public Function ToInt16() As Int16
Return CShort(m_value. Data)
End Function

Public Function ToInt32() As Int32
Return m_value.Data
End Function

Public Shared Function FromInt16(ByVal value As Int16) As MyBits
Return New MyBits(value)
End Function

Public Shared Function FromInt32(ByVal value As Int32) As MyBits
Return New MyBits(value)
End Function

End Structure
Hope this helps
Jay

"QuocSi" <pq*@pqsweb.com > wrote in message
news:42******** **************@ news.free.fr...
| Hi all,
|
| I have a bitsfield
|
| Structure MyBits
| a as boolean
| b as boolean
| c as boolean
| d as boolean
| end structure
|
| I wish to convert it into an Int16 from the binary value of my structure.
|
| Thanks for your help
|
|
|
Nov 21 '05 #3
Doh!

I defined Enum Bit incorrectly, the function wants the mask for the bits,
not the index of said bits. Try the following Enum instead.

Private Enum Bit
A = 1 << 0
B = 1 << 1
C = 1 << 2
D = 1 << 3
End Enum

If you use VB.NET 2002 try:

Private Enum Bit
A = 1
B = 2
C = 4
D = 8
End Enum
Hope this helps
Jay

"Jay B. Harlow [MVP - Outlook]" <Ja************ @msn.com> wrote in message
news:OL******** ******@TK2MSFTN GP15.phx.gbl...
| QuocSi,
| As OHM suggests I would consider either a BitArray or a BitVector32.
|
|
http://msdn.microsoft.com/library/de...classtopic.asp
|
|
http://msdn.microsoft.com/library/de...classtopic.asp
|
| OHM showed the BitArray, here is a BetVector32 sample:
|
| Public Structure MyBits
|
| Private Enum Bit
| A
| B
| C
| D
| End Enum
|
| Private m_value As BitVector32
|
| Public Sub New(ByVal data As Int32)
| m_value = New BitVector32(dat a)
| End Sub
|
| Public Property a() As Boolean
| Get
| Return m_value.Item(Bi t.A)
| End Get
| Set(ByVal value As Boolean)
| m_value.Item(Bi t.A) = value
| End Set
| End Property
|
| Public Property b() As Boolean
| Get
| Return m_value.Item(Bi t.B)
| End Get
| Set(ByVal value As Boolean)
| m_value.Item(Bi t.B) = value
| End Set
| End Property
|
| Public Property c() As Boolean
| Get
| Return m_value.Item(Bi t.C)
| End Get
| Set(ByVal value As Boolean)
| m_value.Item(Bi t.C) = value
| End Set
| End Property
|
| Public Property d() As Boolean
| Get
| Return m_value.Item(Bi t.D)
| End Get
| Set(ByVal value As Boolean)
| m_value.Item(Bi t.D) = value
| End Set
| End Property
|
| Public Function ToInt16() As Int16
| Return CShort(m_value. Data)
| End Function
|
| Public Function ToInt32() As Int32
| Return m_value.Data
| End Function
|
| Public Shared Function FromInt16(ByVal value As Int16) As MyBits
| Return New MyBits(value)
| End Function
|
| Public Shared Function FromInt32(ByVal value As Int32) As MyBits
| Return New MyBits(value)
| End Function
|
| End Structure
|
|
| Hope this helps
| Jay
|
| "QuocSi" <pq*@pqsweb.com > wrote in message
| news:42******** **************@ news.free.fr...
|| Hi all,
||
|| I have a bitsfield
||
|| Structure MyBits
|| a as boolean
|| b as boolean
|| c as boolean
|| d as boolean
|| end structure
||
|| I wish to convert it into an Int16 from the binary value of my structure.
||
|| Thanks for your help
||
||
||
|
|
Nov 21 '05 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

8
3827
by: Sachi | last post by:
Hi, Can anyone convert for me this Vb.bet code into C#? I am trying to convert example a sample from MSDN article. Static types() As Type = {GetType(String), GetType(Byte), _ GetType(Int16), GetType(Int32), GetType(Int64), GetType(Single), _ GetType(Double), GetType(Decimal), GetType
2
29477
by: jiangyh | last post by:
hi there : I have a question about how to convert Type to DbType? thanks a lot. jiangyh
5
18699
by: simon | last post by:
I have datetime variable: Datetime tsEndTime; Should I use (DateTime): tsEndTime=(DateTime)rdr.GetValue(15) or is better to use: tsEndTime=Convert.ToDateTime(rdr.GetValue(15))
0
1634
by: Pat Ireland | last post by:
To use the InvokeMember for calling a method in a dynamically loaded class requires that any arguments to the method must be past in an object . However, when I try to perform a pass by reference for an Int16 (short) object, I have not been able to successful make the conversion. Using boxing, gives me a type of object...
3
2317
by: Patrick Ireland | last post by:
I am dynamically loading a class. One of the methods of the class takes a short * (by reference) argument. However, the InvokeMember call used to invoke the method of the class passings arguments in and object . Since this is a pointer reference being passed it must be invoked in a unsafe {} even thought the dynamically loaded class is a...
9
12673
by: Charles Law | last post by:
Suppose I have a structure Private Structure MyStruct Dim el1 As Byte Dim el2 As Int16 Dim el3 As Byte End Structure I want to convert this into a byte array where
8
1938
by: Stephen Miller | last post by:
Hi, I am converting a code sample found at http://support.microsoft.com/default.aspx?scid=kb;en-us;828279 from C# to VB and I am stuck on one line of code. How would I convert: object pos = new object; (where numaxes is an int) to VB.Net?
6
1757
by: Glenn Wilson | last post by:
I have converted most of the code that I have but am having trouble, mainly with the marked lines. (>>) public static UInt16 checksum( UInt16 buffer, int size ) { Int32 cksum = 0; int counter; counter = 0; while ( size > 0 ) { UInt16 val = buffer;
0
1303
by: Jason | last post by:
I've got a program that wil convert an IP address to decimal. Anyone give me any infor on how I would modify this program that I had converting regular IPs to decimal, hwo would I make it work for an IPv6 address? Would I add just 8 instances of converting ? Thanks for any help.... Private Sub Button1_Click(ByVal sender As...
0
7438
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language...
0
7951
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...
1
7466
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...
0
7803
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...
0
3495
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...
0
3475
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
1926
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
1
1051
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
751
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 can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating...

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.