Author Topic: ( Challenge ) StringToBinary  (Read 10560 times)

0 Members and 1 Guest are viewing this topic.

MP

  • Seagull
  • Posts: 17750
  • Have thousands of dwgs to process? Contact me.
( Challenge ) StringToBinary
« on: March 20, 2009, 05:49:19 PM »
Convert a string to a list of binary strings, each binary string representing the value of the character's ASCII code, padded to 8 bit width.

e.g. (StringToBinary "ABC") => ("01000001" "01000010" "01000011")

Easy stuff, use the language of your choice. :)
Engineering Technologist • CAD Automation Practitioner
Automation ▸ Design ▸ Drafting ▸ Document Control ▸ Client
cadanalyst@gmail.comhttp://cadanalyst.slack.comhttp://linkedin.com/in/cadanalyst

MP

  • Seagull
  • Posts: 17750
  • Have thousands of dwgs to process? Contact me.
Re: ( Challenge ) StringToBinary
« Reply #1 on: March 20, 2009, 05:59:08 PM »
Here's my LISP code:

Code: [Select]
(defun _ByteToBin ( byte / power result )

    ;;  convert an ascii code to a binary equivalent.
    ;;
    ;;  e.g. (_ByteToBin 42) returns "00101010"
   
    (repeat (setq power 8)
        (setq result
            (cons
                (if
                    (zerop
                        (logand byte
                            (expt 2
                                (setq power
                                    (1- power)
                                )
                            )
                        )
                    )       
                    0
                    1
                )
                result
            )
        )               
    )
   
    (apply 'strcat (mapcar 'itoa (reverse result)))

)

(defun _StrToBin ( str )

    (mapcar '_ByteToBin (vl-string->list str))

)

But holy <!> look at my Python equiv:

Code: [Select]
def strToBin(s):
    def foo(i): return (('0'*8)+bin(i)[2:])[-8:]
    return list(map(foo,map(ord,s)))

:-o  :-D
Engineering Technologist • CAD Automation Practitioner
Automation ▸ Design ▸ Drafting ▸ Document Control ▸ Client
cadanalyst@gmail.comhttp://cadanalyst.slack.comhttp://linkedin.com/in/cadanalyst

gile

  • Gator
  • Posts: 2507
  • Marseille, France
Re: ( Challenge ) StringToBinary
« Reply #2 on: March 20, 2009, 06:05:16 PM »
Hi,

Code: [Select]
(defun StringToBinary (str)
  (mapcar
    '(lambda (x / n s)
       (setq n 0
     s (itoa (rem x 2))
       )
       (repeat 7
(setq n (1+ n))
(if (/= 0 (logand x (expt 2 n)))
   (setq s (strcat "1" s))
   (setq s (strcat "0" s))
)
       )
     )
    (vl-string->list str)
  )
)

Speaking English as a French Frog

MP

  • Seagull
  • Posts: 17750
  • Have thousands of dwgs to process? Contact me.
Re: ( Challenge ) StringToBinary
« Reply #3 on: March 20, 2009, 06:14:10 PM »
Well done Gile! :)
Engineering Technologist • CAD Automation Practitioner
Automation ▸ Design ▸ Drafting ▸ Document Control ▸ Client
cadanalyst@gmail.comhttp://cadanalyst.slack.comhttp://linkedin.com/in/cadanalyst

T.Willey

  • Needs a day job
  • Posts: 5251
Re: ( Challenge ) StringToBinary
« Reply #4 on: March 20, 2009, 06:19:40 PM »
Here is my cheesy entry.

Code: [Select]
(defun StringToBinary ( str )
   
    (mapcar
        '(lambda ( x / cnt str tempX)
            (setq cnt 128)
            (setq str "")
            (while (not (zerop cnt))
                (setq tempX (rem x cnt))
                (setq str
                    (strcat
                        str
                        (if (equal tempX x)
                            "0"
                            "1"
                        )
                    )
                )
                (setq x tempX)
                (setq cnt (/ cnt 2))
            )
            str
        )
        (vl-string->list str)
    )
)
Tim

I don't want to ' end-up ', I want to ' become '. - Me

Please think about donating if this post helped you.

MP

  • Seagull
  • Posts: 17750
  • Have thousands of dwgs to process? Contact me.
Re: ( Challenge ) StringToBinary
« Reply #5 on: March 20, 2009, 06:25:27 PM »
Well done Tim. :)
Engineering Technologist • CAD Automation Practitioner
Automation ▸ Design ▸ Drafting ▸ Document Control ▸ Client
cadanalyst@gmail.comhttp://cadanalyst.slack.comhttp://linkedin.com/in/cadanalyst

VovKa

  • Water Moccasin
  • Posts: 1628
  • Ukraine
