Jump to content

Binary Number to String and Vice versa


Recommended Posts

Hello all

So i've written this UDF to convert a string to a Number Binary sequence and vice versa:

Func _binarynumbertostring($sBinarynumbertoconvert)
    $numberofcharacters = StringLen($sBinarynumbertoconvert)
    $numberofstringcharacters = $numberofcharacters / 8
    if IsInt($numberofstringcharacters) Then
        sleep(50)
    Else
        MsgBox(0, "Error", "Incorrect Number of Digits in Binary Number")
    EndIf

    For $i = 1 to $numberofstringcharacters
        $leftcharacter = StringLeft($sBinarynumbertoconvert, 8)
        $stringchr = _binarynumbervalues($leftcharacter)
        $opentempfile = Fileopen(@TempDir & "\stringfrombinary.txt", 9)
        FileWrite($opentempfile, $stringchr)
        FileClose($opentempfile)
        $sBinarynumbertoconvert = StringTrimLeft($sBinarynumbertoconvert, 8)
    Next
    $opentempfile2 = Fileopen(@TempDir & "\stringfrombinary.txt")
    $filecontents = FileRead($opentempfile2)
    FileClose($opentempfile2)
    FileDelete(@TempDir & "\stringfrombinary.txt")
    Return $filecontents
EndFunc   ;==>_binarynumbertostring

Func _StringtoNumberBinary($sBinarynumbertoconvert1)
    $numberofcharacters1 = StringLen($sBinarynumbertoconvert1)
    if IsInt($numberofcharacters1) Then
        sleep(50)
    Else
        MsgBox(0, "Error", "Incorrect Number of Digits in Binary Number")
    EndIf

    For $i1 = 1 to $numberofcharacters1
        $leftcharacter1 = StringLeft($sBinarynumbertoconvert1, 1)
        $stringchr1 = _stringvalues1($leftcharacter1)
        $opentempfile1 = Fileopen(@TempDir & "\stringfrombinary.txt", 9)
        FileWrite($opentempfile1, $stringchr1)
        FileClose($opentempfile1)
        $sBinarynumbertoconvert1 = StringTrimLeft($sBinarynumbertoconvert1, 1)
    Next
    $opentempfile21 = Fileopen(@TempDir & "\stringfrombinary.txt")
    $filecontents1 = FileRead($opentempfile21)
    FileClose($opentempfile21)
    FileDelete(@TempDir & "\stringfrombinary.txt")
    Return $filecontents1
EndFunc   ;==>_StringtoNumberBinary



