Converting a byte array to an integer (4 byte integer)


DevX Home    Today's Headlines   Articles Archive   Tip Bank   Forums   

Results 1 to 3 of 3

Thread: Converting a byte array to an integer (4 byte integer)

  1. #1
    Cody Laird Guest

    Converting a byte array to an integer (4 byte integer)


    I have a VB 6 program that takes in a byte array (stream, if you will) and
    converts it to/from VB and MVS COBOL data types. I think I have a handle
    on all the other data types, but I'm not sure about how to get from an array
    of 4 bytes that make up an integer to the integer itself or a string representation
    of the integer.

    For example a COBOL integer is 4 bytes and the following code converts its
    stream representation to a long in VB6 using the LSET function. How would
    one go about this in VB.Net ?

    Here are the functions that convert to/from a byte array of a long to/from
    a long. The first will convert a 4 byte array to a long the seond converts
    a long to a 4 byte array.

    Private Type LongByteType
    b1 As Byte
    b2 As Byte
    b3 As Byte
    b4 As Byte
    End Type
    Private Type LongType
    l As Long
    End Type
    Private LongRec As LongByteType
    Private MyLong As LongType

    Public Function ByteArraytoLong(pArray As Variant) As Long
    LongRec.b1 = pArray(0)
    LongRec.b2 = pArray(1)
    LongRec.b3 = pArray(2)
    LongRec.b4 = pArray(3)

    LSet MyLong = LongRec

    ByteArraytoLong = MyLong.l
    End Function

    Public Function LongToByteArray(pLong As Long) As Variant
    Dim tArray(0 To 3) As Byte

    MyLong.l = pLong

    LSet LongRec = MyLong

    tArray(0) = LongRec.b1
    tArray(1) = LongRec.b2
    tArray(2) = LongRec.b3
    tArray(3) = LongRec.b4

    LongToByteArray = tArray
    End Function

  2. #2
    Mattias Sjögren Guest

    Re: Converting a byte array to an integer (4 byte integer)

    Cody,

    >For example a COBOL integer is 4 bytes and the following code converts its
    >stream representation to a long in VB6 using the LSET function. How would
    >one go about this in VB.Net ?


    Have a look at the BitConverter class.


    Mattias

    ===
    Mattias Sjögren (VB MVP)
    mattias @ mvps.org
    http://www.msjogren.net/dotnet/

  3. #3
    Cody Laird Guest

    Re: Converting a byte array to an integer (4 byte integer)


    Mattias:

    Perfect! Knowing where to look is half the batttle!

    Thanks!
    Cody

    Mattias Sjögren <mattias.dont.want.spam@mvps.org> wrote:
    >Cody,
    >
    >>For example a COBOL integer is 4 bytes and the following code converts

    its
    >>stream representation to a long in VB6 using the LSET function. How would
    >>one go about this in VB.Net ?

    >
    >Have a look at the BitConverter class.
    >
    >
    >Mattias
    >
    >===
    >Mattias Sjögren (VB MVP)
    >mattias @ mvps.org
    >http://www.msjogren.net/dotnet/



Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
HTML5 Development Center
 
 
FAQ
Latest Articles
Java
.NET
XML
Database
Enterprise
Questions? Contact us.
C++
Web Development
Wireless
Latest Tips
Open Source


   Development Centers

   -- Android Development Center
   -- Cloud Development Project Center
   -- HTML5 Development Center
   -- Windows Mobile Development Center