CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 4 of 4
  1. #1
    Join Date
    Mar 1999
    Posts
    7

    How do I extract doubles, longs, integers etc, from a BYTE array



    OK, I have a byte array that contains a stream of binary data packets. Some are doubles(8bytes), some longs(4 bytes) etc. For this example lets say the first 8 bytes of my array is a binary double value the next 4 bytes are a binary long integer, the next 2 are a binary integer. How do I assign the first 8 bytes to my variable dDblx, the next four to my variable lLngx and the next two bytes

    to my variable iIntx?


    I can not use a Type struct because the bytes are not always in this order. Any suggestions?


    Thanks, Terry



  2. #2
    Join Date
    Mar 1999
    Posts
    3

    Re: How do I extract doubles, longs, integers etc, from a BYTE array



    I found one example of bit array managment on the Web

    I am not sure that it can help you, but this is the address :

    http://www.softcircuits.com/sw_vbsrc.htm

    Bit Array Class Demo [VB5]

  3. #3
    Join Date
    May 1999
    Posts
    45

    Re: How do I extract doubles, longs, integers etc, from a BYTE array



    It depends on how do you save values in the array. If you are saving this array from C prog probably memcpy API can serve the purpose.

    for ex. suppose you want 10,11,12,13 element should be treated as a long


    Dim lVal as Long

    Dim byData as BYTE(1 to 100)

    memcpy(byData[10], lVal, len(lVal))


    should do the job.




  4. #4
    Join Date
    Mar 1999
    Posts
    4

    Re: How do I extract doubles, longs, integers etc, from a BYTE array



    I'll give you an explanation on how.


    say you got an array and you want to extract an integer (we'll assume vb 16 bit) value from the array starting at offset 4.


    Take the first value and shift it times 8 to the next byte value,

    this can be done in vb by multiplying the value * (256) or (&h100),

    then take that value and add the 2nd byte to it.


    example:

    loading in an integer value from a byte array

    myans = byte1 * &h100

    myans = myans + byte2


    example:

    loading a long value from a byte array

    myans = byte1 * &h1000000

    myans = myans + (byte2 * &h10000)

    myans = myans + (byte3 * &h100)

    myans = myans + byte4


    'chart breaking the hex values into decimal values for you

    Hex value Dec value

    &h1000000 16777216

    &h10000 65536

    &h100 256


    **remember when multiplying you are actually shifting to the left

    **when you divide you shift to the right


    I hope I helped you more than confused you, I'm not good at explaining things

    but believe me the above code works and it works well, you could divise a function that you pass in the array, the start element, and how a value indicating how many bytes you want then return from the function the value.

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