Here is the source of the page at
http://techref.massmind.org/method/math/bindecbits.asp
which sort of does this in VBScript
Decimal | Binary |
<%
aryBin = split("000,001,010,011,100,101,110,111", ",")
intI = 60000
intD = 10000
while intI > 9
strOct = Oct(intI)
response.write "
---|
" & intI & " | "
for intJ = 1 to len(strOct)
response.write aryBin(cint(mid(strOct,intJ,1)))
next
strOct = Oct(intI-1)
response.write " |
" & intI - 1 & " | "
for intJ = 1 to len(strOct)
response.write aryBin(cint(mid(strOct,intJ,1)))
next
if intI > intD then
else
intD = intD / 10
end if
intI = intI - intD
wend
%>
|
---
James Newton mailto:jamesnewton@geocities.com 1-619-652-0593
http://techref.massmind.org NEW! FINALLY A REAL NAME!
Members can add private/public comments/pages ($0 TANSTAAFL web hosting)
-----Original Message-----
From: pic microcontroller discussion list
[mailto:PICLIST@MITVMA.MIT.EDU]On Behalf Of Mike Mansheim
Sent: Wednesday, March 22, 2000 13:53
To: PICLIST@MITVMA.MIT.EDU
Subject: Re: [OT] Help Hex->bin conversion (pic=easy, VB=??)
to solve it , It will sound stupid but, I don't know how to convert
a 8 bit (2 ascii Char , Hexadecimal value) to Binary (8 Char ascii bin)
Ej: If a Have in a X variable the "F0" value, I need to get "11110000"
How can i do it under VB?, it is so easy in ASM that I cannot believe that
I can't do it in VB.
I'm sure that more elegant ways to do this abound, but this works and it
shows the
basics of base conversion.
Take each digit in X and convert it to a 4 bit string (F = '1111', 0
= '0000'),
then put them together to make the 8 bit string.
First, X will be a string - access each character in the string with the
mid$ string
function:
binary_string = ""
For j = 1 To Len(X) 'this watches for single character; should
also trap
hex_char = mid$(X,j,1) ' for lengths > 2
Then hex_char needs to be converted to a number, using something like:
number = Asc(hex_char) - 48 'Asc converts char to
ascii number
if (number > 9) Then number = number - 7 'upper case
letters
if (number > 15) Then number = number - 32 'lower case letters
Then run each resulting number through a base conversion routine to build
the 4 bit string:
For i = 3 To 0 Step -1
divisor = 2 ^ i
digit = Int(number / divisor)
number = number - (digit * divisor)
' "&" is string concatenation; Chr is the inverse of Asc
binary_string = binary_string & Chr(digit + 48)
Next i
Next j
'pad with leading '0000' if single digit input
if (Len(X) = 1) Then binary_string = "0000" & binary_string
Hope this helps!