little Binary Help

Discussion in 'AutoCAD' started by jane, Feb 17, 2005.

  1. jane

    jane Guest

    Hi to all,
    I can do some lisp, but not when it comes to binary.
    Would someone be kind enough to make me a lisp that
    could transpose binary digits into an ascii letter?
    This is how I would like it......
    If for arguements sake I had a binary notation like this:
    " 00101010 " of which equals T I would like to be able
    to offer the notion to the lisp and have its resulting ascii equivilent
    shown to screen (or just returned ).

    Is this possible?

    thank you

    regards Jane
     
    jane, Feb 17, 2005
    #1
  2. jane

    Jürg Menzi Guest

    Hi Jane

    This function shold do what you want:
    Code:
    ;
    ; -- Function MeBinary2Dec
    ; Convert Binary to a decimal number.
    ; Arguments [Type]:
    ;   Bin = Binary data, eg. 11101 [STR]
    ;   Msd = Most significant digit [SYM] *)
    ; Return [Type]:
    ;   > Decimal number [INT]
    ; Notes:
    ;   *) If true first digit is MSD, else last digit
    ;
    (defun MeBinary2Dec (Bin Msd / CurBit ExpCnt RetVal StrCnt)
    (setq StrCnt (if Msd (strlen Bin) 1)
    ExpCnt 0
    RetVal 0
    )
    (repeat (strlen Bin)
    (setq CurBit (atoi (substr Bin StrCnt 1))
    RetVal (+ (* CurBit (expt 2 ExpCnt)) RetVal)
    StrCnt (if Msd (1- StrCnt) (1+ StrCnt))
    ExpCnt (1+ ExpCnt)
    )
    )
    RetVal
    )
    
    Sample:
    (chr (MeBinary2Dec "00101010" nil))
    "T"

    Cheers
     
    Jürg Menzi, Feb 17, 2005
    #2
  3. jane

    jane Guest

    Wow, thanks for being so quick Jurg.

    Um, I'm having a problem with it though, it seems you have
    two globals and I dont quite understand the second.
    I have just offered the lisp to autocad and this is what I got back:
    **AutoCAD menu utilities loaded.
    AutoCAD Express Tools Menu loaded.
    Command: (load "bin")
    ; error: no function definition: MEBINARY2DEC

    Command: (mebinary2dec 00101010)
    ; error: no function definition: MEBINARY2DEC

    Command: (mebinary2dec 01010100)
    ; error: no function definition: MEBINARY2DEC**

    I even tried it by reversing the binary notation for the letter T in
    case it wasnt reading from right to left, (or is that left to right?)

    Is there a lisp where I can just put in the binary alone to receive
    the ascii equivalent?

    regards Jane
     
    jane, Feb 17, 2005
    #3
  4. jane

    Jürg Menzi Guest

    Oops, was a bit to theoretically...

    Doesn't affect the result but it's more elegant if you change the line:
    RetVal (+ (* CurBit (expt 2 ExpCnt)) RetVal)
    to
    RetVal (+ (lsh CurBit ExpCnt) RetVal)

    Cheers
     
    Jürg Menzi, Feb 17, 2005
    #4
  5. jane

    jane Guest

    Hmmm, now I am lost.
    I had a hunch making binary to ascii would be hard, I just
    didnt want to have to transpose it the old fashioned way by
    doing it manually counting off.

    regards Jane
     
    jane, Feb 17, 2005
    #5
  6. jane

    Jürg Menzi Guest

    Hi Jane

    Is 'Bin' the file name where you saved the function (bin.lsp)?
    The function requires 2 arguments:
    - Binary value as string, eg. "00101010"
    - MSD (most significant digit) as symbol, eg, T or nil
    T=MSD is right, nil=MSD is left
    Samples:
    (MeBinary2Dec "00101010" nil) returns 84
    (MeBinary2Dec "00101010" T) returns 42
    With the lisp function 'chr' you can convert the decimal number to a
    character string.

    Cheers
     
    Jürg Menzi, Feb 17, 2005
    #6
  7. jane

    jane Guest

    YES, Excellent..!
    Thank you.... it works just fine
    Also, thank you for a promptness that I certainly
    didnt expect.

    regards Jane.
     
    jane, Feb 17, 2005
    #7
  8. jane

    jane Guest

    Hi Jurg,,,,,,,,,
    I thought I had already sent a post saying::::
    YES YES YES, that is Exactly what I wanted, but I cant
    see it in my Sent Folder and it hasnt shown up here.
    I sent it 30seconds after you sent the Attachment.......
    So, in case it is out there somewhere in the ether, I will proffer my
    elation again.............. Thank you Thank you Thank you.
    It works just fine.

    regards Jane
     
    jane, Feb 17, 2005
    #8
  9. jane

    Jürg Menzi Guest

    Hi Jane

    Happy to help where I can. You're welcome...¦-)

    Cheers
     
    Jürg Menzi, Feb 17, 2005
    #9
  10. jane

    jane Guest

    You is one clever cookie,,,,,
    thanks jurg

    regards Jane
     
    jane, Feb 17, 2005
    #10
Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments (here). After that, you can post your question and our members will help you out.