Results 1 to 6 of 6

Thread: OCtets to Bytes

  1. #1
    appi101
    Guest

    OCtets to Bytes

    HI

    How do I convert octets to bytes

    Say I have 853 octets how do I convert them to bytes or rather to the decimal system

  2. #2
    Registered User Nucleus's Avatar
    Join Date
    Apr 2001
    Location
    So that's what you are up to ;)
    Posts
    2,530
    Here you go, convert from any base back to decimal:

    Conversion base 2- base 62 as 0-9, [A-Z], [a-z] is 10 + 26 +26 = max base 62.
    Code:
    Function RadixToDec62(ByVal sRadix$, Radix%) As Variant
     'Nucleus
     Dim ba() As Byte, i&, bitval As Variant, b As Byte
     bitval = 1
     ba = StrConv(sRadix, vbFromUnicode)
     For i = UBound(ba) To 0 Step -1
        b = ba(i)
        If b < 58 Then
            RadixToDec62 = CDec(RadixToDec62 + (b - 48) * bitval)
        ElseIf b < 91 Then RadixToDec62 = CDec(RadixToDec62 + (b - 55) * bitval)
        ElseIf b < 123 Then RadixToDec62 = CDec(RadixToDec62 + (b - 61) * bitval)
        End If
        bitval = CDec(bitval * Radix)
     Next i
    End Function

  3. #3
    appi101
    Guest
    Hi

    Thanks for the code. It works great. Could you explain how it works

  4. #4
    Registered User Nucleus's Avatar
    Join Date
    Apr 2001
    Location
    So that's what you are up to ;)
    Posts
    2,530
    It checks each bit from right to left and works out it's numerical value in decimal, for example A in hex is 10 decimal. Then it simply multiplies this number by the bit value.

    For example:
    2A in hex

    A = 10 decimal * Bitvalue = 10* 16^0 = 10
    2 = 2 decimal * bitvalue = 2 * 16^1 = 32

    so the answer 2A hex = 42 decimal

    What about Octet?
    67 Octet

    7 = 7 * 8^0 = 7
    6 = 6 * 8^1 = 48

    so the answer is 55 decimal

  5. #5
    WALDO
    Guest

    Thumbs up There is a much easier way to do that



    Try this:
    VB Code:
    1. '67 in octal, 37 in hexadecimal
    2. MsgBox("67 in Octal is " & CInt([b]&O[/b]67) & " in decimal." & vbCrLf & _
    3.     "37 in Hexadecimal is " & CInt([b]&H[/b]37) & " in decimal.")

    It should return 55
    The &O denotes an Octal Value
    The &H denotes a Hexadecimal Value

  6. #6
    Registered User Nucleus's Avatar
    Join Date
    Apr 2001
    Location
    So that's what you are up to ;)
    Posts
    2,530
    True WALDO, but how much fun is that?

    Also VB will only convert small octet numbers, where as RadixToDec62 will convert up to 29 digits, like this for e.g., which comes in very handy sometimes . Also the function not only converts octets to binary, but any base 2-62 back to binary, so is much more flexible.

    Code:
    Private Sub Command1_Click()
     Dim s As String
    
     s = "67865432345676543234565432345"
     MsgBox RadixToDec62(s, 8)
     MsgBox CLng("&O" & s) ' VB cannot handle numbers this large, it can only handle a max of 10 digits which is pretty poor In my opinion.
    
    
    End Sub

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