Func _binarynumbervalues($binarynoval)

    If $binarynoval = "00100000" Then
        $binarynoval = Chr(32)
    EndIf

    If $binarynoval = "00110000" Then
        $binarynoval = Chr(48)
    EndIf

    If $binarynoval = "00110001" Then
        $binarynoval = Chr(49)
    EndIf

    If $binarynoval = "00110010" Then
        $binarynoval = Chr(50) 
    EndIf

    If $binarynoval = "00110011" Then
        $binarynoval = Chr(51) 
    EndIf

    If $binarynoval = "00110100" Then
        $binarynoval = Chr(52) 
    EndIf

    If $binarynoval = "00110101" Then
        $binarynoval = Chr(53) 
    EndIf

    If $binarynoval = "00110110" Then
        $binarynoval = Chr(54) 
    EndIf

    If $binarynoval = "00110111" Then
        $binarynoval = Chr(55) 
    EndIf

    If $binarynoval = "00111000" Then
        $binarynoval = Chr(56) 
    EndIf

    If $binarynoval = "00111001" Then
        $binarynoval = Chr(57) 
    EndIf

    If $binarynoval = "01000001" Then
        $binarynoval = Chr(65) 
    EndIf

    If $binarynoval = "01000010" Then
        $binarynoval = Chr(66) 
    EndIf

    If $binarynoval = "01000011" Then
        $binarynoval = Chr(67) 
    EndIf

    If $binarynoval = "01000100" Then
        $binarynoval = Chr(68) 
    EndIf

    If $binarynoval = "01000101" Then
        $binarynoval = Chr(69) 
    EndIf

    If $binarynoval = "01000110" Then
        $binarynoval = Chr(70) 
    EndIf

    If $binarynoval = "01000111" Then
        $binarynoval = Chr(71) 
    EndIf

    If $binarynoval = "01001000" Then
        $binarynoval = Chr(72) 
    EndIf

    If $binarynoval = "01001001" Then
        $binarynoval = Chr(73) 
    EndIf

    If $binarynoval = "01001010" Then
        $binarynoval = Chr(74) 
    EndIf

    If $binarynoval = "01001011" Then
        $binarynoval = Chr(75) 
    EndIf

    If $binarynoval = "01001100" Then
        $binarynoval = Chr(76) 
    EndIf

    If $binarynoval = "01001101" Then
        $binarynoval = Chr(77) 
    EndIf

    If $binarynoval = "01001110" Then
        $binarynoval = Chr(78) 
    EndIf

    If $binarynoval = "01001111" Then
        $binarynoval = Chr(79) 
    EndIf

    If $binarynoval = "01010000" Then
        $binarynoval = Chr(80) 
    EndIf

    If $binarynoval = "01010001" Then
        $binarynoval = Chr(81) 
    EndIf

    If $binarynoval = "01010010" Then
        $binarynoval = Chr(82) 
    EndIf

    If $binarynoval = "01010011" Then
        $binarynoval = Chr(83) 
    EndIf

    If $binarynoval = "01010100" Then
        $binarynoval = Chr(84) 
    EndIf

    If $binarynoval = "01010101" Then
        $binarynoval = Chr(85) 
    EndIf

    If $binarynoval = "01010110" Then
        $binarynoval = Chr(86) 
    EndIf

    If $binarynoval = "01010111" Then
        $binarynoval = Chr(87) 
    EndIf

    If $binarynoval = "01011000" Then
        $binarynoval = Chr(88) 
    EndIf

    If $binarynoval = "01011001" Then
        $binarynoval = Chr(89) 
    EndIf

    If $binarynoval = "01011010" Then
        $binarynoval = Chr(90) 
    EndIf

    If $binarynoval = "01100001" Then
        $binarynoval = Chr(97) 
    EndIf

    If $binarynoval = "01100010" Then
        $binarynoval = Chr(98) 
    EndIf

    If $binarynoval = "01100011" Then
        $binarynoval = Chr(99) 
    EndIf

    If $binarynoval = "01100100" Then
        $binarynoval = Chr(100) 
    EndIf

    If $binarynoval = "01100101" Then
        $binarynoval = Chr(101) 
    EndIf

    If $binarynoval = "01100110" Then
        $binarynoval = Chr(102) 
    EndIf

    If $binarynoval = "01100111" Then
        $binarynoval = Chr(103) 
    EndIf

    If $binarynoval = "01101000" Then
        $binarynoval = Chr(104) 
    EndIf

    If $binarynoval = "01101001" Then
        $binarynoval = Chr(105) 
    EndIf

    If $binarynoval = "01101010" Then
        $binarynoval = Chr(106) 
    EndIf

    If $binarynoval = "01101011" Then
        $binarynoval = Chr(107) 
    EndIf

    If $binarynoval = "01101100" Then
        $binarynoval = Chr(108) 
    EndIf

    If $binarynoval = "01101101" Then
        $binarynoval = Chr(109) 
    EndIf

    If $binarynoval = "01101110" Then
        $binarynoval = Chr(110) 
    EndIf

    If $binarynoval = "01101111" Then
        $binarynoval = Chr(111) 
    EndIf

    If $binarynoval = "01110000" Then
        $binarynoval = Chr(112) 
    EndIf

    If $binarynoval = "01110001" Then
        $binarynoval = Chr(113) 
    EndIf

    If $binarynoval = "01110010" Then
        $binarynoval = Chr(114) 
    EndIf

    If $binarynoval = "01110011" Then
        $binarynoval = Chr(115) 
    EndIf

    If $binarynoval = "01110100" Then
        $binarynoval = Chr(116) 
    EndIf

    If $binarynoval = "01110101" Then
        $binarynoval = Chr(117) 
    EndIf

    If $binarynoval = "01110110" Then
        $binarynoval = Chr(118) 
    EndIf

    If $binarynoval = "01110111" Then
        $binarynoval = Chr(119) 
    EndIf

    If $binarynoval = "01111000" Then
        $binarynoval = Chr(120) 
    EndIf

    If $binarynoval = "01111001" Then
        $binarynoval = Chr(121) 
    EndIf

    If $binarynoval = "01111010" Then
        $binarynoval = Chr(122) 
    EndIf

    If $binarynoval = "00100001" Then
        $binarynoval = Chr(33) 
    EndIf

    If $binarynoval = "00100010" Then
        $binarynoval = Chr(34) 
    EndIf

    If $binarynoval = "00100011" Then
        $binarynoval = Chr(35) 
    EndIf

    If $binarynoval = "00100100" Then
        $binarynoval = Chr(36) 
    EndIf

    If $binarynoval = "00100101" Then
        $binarynoval = Chr(37) 
    EndIf

    If $binarynoval = "00100110" Then
        $binarynoval = Chr(38) 
    EndIf

    If $binarynoval = "00100111" Then
        $binarynoval = Chr(39) 
    EndIf

    If $binarynoval = "00101000" Then
        $binarynoval = Chr(40) 
    EndIf

    If $binarynoval = "00101001" Then
        $binarynoval = Chr(41) 
    EndIf

    If $binarynoval = "00101010" Then
        $binarynoval = Chr(42) 
    EndIf

    If $binarynoval = "00101011" Then
        $binarynoval = Chr(43) 
    EndIf

    If $binarynoval = "00101100" Then
        $binarynoval = Chr(44) 
    EndIf

    If $binarynoval = "00101101" Then
        $binarynoval = Chr(45) 
    EndIf

    If $binarynoval = "00101110" Then
        $binarynoval = Chr(46) 
    EndIf

    If $binarynoval = "00101111" Then
        $binarynoval = Chr(47) 
    EndIf

    If $binarynoval = "00111010" Then
        $binarynoval = Chr(58) 
    EndIf

    If $binarynoval = "00111011" Then
        $binarynoval = Chr(59) 
    EndIf

    If $binarynoval = "00111100" Then
        $binarynoval = Chr(60) 
    EndIf

    If $binarynoval = "00111101" Then
        $binarynoval = Chr(61) 
    EndIf

    If $binarynoval = "00111110" Then
        $binarynoval = Chr(62) 
    EndIf

    If $binarynoval = "00111111" Then
        $binarynoval = Chr(63) 
    EndIf

    If $binarynoval = "01000000" Then
        $binarynoval = Chr(64) 
    EndIf

    If $binarynoval = "01011011" Then
        $binarynoval = Chr(91) 
    EndIf

    If $binarynoval = "01011100" Then
        $binarynoval = Chr(92) 
    EndIf

    If $binarynoval = "01011101" Then
        $binarynoval = Chr(93) 
    EndIf

    If $binarynoval = "01011110" Then
        $binarynoval = Chr(94) 
    EndIf

    If $binarynoval = "01011111" Then
        $binarynoval = Chr(95) 
    EndIf

    If $binarynoval = "01100000" Then
        $binarynoval = Chr(96) 
    EndIf

    Return $binarynoval

