DevX Home    Today's Headlines   Articles Archive   Tip Bank   Forums   

+ Reply to Thread
Results 1 to 3 of 3
  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/



Bookmarks

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


Top DevX Stories

Easy Web Services with SQL Server 2005 HTTP Endpoints
JavaOne 2005: Java Platform Roadmap Focuses on Ease of Development, Sun Focuses on the "Free" in F.O.S.S.
Wed Yourself to UML with the Power of Associations
Microsoft to Add AJAX Capabilities to ASP.NET
IBM's Cloudscape Versus MySQL


Sponsored Links