Solved

VBScript: ASCII to Binary

Posted on 2004-08-13
4
1,955 Views
Last Modified: 2008-12-04
I'm searching for a function to convert an ascii character to binary.

For example, I have this character: @

I want to convert it to: 01000000

does anyone know where I can find code to do this? It has to be regular VB script, I can't load any kind of components or use any .net stuff to do this.
0
Comment
Question by:WebArchitect_Vince
  • 2
  • 2
4 Comments
 
LVL 9

Expert Comment

by:Rodney Helsens
Comment Utility
IS this what you are looking to do?

http://www.motobit.com/tips/detpg_BinASP.htm
0
 
LVL 9

Expert Comment

by:Rodney Helsens
Comment Utility
This seems to work too..

<%

Function cBIN(iC)
cBIN = ""
X = 256
For Y = 1 To 8
     bC = 0
     X = X / 2
     If iC >= X Then
     bC = 1
     iC = iC - X
     End If
   cBIN = cBIN & bC
Next
End Function

Response.Write ( cBIN(Asc("@")) )

%>
0
 
LVL 15

Accepted Solution

by:
deighc earned 500 total points
Comment Utility
Try this. You need to pass both the character and the length of the string you want returned (8 chars in your example:

decVal = DecToBin("@", 8)

Function DecToBin(strChar, len)
  dim strResult
  dim intValue
  dim intExp
  dim intDec
 
  intDec = Asc(strChar)
  strResult = ""

  intValue = intDEC
  intExp = 2 ^ (len - 1)
  while intExp >= 1
    if intValue >= intExp then
      intValue = intValue - intExp
      strResult = strResult & "1"
    else
      strResult = strResult & "0"
    end if
    intExp = intExp / 2
  wend

  DecToBin = strResult
End Function
0
 
LVL 15

Expert Comment

by:deighc
Comment Utility
... and maybe a better function name would be "charToBinary" ....
0

Featured Post

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

I was asked about the differences between classic ASP and ASP.NET, so let me put them down here, for reference: Let's make the introductions... Classic ASP was launched by Microsoft in 1998 and dynamically generate web pages upon user interact…
This demonstration started out as a follow up to some recently posted questions on the subject of logging in: http://www.experts-exchange.com/Programming/Languages/Scripting/JavaScript/Q_28634665.html and http://www.experts-exchange.com/Programming/…
Internet Business Fax to Email Made Easy - With eFax Corporate (http://www.enterprise.efax.com), you'll receive a dedicated online fax number, which is used the same way as a typical analog fax number. You'll receive secure faxes in your email, fr…
Polish reports in Access so they look terrific. Take yourself to another level. Equations, Back Color, Alternate Back Color. Write easy VBA Code. Tighten space to use less pages. Launch report from a menu, considering criteria only when it is filled…

762 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

10 Experts available now in Live!

Get 1:1 Help Now