EndFunc   ;==>_binarynumbervalues


Func _stringvalues1($binarynoval1)

    If $binarynoval1 = Chr(32) Then
        $binarynoval1 = "00100000"
    EndIf

    If $binarynoval1 = Chr(48) Then
        $binarynoval1 = "00110000"
    EndIf

    If $binarynoval1 = Chr(49) Then
        $binarynoval1 = "00110001"
    EndIf

    If $binarynoval1 = Chr(50) Then
        $binarynoval1 = "00110010" 
    EndIf

    If $binarynoval1 = Chr(51) Then
        $binarynoval1 = "00110011" 
    EndIf

    If $binarynoval1 = Chr(52) Then
        $binarynoval1 = "00110100" 
    EndIf

    If $binarynoval1 = Chr(53) Then
        $binarynoval1 = "00110101" 
    EndIf

    If $binarynoval1 = Chr(54) Then
        $binarynoval1 = "00110110" 
    EndIf

    If $binarynoval1 = Chr(55) Then
        $binarynoval1 = "00110111" 
    EndIf

    If $binarynoval1 = Chr(56) Then
        $binarynoval1 = "00111000" 
    EndIf

    If $binarynoval1 = Chr(57) Then
        $binarynoval1 = "00111001" 
    EndIf

    If $binarynoval1 = Chr(65) Then
        $binarynoval1 = "01000001" 
    EndIf

    If $binarynoval1 = Chr(66) Then
        $binarynoval1 = "01000010" 
    EndIf

    If $binarynoval1 = Chr(67) Then
        $binarynoval1 = "01000011" 
    EndIf

    If $binarynoval1 = Chr(68) Then
        $binarynoval1 = "01000100" 
    EndIf

    If $binarynoval1 = Chr(69) Then
        $binarynoval1 = "01000101" 
    EndIf

    If $binarynoval1 = Chr(70) Then
        $binarynoval1 = "01000110" 
    EndIf

    If $binarynoval1 = Chr(71) Then
        $binarynoval1 = "01000111" 
    EndIf

    If $binarynoval1 = Chr(72) Then
        $binarynoval1 = "01001000" 
    EndIf

    If $binarynoval1 = Chr(73) Then
        $binarynoval1 = "01001001" 
    EndIf

    If $binarynoval1 = Chr(74) Then
        $binarynoval1 = "01001010" 
    EndIf

    If $binarynoval1 = Chr(75) Then
        $binarynoval1 = "01001011" 
    EndIf

    If $binarynoval1 = Chr(76) Then
        $binarynoval1 = "01001100" 
    EndIf

    If $binarynoval1 = Chr(77) Then
        $binarynoval1 = "01001101" 
    EndIf

    If $binarynoval1 = Chr(78) Then
        $binarynoval1 = "01001110" 
    EndIf

    If $binarynoval1 = Chr(79) Then
        $binarynoval1 = "01001111" 
    EndIf

    If $binarynoval1 = Chr(80) Then
        $binarynoval1 = "01010000" 
    EndIf

    If $binarynoval1 = Chr(81) Then
        $binarynoval1 = "01010001" 
    EndIf

    If $binarynoval1 = Chr(82) Then
        $binarynoval1 = "01010010" 
    EndIf

    If $binarynoval1 = Chr(83) Then
        $binarynoval1 = "01010011" 
    EndIf

    If $binarynoval1 = Chr(84) Then
        $binarynoval1 = "01010100" 
    EndIf

    If $binarynoval1 = Chr(85) Then
        $binarynoval1 = "01010101" 
    EndIf

    If $binarynoval1 = Chr(86) Then
        $binarynoval1 = "01010110" 
    EndIf

    If $binarynoval1 = Chr(87) Then
        $binarynoval1 = "01010111" 
    EndIf

    If $binarynoval1 = Chr(88) Then
        $binarynoval1 = "01011000" 
    EndIf

    If $binarynoval1 = Chr(89) Then
        $binarynoval1 = "01011001" 
    EndIf

    If $binarynoval1 = Chr(90) Then
        $binarynoval1 = "01011010" 
    EndIf

    If $binarynoval1 = Chr(97) Then
        $binarynoval1 = "01100001" 
    EndIf

    If $binarynoval1 = Chr(98) Then
        $binarynoval1 = "01100010" 
    EndIf

    If $binarynoval1 = Chr(99) Then
        $binarynoval1 = "01100011" 
    EndIf

    If $binarynoval1 = Chr(100) Then
        $binarynoval1 = "01100100" 
    EndIf

    If $binarynoval1 = Chr(101) Then
        $binarynoval1 = "01100101" 
    EndIf

    If $binarynoval1 = Chr(102) Then
        $binarynoval1 = "01100110" 
    EndIf

    If $binarynoval1 = Chr(103) Then
        $binarynoval1 = "01100111" 
    EndIf

    If $binarynoval1 = Chr(104) Then
        $binarynoval1 = "01101000" 
    EndIf

    If $binarynoval1 = Chr(105) Then
        $binarynoval1 = "01101001" 
    EndIf

    If $binarynoval1 = Chr(106) Then
        $binarynoval1 = "01101010" 
    EndIf

    If $binarynoval1 = Chr(107) Then
        $binarynoval1 = "01101011" 
    EndIf

    If $binarynoval1 = Chr(108) Then
        $binarynoval1 = "01101100" 
    EndIf

    If $binarynoval1 = Chr(109) Then
        $binarynoval1 = "01101101" 
    EndIf

    If $binarynoval1 = Chr(110) Then
        $binarynoval1 = "01101110" 
    EndIf

    If $binarynoval1 = Chr(111) Then
        $binarynoval1 = "01101111" 
    EndIf

    If $binarynoval1 = Chr(112) Then
        $binarynoval1 = "01110000" 
    EndIf

    If $binarynoval1 = Chr(113) Then
        $binarynoval1 = "01110001" 
    EndIf

    If $binarynoval1 = Chr(114) Then
        $binarynoval1 = "01110010" 
    EndIf

    If $binarynoval1 = Chr(115) Then
        $binarynoval1 = "01110011" 
    EndIf

    If $binarynoval1 = Chr(116) Then
        $binarynoval1 = "01110100" 
    EndIf

    If $binarynoval1 = Chr(117) Then
        $binarynoval1 = "01110101" 
    EndIf

    If $binarynoval1 = Chr(118) Then
        $binarynoval1 = "01110110" 
    EndIf

    If $binarynoval1 = Chr(119) Then
        $binarynoval1 = "01110111" 
    EndIf

    If $binarynoval1 = Chr(120) Then
        $binarynoval1 = "01111000" 
    EndIf

    If $binarynoval1 = Chr(121) Then
        $binarynoval1 = "01111001" 
    EndIf

    If $binarynoval1 = Chr(122) Then
        $binarynoval1 = "01111010" 
    EndIf

    If $binarynoval1 = Chr(33) Then
        $binarynoval1 = "00100001" 
    EndIf

    If $binarynoval1 = Chr(34) Then
        $binarynoval1 = "00100010" 
    EndIf

    If $binarynoval1 = Chr(35) Then
        $binarynoval1 = "00100011" 
    EndIf

    If $binarynoval1 = Chr(36) Then
        $binarynoval1 = "00100100" 
    EndIf

    If $binarynoval1 = Chr(37) Then
        $binarynoval1 = "00100101" 
    EndIf

    If $binarynoval1 = Chr(38) Then
        $binarynoval1 = "00100110" 
    EndIf

    If $binarynoval1 = Chr(39) Then
        $binarynoval1 = "00100111" 
    EndIf

    If $binarynoval1 = Chr(40) Then
        $binarynoval1 = "00101000" 
    EndIf

    If $binarynoval1 = Chr(41) Then
        $binarynoval1 = "00101001" 
    EndIf

    If $binarynoval1 = Chr(42) Then
        $binarynoval1 = "00101010" 
    EndIf

    If $binarynoval1 = Chr(43) Then
        $binarynoval1 = "00101011" 
    EndIf

    If $binarynoval1 = Chr(44) Then
        $binarynoval1 = "00101100" 
    EndIf

    If $binarynoval1 = Chr(45) Then
        $binarynoval1 = "00101101" 
    EndIf

    If $binarynoval1 = Chr(46) Then
        $binarynoval1 = "00101110" 
    EndIf

    If $binarynoval1 = Chr(47) Then
        $binarynoval1 = "00101111" 
    EndIf

    If $binarynoval1 = Chr(58) Then
        $binarynoval1 = "00111010" 
    EndIf

    If $binarynoval1 = Chr(59) Then
        $binarynoval1 = "00111011" 
    EndIf

    If $binarynoval1 = Chr(60) Then
        $binarynoval1 = "00111100" 
    EndIf

    If $binarynoval1 = Chr(61) Then
        $binarynoval1 = "00111101" 
    EndIf

    If $binarynoval1 = Chr(62) Then
        $binarynoval1 = "00111110" 
    EndIf

    If $binarynoval1 = Chr(63) Then
        $binarynoval1 = "00111111" 
    EndIf

    If $binarynoval1 = Chr(64) Then
        $binarynoval1 = "01000000" 
    EndIf

    If $binarynoval1 = Chr(91) Then
        $binarynoval1 = "01011011" 
    EndIf

    If $binarynoval1 = Chr(92) Then
        $binarynoval1 = "01011100" 
    EndIf

    If $binarynoval1 = Chr(93) Then
        $binarynoval1 = "01011101" 
    EndIf

    If $binarynoval1 = Chr(94) Then
        $binarynoval1 = "01011110" 
    EndIf

    If $binarynoval1 = Chr(95) Then
        $binarynoval1 = "01011111" 
    EndIf

    If $binarynoval1 = Chr(96) Then
        $binarynoval1 = "01100000" 
    EndIf

    Return $binarynoval1

