Hi all

I am looking for a function similar to:

<% response.write(Hex(460)) %>

converts 469 into its HEX of 1CC

but to do the same with binary

i.e. 5 would give 101

looked through the W3C VBScript lists but nothing

I can do it in javascript:

<script language="javascript" type="text/javascript">
function decimal(dec)
{
this.dec=dec;
this.toBinary=function() { return this.dec.toString(2); }
}


</script>

then document.write(new decimal(5).toBinary());

but need to be able to do it with VBscript to use with my ASP

thanks for any help

Matt Houldsworth
Digitalquill

Re: Number to binary by Björn

Björn
Wed Jun 22 04:37:01 CDT 2005

"Hoggman" <thequill78@yahoo.co.uk> wrote in message
news:GqicnU_fD7DwhSTfSa8jmw@karoo.co.uk...
> Hi all
>
> I am looking for a function similar to:
>
> <% response.write(Hex(460)) %>
>
> converts 469 into its HEX of 1CC
>
> but to do the same with binary
>
> i.e. 5 would give 101
>
> looked through the W3C VBScript lists but nothing
>
> I can do it in javascript:
>
> <script language="javascript" type="text/javascript">
> function decimal(dec)
> {
> this.dec=dec;
> this.toBinary=function() { return this.dec.toString(2); }
> }
>
>
> </script>
>
> then document.write(new decimal(5).toBinary());
>
> but need to be able to do it with VBscript to use with my ASP
>
> thanks for any help
>
> Matt Houldsworth
> Digitalquill

Nothing builtin for this AFAIK, but here's a very simple function to
accomplish what you want:

Function toBinary(ByVal Dec)
Dim Bin, Pos, i, Bit
Pos = Int(Log(Dec) / Log(2))
For i = Pos To 0 Step -1
Bit = Dec\2^i
Bin = Bin & CStr(Bit)
Dec = Dec-Bit*2^i
Next
toBinary = Bin
End Function


--
Björn Holmgren




Re: Number to binary by Joe

Joe
Wed Jun 22 04:01:35 CDT 2005

Hi,

"Hoggman" <thequill78@yahoo.co.uk> wrote in message
news:GqicnU_fD7DwhSTfSa8jmw@karoo.co.uk...
> Hi all
>
> I am looking for a function similar to:
>
> <% response.write(Hex(460)) %>
>
> converts 469 into its HEX of 1CC
>
> but to do the same with binary
>
> i.e. 5 would give 101
>
> looked through the W3C VBScript lists but nothing
>
> I can do it in javascript:
>
> <script language="javascript" type="text/javascript">
> function decimal(dec)
> {
> this.dec=dec;
> this.toBinary=function() { return this.dec.toString(2); }
> }
>
>
> </script>
>
> then document.write(new decimal(5).toBinary());
>
> but need to be able to do it with VBscript to use with my ASP
>
> thanks for any help
>
> Matt Houldsworth
> Digitalquill
>
>

An interesting exercise. I think that this should work --

-----
for i= 1 to 16
wscript.echo toBinary(i)
next

function ToBinary (byVal viInt)
' coerces viInt to positive long integer
' returns binary string

on error resume next
viInt= clng(viInt): if err then viInt= 0
on error goto 0

if (viInt<1) then toBinary= "0": exit function

dim viMaxPwr: viMaxPwr= cint(log(viInt)/log(2)) +1

dim viBinPwr, viBinVal: for viBinPwr= 1 to viMaxPwr
viBinVal= viInt mod 2^viBinPwr: viInt= viInt -viBinVal

if (cbool(viBinVal) or (viBinPwr<viMaxPwr)) then
ToBinary= sgn(viBinVal) & ToBinary
end if
next
end function
-----

Joe Earnest



Re: Number to binary by Joe

Joe
Wed Jun 22 04:19:02 CDT 2005

Hi,

Hadn't see Bjorn's post when I posted mine. His routine is slightly
shorter.

Joe Earnest



Re: Number to binary by James

James
Wed Jun 22 07:46:06 CDT 2005


"Hoggman" <thequill78@yahoo.co.uk> wrote in message
news:GqicnU_fD7DwhSTfSa8jmw@karoo.co.uk...
> Hi all
>
> I am looking for a function similar to:
> <% response.write(Hex(460)) %>
> converts 469 into its HEX of 1CC
> but to do the same with binary
> i.e. 5 would give 101
> looked through the W3C VBScript lists but nothing
> I can do it in javascript:
>
> <script language="javascript" type="text/javascript">
> function decimal(dec)
> {
> this.dec=dec;
> this.toBinary=function() { return this.dec.toString(2); }
> }
> </script>
>
> then document.write(new decimal(5).toBinary());
> but need to be able to do it with VBscript to use with my ASP
> thanks for any help
>
> Matt Houldsworth
> Digitalquill

Give this a try:

Function Dec2Bin(Dec)
Do Until Dec < 1
Dec2Bin = Dec Mod 2 & Dec2Bin
Dec = Dec \ 2
Loop
End Function