CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 5 of 5

Thread: long to byte

  1. #1
    Join Date
    Mar 1999
    Posts
    22

    long to byte

    Anyone plse help
    How do I convert a long data type to 2 bytes
    cbyte only converts to one byte
    This is to transmit a long crc computation result
    by com

    Thanks,
    Martin
    [email protected]



  2. #2
    Join Date
    May 1999
    Posts
    3,332

    Re: long to byte

    no need to convert

    Dim lon as Long
    Dim intv as Integer
    lon = 123
    intv = lon



    unless you get an error, because the long value is too big.


  3. #3
    Join Date
    Mar 1999
    Posts
    22

    Re: long to byte

    That is just my problem that the long is too large
    for one byte I need to get it into a high and low byte


  4. #4
    Join Date
    May 1999
    Posts
    3,332

    Re: long to byte

    try these functions...

    private Function HiWord(dw as Long) as Integer
    If dw And &H80000000 then
    HiWord = (dw \ 65535) - 1
    else
    HiWord = dw \ 65535
    End If
    End Function

    private Function LoByte(w as Integer) as Byte
    LoByte = w And &HFF
    End Function

    private Function LoWord(dw as Long) as Integer
    If dw And &H8000& then
    LoWord = &H8000 Or (dw And &H7FFF&)
    else
    LoWord = dw And &HFFFF&
    End If
    End Function

    private Function HiByte(byval w as Integer) as Byte
    If w And &H8000 then
    HiByte = &H80 Or ((w And &H7FFF) \ &HFF)
    else
    HiByte = w \ 256
    End If
    End Function






  5. #5
    Guest

    Re: long to byte


    Dim v(3) as Byte, x as Long

    ...
    ...
    ...

    for i = 0 to 3
    v(i) = x Mod 256
    x = Int(x / 256)
    next

    v contains the four bytes, from
    the lowest to the highest one.





Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured