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

    Combining And Splitting Bytes

    I have two bytes (8 bits each) I need to combine these into one interger. And then split the top 12 bits into one integer and the bottom 4 bits into another integer.

    i.e A = 0xFE
    B = 0x03

    First step combine two bytes: = 0xFE03
    binary - 11111110 00000011

    then split -------- ----++++ - = top 12 bits, + bottom 4 bits

    then store seperatly

    so one integer equals 0000111111100000
    and the other equals 0000000000000011


    Sorry it is hard to explain, please help.



  2. #2
    Join Date
    Sep 1999
    Location
    Trivandrum, Kerala, INDIA.
    Posts
    32

    Re: Combining And Splitting Bytes

    Try this module. I haven't tried different inputs, but I think it will work. Reply if it is to your satisfaction or if you have any further problems.

    Deepak



    Dim b1, b2 as Byte
    Dim str as string 'to see the hex number in string format


    Dim l1, l2, l3, l4, l5 as Long
    'though an integer is 2 bytes long, the maximum digital value
    ' that can be assigned is approx. 32000, which does not suffice,
    ' and so we go for the next larger data type, "long"

    ' &h is used to denote hex numbers
    b1 = &HF0
    ' Hex() converts the number to string format
    str = Hex(b1)

    'I've included the hex() after every statement to view the results of
    ' each operation
    b2 = &H1A
    str = Hex(b2)

    l1 = b1
    str = Hex(l1)

    l2 = b2
    str = Hex(l2)

    str = Hex(l1)
    ' to shift 1 bit you multiply by 2
    ' to shift 4 bits ( 1 nibble) multiply by 2 raised to 4 which is 16
    ' to shift 1 byte, you multiply by 2 raised to 8 which is 256
    l1 = l1 * 256
    str = Hex(l1)

    l1 = l1 Or l2

    str = Hex(l1)
    'l3 will give you the lower 4 bits
    l3 = l1 And &HF
    str = Hex(l3)

    'l4 will give you the upper 12 bits
    l4 = l1 And &HFFF0
    str = Hex(l4)

    l4 = CLng(l4 / 16)

    str = Hex(l4)

    'if you didn't know, "or" is used to set bits to 1
    ' and "and" is used to set bits to 0 ( resetting)






  3. #3
    Guest

    Re: Combining And Splitting Bytes

    Your declarations for these variables is wrong:
    Dim b1, b2 as Byte
    This is proper:
    Dim b1 as Byte, b2 as Byte <--- Need to specify type for each variable, otherwise only the last in the line will be the type, the others will be variants.

    That's all. (Not to be a tight *** or anything...


  4. #4
    Guest

    Re: Combining And Splitting Bytes

    Here's the goods:


    Sub ByteParse(In1 as Byte, In2 as Byte, Out12 as Integer, Out4 as Integer)
    Dim lngBase as Long
    Dim Temp1 as Long, Temp2 as Long

    Temp1 = In1
    Temp2 = In2
    lngBase = (Temp1 * 256) Or Temp2 'Combine Bytes
    Out12 = (lngBase And 65520) / 16 'First 12 bits
    Out4 = lngBase And 15 'Last 4 bits
    End Sub




    BrewGuru99


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