Big Endian vs Little Endian 
Author Message
 Big Endian vs Little Endian

I want to read in a PalmOS database file. I have the layout and I tried to
read in an integer. In Palm, a two byte integer of eight looks like this 00
08, but in VB, it is read as 2048. VB switch the two bytes around. I believe
this is the Big Endian vs the Little Endian problem.

I need to read in the file, allow users to modify the file, and then write
out to the file while still allowing PalmOS hand helds to read the file. Can
you help me?

Jeff Ayers



Wed, 18 Jun 1902 08:00:00 GMT  
 Big Endian vs Little Endian

Quote:

> I want to read in a PalmOS database file. I have the layout and I tried to
> read in an integer. In Palm, a two byte integer of eight looks like this 00
> 08, but in VB, it is read as 2048. VB switch the two bytes around. I believe
> this is the Big Endian vs the Little Endian problem.

> I need to read in the file, allow users to modify the file, and then write
> out to the file while still allowing PalmOS hand helds to read the file. Can
> you help me?

> Jeff Ayers

hmm.. i just whipped up the following. Seems to work quite fine.

Function SwapEndian(x As Integer) As Integer
    SwapEndian = ((x Mod 256) * 256) + (x \ 256)
End Function

Give that a go.

Jason



Wed, 18 Jun 1902 08:00:00 GMT  
 Big Endian vs Little Endian
Thanks, that works well with positive numbers. However, it doesn't work with
negative numbers. Is there an unsigned integer in VB?

Jeff Ayers


Quote:

> > I want to read in a PalmOS database file. I have the layout and I tried
to
> > read in an integer. In Palm, a two byte integer of eight looks like this
00
> > 08, but in VB, it is read as 2048. VB switch the two bytes around. I
believe
> > this is the Big Endian vs the Little Endian problem.

> > I need to read in the file, allow users to modify the file, and then
write
> > out to the file while still allowing PalmOS hand helds to read the file.
Can
> > you help me?

> > Jeff Ayers

> hmm.. i just whipped up the following. Seems to work quite fine.

> Function SwapEndian(x As Integer) As Integer
>     SwapEndian = ((x Mod 256) * 256) + (x \ 256)
> End Function

> Give that a go.

> Jason



Wed, 18 Jun 1902 08:00:00 GMT  
 Big Endian vs Little Endian
Hi Jeff,

Unfortunately VB does not have unsigned integers.
Here is a code that will swap the bytes correctly even for negative
integers.
I am using the same function name Jason created. You can use the Jason's
method of shifting bits, but you will have to add code to handle the case
when the high bit is set (negative numbers).

Instead of using the LSet statement as shown below, you can achieve the same
result with RTLMovememory() :

Public Declare Sub MoveMemory Lib "kernel32" Alias _
    "RtlMoveMemory" (dest As Any, Source As Any, _
                     ByVal Length As Long)

Copy this code to a Form that has Command1 button to test it:

Private Type T_HiLoByte
    hi As Byte
    lo As Byte
End Type

Private Type T_INT
    int As Integer
End Type

Private Sub Command1_Click()
   Debug.Print "&H" & Hex$(SwapEndian(&HAABB))

End Sub

Private Function SwapEndian(x As Integer) As Integer
    Dim uHiLoByte As T_HiLoByte
    Dim uInt As T_INT
    Dim bTemp As Byte

    'Put the integer in the uInt
    uInt.int = x

    'Get the high and low bytes
    LSet uHiLoByte = uInt

    'swap the bytes
    With uHiLoByte
        bTemp = .hi
        .hi = .lo
        .lo = bTemp
    End With

    'make the new integer
    LSet uInt = uHiLoByte

    'return the integer wit swapped bytes
    SwapEndian = uInt.int

End Function


Quote:
> Thanks, that works well with positive numbers. However, it doesn't work
with
> negative numbers. Is there an unsigned integer in VB?

> Jeff Ayers




> > > I want to read in a PalmOS database file. I have the layout and I
tried
> to
> > > read in an integer. In Palm, a two byte integer of eight looks like
this
> 00
> > > 08, but in VB, it is read as 2048. VB switch the two bytes around. I
> believe
> > > this is the Big Endian vs the Little Endian problem.

> > > I need to read in the file, allow users to modify the file, and then
> write
> > > out to the file while still allowing PalmOS hand helds to read the
file.
> Can
> > > you help me?

> > > Jeff Ayers

> > hmm.. i just whipped up the following. Seems to work quite fine.

> > Function SwapEndian(x As Integer) As Integer
> >     SwapEndian = ((x Mod 256) * 256) + (x \ 256)
> > End Function

> > Give that a go.

> > Jason



Mon, 26 May 2003 23:54:05 GMT  
 Big Endian vs Little Endian

Quote:
> Thanks, that works well with positive numbers. However, it doesn't work
with
> negative numbers. Is there an unsigned integer in VB?

No (except for the Byte). Bill Gates might just tackle this job once he has
finished counting his money! (Not himself, of course. He just wrote the
original {*filter*}Basic on which VB is based. He can't program for toffee - but
he is a very good salesman!).

Mike



Tue, 27 May 2003 01:07:28 GMT  
 
 [ 5 post ] 

 Relevant Pages 

1. Big-endian Little-endian integer swap

2. Big endian - Little endian question

3. little endian/ big endian data

4. C=>VB for big-endian to little-endian conversion

5. inet_addr big endian to little endian conversion

6. Convert Little-Endian to Big and back

7. Big-Endian to Little-Endian conversion... (help? Please?)

8. Big-Endian to Little-Endian conversion... (help? Please?)

9. Lost Little-Endian

10. Little Endian.......

11. Big endian udp stream

12. Binary Data - Big Endian Encoding

 

 
Powered by phpBB® Forum Software