Re: ( Challenge ) StringToBinary
« Reply #6 on: March 20, 2009, 07:07:34 PM »
Code: [Select]
(defun vk_Dec2Bin (Dec)
  (if (< (setq Dec (fix Dec)) 2)
    (itoa Dec)
    (strcat (vk_Dec2Bin (/ Dec 2)) (itoa (rem Dec 2)))
  )
)

(mapcar 'vk_Dec2Bin (vl-string->list "ABC"))

not padded to a byte length :(
« Last Edit: March 20, 2009, 07:20:46 PM by VovKa »

ronjonp

  • Needs a day job
  • Posts: 7527
Re: ( Challenge ) StringToBinary
« Reply #7 on: March 20, 2009, 07:17:29 PM »
Nerds  :-P  .... and it's awesome :)

Windows 11 x64 - AutoCAD /C3D 2023

Custom Build PC

MP

  • Seagull
  • Posts: 17750
  • Have thousands of dwgs to process? Contact me.
Re: ( Challenge ) StringToBinary
« Reply #8 on: March 20, 2009, 07:23:25 PM »
Nice work VovKa but ... it doesn't satisfy the requirement to pad all binary strings to an 8 bit width.

i.e. "A" should be "01000001", not "1000001".
Engineering Technologist • CAD Automation Practitioner
Automation ▸ Design ▸ Drafting ▸ Document Control ▸ Client
cadanalyst@gmail.comhttp://cadanalyst.slack.comhttp://linkedin.com/in/cadanalyst

VovKa

  • Water Moccasin
  • Posts: 1628
  • Ukraine
Re: ( Challenge ) StringToBinary
« Reply #9 on: March 20, 2009, 07:29:22 PM »
thanks MP, i was too exited to read till the end :)
Code: [Select]
(defun vk_Dec2Bin (Dec Len)
  (if (< Len 2)
    (itoa (rem Dec 2))
    (strcat (vk_Dec2Bin (/ Dec 2) (1- Len)) (itoa (rem Dec 2)))
  )
)
(mapcar '(lambda (a) (vk_Dec2Bin a 8)) (vl-string->list "ABC"))

MP

  • Seagull
  • Posts: 17750
  • Have thousands of dwgs to process? Contact me.
Re: ( Challenge ) StringToBinary
« Reply #10 on: March 20, 2009, 07:30:37 PM »
Outstanding, well played. :)
Engineering Technologist • CAD Automation Practitioner
Automation ▸ Design ▸ Drafting ▸ Document Control ▸ Client
cadanalyst@gmail.comhttp://cadanalyst.slack.comhttp://linkedin.com/in/cadanalyst

gile

  • Gator
  • Posts: 2507
  • Marseille, France
Re: ( Challenge ) StringToBinary
« Reply #11 on: March 20, 2009, 08:32:59 PM »
Very nice Vovka !

Dirty C#

Code: [Select]
static string[] StringToBinary(string str)
{
    Encoding ascii = Encoding.ASCII;
    Byte[] bytes = ascii.GetBytes(str);
    string[]result = new string[bytes.Length];
    for (int i = 0; i < bytes.Length; i++)
    {
        string bin = "";
        for (int expt = 7; expt >= 0; expt--)
        {
            bin += (bytes[i] & (int)Math.Pow(2, expt)) == 0 ? "0" : "1";
        }
        result[i] = bin;
    }
    return result;
}
Speaking English as a French Frog

MP

  • Seagull
  • Posts: 17750
  • Have thousands of dwgs to process? Contact me.
Re: ( Challenge ) StringToBinary
« Reply #12 on: March 20, 2009, 08:52:37 PM »
Nice Gile, including the use of the ternary operator. :)
Engineering Technologist • CAD Automation Practitioner
Automation ▸ Design ▸ Drafting ▸ Document Control ▸ Client
cadanalyst@gmail.comhttp://cadanalyst.slack.comhttp://linkedin.com/in/cadanalyst

SomeCallMeDave

  • Guest
Re: ( Challenge ) StringToBinary
« Reply #13 on: March 20, 2009, 09:48:46 PM »
I'm loving Ruby more every day

Code: [Select]
"ABC".each_byte {|x| puts x.to_s(2).rjust(8,"0")}

Returns
Code: [Select]
01000001
01000010
01000011

MP

  • Seagull
  • Posts: 17750
  • Have thousands of dwgs to process? Contact me.
Re: ( Challenge ) StringToBinary
« Reply #14 on: March 20, 2009, 09:56:28 PM »
That's pretty cool, tho you didn't pen it as a function.

Python equivalent:

list(map(lambda a: (('0'*8)+bin(ord(a))[2:])[-8:],"ABC"))

=> ['01000001', '01000010', '01000011']

:P

I'm sure someone like Tim could do it more elegantly | concisely.
Engineering Technologist • CAD Automation Practitioner
Automation ▸ Design ▸ Drafting ▸ Document Control ▸ Client
cadanalyst@gmail.comhttp://cadanalyst.slack.comhttp://linkedin.com/in/cadanalyst