Believe it or not have a 3bytes that I want to convert to a unsigned 24 bit integer in VB. Anyone know how to do this in .net? Is it even possible?
Printable View
Believe it or not have a 3bytes that I want to convert to a unsigned 24 bit integer in VB. Anyone know how to do this in .net? Is it even possible?
As I know, VB does not support 24 bit integer, but depending on why you need to do it, it may be many solutions.
For example, you can create your own data type, emulating a 24 bit behavior, but why no use int32? to save space? for compatibility with some algorithm?
Quick solution: use the top 24bits of a 32 bit integer: just multiply everything by 2^8, manipulate, divide by 2^8 (ideally, you'd use logical shifts, but I'mm not certain that's possible in VB)
Yea I was trying to avoid breaking everything down into binary and doing the math but it looks like i have no choice. The 24 bit thing was not my choice just happens to be what I am looking at I wish it was 32bit I could just use the Bitconverter and that would be the end of it but that would be too easy.
This class intend to raise exceptions on 24 bit overflows.
Note that it needs more operators defined (i/e. integer + SignedInt24 is not the same as SignedInt24 + Integer)
it does not use bit shift, but the bit shift operator is
integer << 8 '(equals integer*256)
integer >> 8 '(equals integer/256)
Code:Public Class Form1
Friend Class SignedInt24
Private Shared Function ToInternalRepresentation(ByRef value As Int32) As Int32
Return 256 * value 'Shift the bits to the left <-8 bits-
End Function
Private Shared Function ToExternalRepresentation(ByRef value As Int32) As Int32
Return CInt(value / 256) 'Shift the bits to the right -8 bits->
End Function
Private InternalValue As Int32 = 0
Public Property Value() As Int32
Get
Return ToExternalRepresentation(InternalValue)
End Get
Set(ByVal NewValue As Int32)
InternalValue = ToInternalRepresentation(NewValue)
End Set
End Property
Private ReadOnly Property ToExternal() As Int32
Get
Return ToExternalRepresentation(InternalValue)
End Get
End Property
Friend Sub New(Optional ByRef NewValue As Int32 = 0)
InternalValue = ToInternalRepresentation(NewValue)
End Sub
Public Shared Operator *(ByVal class1 As SignedInt24, _
ByVal class2 As SignedInt24) As SignedInt24
Static Result As New SignedInt24
Result.InternalValue = ToInternalRepresentation(class1.ToExternal * class2.ToExternal)
Return Result
End Operator
Public Shared Operator *(ByVal class1 As SignedInt24, _
ByVal Other As Integer) As SignedInt24
Return New SignedInt24(class1.ToExternal * Other)
End Operator
Public Shared Operator +(ByVal class1 As SignedInt24, _
ByVal class2 As SignedInt24) As SignedInt24
Static Result As New SignedInt24
Result.InternalValue = class1.InternalValue + class2.InternalValue
Return Result
End Operator
Public Shared Operator +(ByVal class1 As SignedInt24, _
ByVal other As Int32) As SignedInt24
Static Result As New SignedInt24
Result.InternalValue = class1.InternalValue + ToInternalRepresentation(other)
Return Result
End Operator
Friend Shared ReadOnly Property MaxValue() As Int32
Get
Return ToExternalRepresentation(Int32.MaxValue)
End Get
End Property
End Class 'SignedInt24
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
Dim a As New SignedInt24(Int16.MaxValue)
Dim b As New SignedInt24 : b.Value = 256
Dim c As New SignedInt24
c = a * b
MsgBox("Int16.MaxValue * 256 = " & c.Value & vbCrLf & _
"(1 + Int16.MaxValue) * 256 = " & 256 + c.Value & vbCrLf & _
"SignedInt24.MaxValue = " & SignedInt24.MaxValue)
MsgBox("SignedInt24.MaxValue +1 should throw an overflow exception")
Dim d As New SignedInt24(SignedInt24.MaxValue)
d += 1
End Sub
End Class
Code:Dim b() As Byte = New Byte() {7, 5, 3} 'test bytes
Dim x, y, bi, result As Integer
Dim ab As Byte
x = 0
For y = 23 To 0 Step -1
If (y + 1) Mod 8 = 0 Then
ab = b(x) 'get next byte
x += 1
bi = 7
End If
If (ab And CByte(2 ^ bi)) = CByte(2 ^ bi) Then
result = result Or CInt(2 ^ y)
End If
bi -= 1
Next
Why over complicate it???
Just use a regulat 32 bit integer for all internal calculations. IGNORE the most significant byte.
Remember integer operations "wrap around" they do NOT "overflow".
The only possible check you MIGHT need is to clean up the upper byte in the event of partial overflows, but that is rarely necessary.
I agree entirely, although I'd use the MSB
Surely it makes more sense to ignore the LSB from a design perspective (i.e. two's complement interpretation, allowing certain assembler flags will be correct). Of course, sign extension may solve this, but leads to it's own set of problems.
Or is there a simple correction which I'm missing here?
Consider all of the extra work you have to do (that I dont) to perform operations with integer variables of differing sizes.
The LEAST significant bit should ALWAYS mean "1". You use as many significant bits as necessary to carry the necessary information.
"Psuedo"
Code:System.Int8 a;
System.Int16 b;
MySpecial.Int24 c;
System.Int32 d;
Console.Writeline(a+b+c+d);
Is bit padding much more difficult to do?
lol yea byte sorry. So just out of curiosity what if you had a to convert 9 bytes into a 6 bit integer, is this even possible?