Jump to content

UDF: convert a character string to a binary one and back


Pakku
 Share

Recommended Posts

The function doesn't do his job, it will be updated as soon as possible

Hi all,

i wrote a little udf which allows us to convert a character string to a binary string and the other way around.

i hope i can make some of you happy with this!

;===============================================================================
;
; Description:    Converts a character string to a binary string
; Parameter(s):  $_string - The character string to use
; Requirement(s):   None
; Return Value(s):  On Success - The binary string
;                  On Failure - It just can't fail
; Author(s):        Pakku
; Note(s):
;
;===============================================================================
Func _String2Binary($_string)
    $_string_split = StringSplit($_string, "")
    $output = ""
    For $a = 1 to $_string_split[0]
        $asc = Asc($_string_split[$a])
        $mod = ""
        For $b = 1 to 8
            $mod = Mod($asc, 2) & $mod
            $asc = Floor($asc/2)
        Next
        $output &= $mod
    Next
    Return $output
EndFunc

;===============================================================================
;
; Description:    Converts a binary string to a character string
; Parameter(s):  $_string - The binary string to use
; Requirement(s):   None
; Return Value(s):  On Success - The character string
;                  On Failure - It just can't fail
; Author(s):        Pakku
; Note(s):
;
;===============================================================================
Func _Binary2String($_string)
    $_string_split = StringSplit(StringRegExpReplace($_string, "[^:xdigit:]", ""), "")
    $_output = ""
    For $a = 1 to $_string_split[0] Step 8
        $_temp = ""
        For $b = 0 To 7
            $_temp &= $_string_split[$a+$b]
        Next
        $_chr = 0
        $_temp = StringSplit($_temp, "")
        For $c = 1 to $_temp[0]
            $_chr *= 2
            $_chr += Number($_temp[$c])
        Next
        $_output &= Chr($_chr)
    Next
    Return $_output
EndFunc

Any response, questions, modification or what ever is welcome!

Edited by Pakku
Link to comment
Share on other sites

  • Moderators

Any response, questions, modification or what ever is welcome!

Arjan

Seems to be alot like nfwu's

http://www.autoitscript.com/forum/index.ph...mp;#entry211599

Edited by SmOke_N

Common sense plays a role in the basics of understanding AutoIt... If you're lacking in that, do us all a favor, and step away from the computer.

Link to comment
Share on other sites

yes you are right, but mine is shorter

Edited by Pakku
Link to comment
Share on other sites

yes you are right, but mine is shorter

Arjan

and appears wrong. Tested your function w/the following test

$var = "simple test"

$ret = _String2Binary($var)

ConsoleWrite("$ret - "&$ret&@CRLF)

$msg = _Binary2String($ret)

ConsoleWrite("$msg - "&$msg&@CRLF)

-------Output-------

$ret - 0111001101101001011100110110110101101001011100110111000001101101011010010111001101101100011100000110

1101011010010111001101100101011011000111000001101101011010010111001100100000011001010110110001110000

0110110101101001011100110111010000100000011001010110110001110000011011010110100101110011011001010111

0100001000000110010101101100011100000110110101101001011100110111001101100101011101000010000001100101

0110110001110000011011010110100101110011011101000111001101100101011101000010000001100101011011000111

0000011011010110100101110011

$msg - ssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssss

Link to comment
Share on other sites

i see, you are right.

i will update it as soon as possible

thanks

See first post

Edited by Pakku
Link to comment
Share on other sites

  • 1 year later...
  • Moderators

still not working (_Binary2String that is)

Just returns an empty string :|

You're kidding right???? You brought up a 2 year old thread to say... "Still not working"... well no shit.

Common sense plays a role in the basics of understanding AutoIt... If you're lacking in that, do us all a favor, and step away from the computer.

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...