EndFunc   ;==>_stringvalues1

So it works, but when I do this:

#include <BinaryNumberandString.au3>

$s = _StringtoNumberBinary("Hello")
$q = _binarynumbertostring($s)

ConsoleWrite($q)

The string comes back as HELLO (i.e. Capitals) and I cant for the life of me figure out why, just wondering whether any of you would be able to lend a hand

Thanks in advance

Link to comment
Share on other sites

What's wrong with StringToBinary() and BinaryToString()?

$sString = "Hello"
$bBinary = StringToBinary($sString)
$sString = BinaryToString($bBinary)

ConsoleWrite($sString & @CRLF)

UDF List:

 
_AdapterConnections() • _AlwaysRun() • _AppMon() • _AppMonEx() • _ArrayFilter/_ArrayReduce • _BinaryBin() • _CheckMsgBox() • _CmdLineRaw() • _ContextMenu() • _ConvertLHWebColor()/_ConvertSHWebColor() • _DesktopDimensions() • _DisplayPassword() • _DotNet_Load()/_DotNet_Unload() • _Fibonacci() • _FileCompare() • _FileCompareContents() • _FileNameByHandle() • _FilePrefix/SRE() • _FindInFile() • _GetBackgroundColor()/_SetBackgroundColor() • _GetConrolID() • _GetCtrlClass() • _GetDirectoryFormat() • _GetDriveMediaType() • _GetFilename()/_GetFilenameExt() • _GetHardwareID() • _GetIP() • _GetIP_Country() • _GetOSLanguage() • _GetSavedSource() • _GetStringSize() • _GetSystemPaths() • _GetURLImage() • _GIFImage() • _GoogleWeather() • _GUICtrlCreateGroup() • _GUICtrlListBox_CreateArray() • _GUICtrlListView_CreateArray() • _GUICtrlListView_SaveCSV() • _GUICtrlListView_SaveHTML() • _GUICtrlListView_SaveTxt() • _GUICtrlListView_SaveXML() • _GUICtrlMenu_Recent() • _GUICtrlMenu_SetItemImage() • _GUICtrlTreeView_CreateArray() • _GUIDisable() • _GUIImageList_SetIconFromHandle() • _GUIRegisterMsg() • _GUISetIcon() • _Icon_Clear()/_Icon_Set() • _IdleTime() • _InetGet() • _InetGetGUI() • _InetGetProgress() • _IPDetails() • _IsFileOlder() • _IsGUID() • _IsHex() • _IsPalindrome() • _IsRegKey() • _IsStringRegExp() • _IsSystemDrive() • _IsUPX() • _IsValidType() • _IsWebColor() • _Language() • _Log() • _MicrosoftInternetConnectivity() • _MSDNDataType() • _PathFull/GetRelative/Split() • _PathSplitEx() • _PrintFromArray() • _ProgressSetMarquee() • _ReDim() • _RockPaperScissors()/_RockPaperScissorsLizardSpock() • _ScrollingCredits • _SelfDelete() • _SelfRename() • _SelfUpdate() • _SendTo() • _ShellAll() • _ShellFile() • _ShellFolder() • _SingletonHWID() • _SingletonPID() • _Startup() • _StringCompact() • _StringIsValid() • _StringRegExpMetaCharacters() • _StringReplaceWholeWord() • _StringStripChars() • _Temperature() • _TrialPeriod() • _UKToUSDate()/_USToUKDate() • _WinAPI_Create_CTL_CODE() • _WinAPI_CreateGUID() • _WMIDateStringToDate()/_DateToWMIDateString() • Au3 script parsing • AutoIt Search • AutoIt3 Portable • AutoIt3WrapperToPragma • AutoItWinGetTitle()/AutoItWinSetTitle() • Coding • DirToHTML5 • FileInstallr • FileReadLastChars() • GeoIP database • GUI - Only Close Button • GUI Examples • GUICtrlDeleteImage() • GUICtrlGetBkColor() • GUICtrlGetStyle() • GUIEvents • GUIGetBkColor() • Int_Parse() & Int_TryParse() • IsISBN() • LockFile() • Mapping CtrlIDs • OOP in AutoIt • ParseHeadersToSciTE() • PasswordValid • PasteBin • Posts Per Day • PreExpand • Protect Globals • Queue() • Resource Update • ResourcesEx • SciTE Jump • Settings INI • SHELLHOOK • Shunting-Yard • Signature Creator • Stack() • Stopwatch() • StringAddLF()/StringStripLF() • StringEOLToCRLF() • VSCROLL • WM_COPYDATA • More Examples...

