|
-
Jan 15th, 2013, 09:00 PM
#1
Thread Starter
Hyperactive Member
Reading bits in Big Endian format.
I have never had to bother with Big Endian format before. Recently, my work has required me to mess with it a bit and it's a complete headache.
I understand that to convert from little endian (&h12345678) to big endian we just switch the byte order (&h78563412). I get that.
My problem comes when I need to read and write bits. There is a part in a file where each value consists of nine (9) bits. These values are consecutive. This means the first value uses all eight bits of the first byte and the first bit of the second byte. The second value uses the last seven bits of the second byte and the first two bits of the third byte, and so on.
For little endian I would just create a mask, such as &H1FF for nine bits, and mask off the part I need. No matter what I try, I cannot figure out how I would do this for big endian.
So, my question is: How would I read / write a specific amount of bits from a value stored in big endian format?
Thanks.
Prefix has no suffix, but suffix has a prefix.
-
Jan 16th, 2013, 02:24 AM
#2
Re: Reading bits in Big Endian format.
Not sure how you read that "value" and how long that value (bytes) is. Normally I would suggest reading the "value" into a byte array.
Have a look at this:
http://msdn.microsoft.com/en-us/libr...converter.aspx
-
Jan 16th, 2013, 02:52 AM
#3
Thread Starter
Hyperactive Member
Re: Reading bits in Big Endian format.
Let me try this again. Say I have the following value: &H5858A911A. This is the little endian representation of the value.
The big endian representation of the value would be: &H1A918A855.
Now, I figured out how to read bits from the value. For example, to read 9 bits starting at bit 0 I would simply use the following:
vb.net Code:
result = (value >> (32 - Length - b)) And Mask
Length = 9 (bits)
b is the bit to start at (0)
Mask is a nine bit mask (&H1FF)
My issue comes when I try to write nine bits in big endian format. I figure it would simply be the opposite:
vb.net Code:
result = value << (32 - Length - b)
Then I could OR the bits in. It's just not working out. I was hoping someone had dealt with this before and had any info to share.
Prefix has no suffix, but suffix has a prefix.
-
Jan 16th, 2013, 03:30 AM
#4
Re: Reading bits in Big Endian format.
 Originally Posted by Troy Lundin
...I was hoping someone had dealt with this before......
Ehhh....yes.
Did you read the link?
Just read the bytes in the right order in a byte array.
Convert to a datatype depending the nbr of bytes.
Mask the bits (9 in your case).
-
Jan 16th, 2013, 09:54 AM
#5
Re: Reading bits in Big Endian format.
 Originally Posted by Troy Lundin
Let me try this again. Say I have the following value: &H5858A911A. This is the little endian representation of the value.
The big endian representation of the value would be: &H1A918A855.
Now, I figured out how to read bits from the value. For example, to read 9 bits starting at bit 0 I would simply use the following:
vb.net Code:
result = (value >> (32 - Length - b)) And Mask
Length = 9 (bits)
b is the bit to start at (0)
Mask is a nine bit mask (&H1FF)
My issue comes when I try to write nine bits in big endian format. I figure it would simply be the opposite:
vb.net Code:
result = value << (32 - Length - b)
Then I could OR the bits in. It's just not working out. I was hoping someone had dealt with this before and had any info to share.
It is unclear which bit 0 (little or big version of the value) you want.
Code:
Dim foo As Integer = &HDEADBEEF
Dim bar As Integer = Net.IPAddress.HostToNetworkOrder(foo) 'convert endianess
Dim mask As Integer = &H1FF
Dim result As Integer = bar And (mask << 0) 'adjust shift as needed
Dim resultli As Integer = Net.IPAddress.NetworkToHostOrder(result) 'convert endianess
IPAddress has an overload for longs also.
-
Jan 16th, 2013, 10:52 AM
#6
Re: Reading bits in Big Endian format.
I would perform the extraction of the 9 bit values separately from considering the endian-ness. One way you could do this is reading the big-endian encoded bytes and writing a little-endian encoded set of bytes. Since you now have the bytes in little-endian format you can use the same access code as before. It's unclear at what level the endian-ness is relevant, so this might be exactly what you need, but the principle is to separate orthogonal concerns: how bytes are encoded is separate from the values that they represent.
-
Jan 16th, 2013, 02:05 PM
#7
Thread Starter
Hyperactive Member
Re: Reading bits in Big Endian format.
Just wanted to let you guys know I figured it out. The functions are below.
vb.net Code:
''' <summary>
''' Writes the specified value to the buffer.
''' </summary>
''' <param name="Offset">Required. The offset to start writing.</param>
''' <param name="Bit">Required. The bit to start writing.</param>
''' <param name="Length">Required. The number of bits to write.</param>
''' <param name="Value">Required. The value to write.</param>
Public Shared Sub NumberBase(ByVal Offset As Int32, ByVal Bit As Int32, ByVal Length As Int32, ByVal Value As Int32)
Try
Dim b As Int32 = Bit ' Bit currently being manipulated
Dim ofs As Int32 = Offset ' Offset being written
Dim val As Int32 = 0 ' Value to write
Dim State As Boolean ' State of current bit
' Precautionary removal of negative sign.
Length = Math.Abs(Length)
' Modify the bit and offset accordingly.
While b > 8
b -= 8
ofs += 1
End While
' Shift the value to the left.
val = Value << (32 - Length - b)
' Loop through each bit.
For i As Int32 = b To Length + b - 1
State = CBool((val >> (31 - i)) And 1) ' Get the state of the bit.
BitflagBase(ofs, i, State, False) ' Set it in the buffer.
Next
Catch ex As Exception
GetError(ex)
End Try
End Sub
''' <summary>
''' Sets the state of the specified bit relative to the specified offset, inverting if necessary.
''' </summary>
''' <param name="Offset">Required. The offset at which to start.</param>
''' <param name="Bit">Required. The bit whose state is to be set.</param>
''' <param name="State">Required. Whether or not the bit is set.</param>
''' <param name="Invert">Required. Inverts the state.</param>
Public Shared Sub BitflagBase(Offset As Int32, Bit As Int32, State As Boolean, Invert As Boolean)
Try
Dim b As Int32 = Bit
Dim ofs As Int32 = Offset
Dim temp As Int32 = 0
' If the bit we want isn't in the offset provided,
' Move on to the next offset.
While b >= 8
b -= 8
ofs += 1
End While
' Read the byte.
Select Case EditType
Case EditTypeEnum.Buffer
temp = Common.Buffer(ofs)
Case EditTypeEnum.Memory_IRAM
temp = ReadIRAM(ofs, 1)
Case EditTypeEnum.Memory_WRAM
temp = ReadWRAM(ofs, 1)
End Select
' Invert, if necessary.
State = If(Invert, Not State, State)
' Manipulate the bit.
temp = If(State, temp Or (1 << (7 - b)), temp And Not (1 << (7 - b)))
' Write the byte.
Select Case EditType
Case EditTypeEnum.Buffer
Common.Buffer(ofs) = CByte(temp And &HFF)
Case EditTypeEnum.Memory_IRAM
WriteIRAM(ofs, 1, temp And &HFF)
Case EditTypeEnum.Memory_WRAM
WriteWRAM(ofs, 1, temp And &HFF)
End Select
Catch ex As Exception
GetError(ex)
End Try
End Sub
Prefix has no suffix, but suffix has a prefix.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|