Solved

VBScript: ASCII to Binary

Posted on 2004-08-13
4
2,013 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
ID: 11793855
IS this what you are looking to do?

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

Expert Comment

by:Rodney Helsens
ID: 11793940
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
ID: 11793988
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
ID: 11794003
... and maybe a better function name would be "charToBinary" ....
0

Featured Post

Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Hello, all! I just recently started using Microsoft's IIS 7.5 within Windows 7, as I just downloaded and installed the 90 day trial of Windows 7. (Got to love Microsoft for allowing 90 days) The main reason for downloading and testing Windows 7 is t…
I would like to start this tip/trick by saying Thank You, to all who said that this could not be done, as it forced me to make sure that it could be accomplished. :) To start, I want to make sure everyone understands the importance of utilizing p…
In a recent question (https://www.experts-exchange.com/questions/29004105/Run-AutoHotkey-script-directly-from-Notepad.html) here at Experts Exchange, a member asked how to run an AutoHotkey script (.AHK) directly from Notepad++ (aka NPP). This video…

820 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