Updated: 22/04/2018

Link to comment
Share on other sites

jchd created a function ages ago to do what you want.

UDF List:

 
_AdapterConnections() • _AlwaysRun() • _AppMon() • _AppMonEx() • _ArrayFilter/_ArrayReduce • _BinaryBin() • _CheckMsgBox() • _CmdLineRaw() • _ContextMenu() • _ConvertLHWebColor()/_ConvertSHWebColor() • _DesktopDimensions() • _DisplayPassword() • _DotNet_Load()/_DotNet_Unload() • _Fibonacci() • _FileCompare() • _FileCompareContents() • _FileNameByHandle() • _FilePrefix/SRE() • _FindInFile() • _GetBackgroundColor()/_SetBackgroundColor() • _GetConrolID() • _GetCtrlClass() • _GetDirectoryFormat() • _GetDriveMediaType() • _GetFilename()/_GetFilenameExt() • _GetHardwareID() • _GetIP() • _GetIP_Country() • _GetOSLanguage() • _GetSavedSource() • _GetStringSize() • _GetSystemPaths() • _GetURLImage() • _GIFImage() • _GoogleWeather() • _GUICtrlCreateGroup() • _GUICtrlListBox_CreateArray() • _GUICtrlListView_CreateArray() • _GUICtrlListView_SaveCSV() • _GUICtrlListView_SaveHTML() • _GUICtrlListView_SaveTxt() • _GUICtrlListView_SaveXML() • _GUICtrlMenu_Recent() • _GUICtrlMenu_SetItemImage() • _GUICtrlTreeView_CreateArray() • _GUIDisable() • _GUIImageList_SetIconFromHandle() • _GUIRegisterMsg() • _GUISetIcon() • _Icon_Clear()/_Icon_Set() • _IdleTime() • _InetGet() • _InetGetGUI() • _InetGetProgress() • _IPDetails() • _IsFileOlder() • _IsGUID() • _IsHex() • _IsPalindrome() • _IsRegKey() • _IsStringRegExp() • _IsSystemDrive() • _IsUPX() • _IsValidType() • _IsWebColor() • _Language() • _Log() • _MicrosoftInternetConnectivity() • _MSDNDataType() • _PathFull/GetRelative/Split() • _PathSplitEx() • _PrintFromArray() • _ProgressSetMarquee() • _ReDim() • _RockPaperScissors()/_RockPaperScissorsLizardSpock() • _ScrollingCredits • _SelfDelete() • _SelfRename() • _SelfUpdate() • _SendTo() • _ShellAll() • _ShellFile() • _ShellFolder() • _SingletonHWID() • _SingletonPID() • _Startup() • _StringCompact() • _StringIsValid() • _StringRegExpMetaCharacters() • _StringReplaceWholeWord() • _StringStripChars() • _Temperature() • _TrialPeriod() • _UKToUSDate()/_USToUKDate() • _WinAPI_Create_CTL_CODE() • _WinAPI_CreateGUID() • _WMIDateStringToDate()/_DateToWMIDateString() • Au3 script parsing • AutoIt Search • AutoIt3 Portable • AutoIt3WrapperToPragma • AutoItWinGetTitle()/AutoItWinSetTitle() • Coding • DirToHTML5 • FileInstallr • FileReadLastChars() • GeoIP database • GUI - Only Close Button • GUI Examples • GUICtrlDeleteImage() • GUICtrlGetBkColor() • GUICtrlGetStyle() • GUIEvents • GUIGetBkColor() • Int_Parse() & Int_TryParse() • IsISBN() • LockFile() • Mapping CtrlIDs • OOP in AutoIt • ParseHeadersToSciTE() • PasswordValid • PasteBin • Posts Per Day • PreExpand • Protect Globals • Queue() • Resource Update • ResourcesEx • SciTE Jump • Settings INI • SHELLHOOK • Shunting-Yard • Signature Creator • Stack() • Stopwatch() • StringAddLF()/StringStripLF() • StringEOLToCRLF() • VSCROLL • WM_COPYDATA • More Examples...

