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
Printable View
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
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
Hi
Thanks for the code. It works great. Could you explain how it works
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
:)
Try this:
VB Code:
'67 in octal, 37 in hexadecimal MsgBox("67 in Octal is " & CInt([b]&O[/b]67) & " in decimal." & vbCrLf & _ "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
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