Updated: 22/04/2018

Link to comment
Share on other sites

I have writtten a function. _BaseToBase() in math10.au3

Here's the topic /thread:

It maxes out at 999999999999999. To convert hex you need to remove the 0x prefix and pass the number as a string.

With text you could convert 32 bits at a time using a loop. There is also a better way. Let me think.

Edited by czardas
Link to comment
Share on other sites

Hello all

So i've written this UDF to convert a string to a Number Binary sequence and vice versa:

.....

The string comes back as HELLO (i.e. Capitals) and I cant for the life of me figure out why, just wondering whether any of you would be able to lend a hand

Thanks in advance

 

hi Noviceatthis

your script is a little "verbose"

I just semplified a bit only the decimal to binary conversion and back

anyway it can be semplified also a lot more

Func _binarynumbertostring($sBinarynumbertoconvert)
    $numberofcharacters = StringLen($sBinarynumbertoconvert)
    $numberofstringcharacters = $numberofcharacters / 8
    If IsInt($numberofstringcharacters) Then
        Sleep(50)
    Else
        MsgBox(0, "Error", "Incorrect Number of Digits in Binary Number")
    EndIf

    For $i = 1 To $numberofstringcharacters
        $leftcharacter = StringLeft($sBinarynumbertoconvert, 8)
        $stringchr = Chr(_binarynumbervalues($leftcharacter))
        $opentempfile = FileOpen(@TempDir & "\stringfrombinary.txt", 9)
        FileWrite($opentempfile, $stringchr)
        FileClose($opentempfile)
        $sBinarynumbertoconvert = StringTrimLeft($sBinarynumbertoconvert, 8)
    Next
    $opentempfile2 = FileOpen(@TempDir & "\stringfrombinary.txt")
    $filecontents = FileRead($opentempfile2)
    FileClose($opentempfile2)
    FileDelete(@TempDir & "\stringfrombinary.txt")
    Return $filecontents
EndFunc   ;==>_binarynumbertostring

Func _StringtoNumberBinary($sBinarynumbertoconvert1)
    $numberofcharacters1 = StringLen($sBinarynumbertoconvert1)
    If IsInt($numberofcharacters1) Then
        Sleep(50)
    Else
        MsgBox(0, "Error", "Incorrect Number of Digits in Binary Number")
    EndIf

    For $i1 = 1 To $numberofcharacters1
        $leftcharacter1 = Asc(StringLeft($sBinarynumbertoconvert1, 1))

        $stringchr1 = _stringvalues1($leftcharacter1) ; dec to bin
        $opentempfile1 = FileOpen(@TempDir & "\stringfrombinary.txt", 9)
        FileWrite($opentempfile1, $stringchr1)
        FileClose($opentempfile1)
        $sBinarynumbertoconvert1 = StringTrimLeft($sBinarynumbertoconvert1, 1)
    Next
    $opentempfile21 = FileOpen(@TempDir & "\stringfrombinary.txt")
    $filecontents1 = FileRead($opentempfile21)
    FileClose($opentempfile21)
    FileDelete(@TempDir & "\stringfrombinary.txt")
    Return $filecontents1
EndFunc   ;==>_StringtoNumberBinary

Func _binarynumbervalues($binarynoval1)
    Local $dec = 0
    For $i = 0 To StringLen($binarynoval1) - 1
        $dec += 2 ^ $i * StringMid($binarynoval1, StringLen($binarynoval1) - $i, 1)
    Next
    Return $dec
EndFunc   ;==>_binarynumbervalues

Func _stringvalues1($binarynoval2)
    Local $sBinString = ""
    While $binarynoval2
        $sBinString = BitAND($binarynoval2, 1) & $sBinString
        $binarynoval2 = BitShift($binarynoval2, 1)
    WEnd
    ; limit returned value to 8 bit 0 - 255
    Return StringRight("00000000" & $sBinString, 8)
EndFunc   ;==>_stringvalues1
#include <BinaryNumberandString.au3>
$a = "Hello"
ConsoleWrite("1-> " & $a & @CRLF)
$s = _StringtoNumberBinary($a)
ConsoleWrite("2-> " & $s & @CRLF)
$q = _binarynumbertostring($s)
ConsoleWrite("3-> " & $q & @CRLF)
Edited by PincoPanco

 

image.jpeg.9f1a974c98e9f77d824b358729b089b0.jpeg Chimp

small minds discuss people average minds discuss events great minds discuss ideas.... and use AutoIt....

Link to comment
Share on other sites

I thought you wanted to convert numbers when I read the title. :doh:

Here's how I would do it. I haven't tested it for speed.

Global $g_aBINARY[16] = ["0000","0001","0010","0011","0100","0101","0110","0111","1000","1001","1010","1011","1100","1101","1110","1111"]

Func _TextToBinaryString($sText)
    $sText = StringTrimLeft(StringToBinary($sText), 2)
    For $i = 0 To 15
        $sText = StringReplace($sText, StringRight(Hex($i), 1) , $g_aBINARY[$i])
    Next

    Return $sText
EndFunc

Func _BinaryStringToText($sBinary)
    Local $sString = ""
    For $i = 1 To StringLen($sBinary) Step 4
        $sString &= StringMid($sBinary, $i, 4) & " "
    Next
    For $i = 0 To 15
        $sString = StringReplace($sString, $g_aBINARY[$i], StringRight(Hex($i), 1))
    Next
    $sString = BinaryToString("0x" & StringReplace($sString, " ", ""))

    Return $sString
EndFunc

Local $sText = "Hello"
$sText = _TextToBinaryString($sText)
MsgBox(0, "", $sText)

$sText = _BinaryStringToText($sText)
MsgBox(0, "", $sText)
Edited by czardas
Link to comment
Share on other sites

No worries. :)

It works like this:

Start with some hex.

Replace 0 with 0000

Replace 1 with 0001 ==> These two have to be converted first in the correct order to avoid corruption.

Replace the rest of the values 2 to F in any order you wish.

The reverse process is a bit different.

Separate each set of 4 binary digits with space and then replace with single hex digits. etc...

Link to comment
Share on other sites

yet another one...

#include <Array.au3>
#include <String.au3>

local $str = 'Hello'

ConsoleWrite(stringformat('%10s %10s %10s %10s','CHAR','DEC','HEX','BINARY') & @LF)

for $1 = 1 to stringlen($str)
    ConsoleWrite(stringformat('%10s %10s %10s %10s',  _
                stringmid($str,$1,1),  _
                asc(stringmid($str,$1,1)), _
                hex(asc(stringmid($str,$1,1)),3), _
                Integer2Binary( asc(stringmid($str,$1,1)) ) ) & @lf)
next

Func Integer2Binary($in)
    ;
    ; adapted from orginal code by UEZ and modified by FireFox, Kylomas
    ;
    If $in = 0 Then Return StringFormat("%08s", 0)
    Local $bin
    While $in > 0
        $bin &= Mod($in, 2)
        $in = Floor($in / 2)
    WEnd
    $bin = _StringReverse($bin)
    $bin = StringFormat("%08s", $bin)
    Return ($bin)
EndFunc   ;==>Integer2Binary

Forum Rules         Procedure for posting code

"I like pigs.  Dogs look up to us.  Cats look down on us.  Pigs treat us as equals."

- Sir Winston Churchill

Link to comment
Share on other sites

...yet another...

Global $Text = "Hello"
ConsoleWrite($Text & @CRLF)
ConsoleWrite(_TextToBin($Text) & @CRLF)
ConsoleWrite(_BinToText(_TextToBin($Text)) & @CRLF)

Func _TextToBin($Text)
    Local $Binary = ""
    For $i = 1 To StringLen($Text)
        $Binary &= _DecToBin(Asc(StringMid($Text, $i, 1)))
    Next
    Return $Binary
EndFunc   ;==>_TextToBin

Func _BinToText($Binary)
    Local $Text = ""
    For $i = 1 To StringLen($Binary) Step 8
        $Text &= Chr(_BinToDec(StringMid($Binary, $i, 8)))
    Next
    Return $Text
EndFunc   ;==>_BinToText

Func _DecToBin($Dec) ; from decimal to binary
    Local $sBinString = ""
    While $Dec
        $sBinString = BitAND($Dec, 1) & $sBinString
        $dec = BitShift($dec, 1)
    WEnd
    Return StringRight("00000000" & $sBinString, 8)
EndFunc   ;==>_DecToBin

Func _BinToDec($Bin) ; from binary to decimal
    Local $dec = 0
    For $i = 0 To StringLen($Bin) - 1
        $dec += 2 ^ $i * StringMid($Bin, StringLen($Bin) - $i, 1)
    Next
    Return $dec
EndFunc   ;==>_BinToDec

edit:

decimal to binary found >here

Edited by PincoPanco

 

image.jpeg.9f1a974c98e9f77d824b358729b089b0.jpeg Chimp

small minds discuss people average minds discuss events great minds discuss ideas.... and use AutoIt....

Link to comment
Share on other sites

There are some good ideas to learn from here, but replacing single characters one at a time is never going to be as efficient as making replacements on the whole string in one hit... 16 times.

@Noviceatthis - My code needs error checks adding at the start of each function - to check that the input is valid.

Edited by czardas
Link to comment
Share on other sites

There are some good ideas to learn from here, but replacing single characters one at a time is never going to be as efficient as making replacements on the whole string in one hit... 16 times.

@Noviceatthis - My code needs error checks adding at the start of each function - to check that the input is valid.

 

I agree with you,

I like the way you use that array of nibble

very cool

 

image.jpeg.9f1a974c98e9f77d824b358729b089b0.jpeg Chimp

small minds discuss people average minds discuss events great minds discuss ideas.... and use AutoIt....

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...