Page 1 of 1

Binary converter

Posted: 11 Apr 2012 09:10
by doscode
I looked internet if there is some example how to make converter from binary to text and from text to binary. I have found this video http://www.youtube.com/watch?v=gEl5yzhcfsw and tried to rewrite the script. I did not see all parts of the video, because some parts of the code are not visible. I do not know help myself with some values. Maybe you can help me and repair my code or to add some lines?


Code: Select all

@echo off
title Hex2Ascii converter
setlocal enabledelayedexpansion

pushd %temp%

:Start
:: Reset varaibles
set Counter=0
set AsciiCode=
ser AsciiCode_Puffer=
set DecimalCode
set BinaryCode=
set HexCode=

:: Ask for hexadecimal code
set /p HexCode=Hex:
set HexCode=%HexCode:0x=%

:: Check the beginning and the ending
if "%HexCode:~0,1%" == "{" set Counter=1
echo !HexCode! | find "}" >nul 2>null || set HexCode=!HexCode! {p8UyY}

:Conversion
set AsciiCode_Puffer=!AsciiCode!
::Check if finished
if "!HexCode:~%Counter%,1!" == "}" goto :Finished
if "!HexCode:~%Counter%,9!" == "  p8UyY" goto :Finished

:: Check for symbols and spaces
if "HexCode:~%Counters%,1!" == "-" set /a Counter=%Counter% + 1 & goto :Conversion
if "HexCode:~%Counters%,1!" == " " set /a Counter=%Counter% + 1 & goto :Conversion
if "HexCode:~%Counters%,1!" == "," set /a Counter=%Counter% + 1 & goto :Conversion
if "HexCode:~%Counters%,1!" == "|" set /a Counter=%Counter% + 1 & goto :Conversion
if "HexCode:~%Counters%,1!" == "." set /a Counter=%Counter% + 1 & goto :Conversion
if "HexCode:~%Counters%,1!" == ":" set /a Counter=%Counter% + 1 & goto :Conversion
if "HexCode:~%Counters%,1!" == ";" set /a Counter=%Counter% + 1 & goto :Conversion

:: Start of conversion from hex to ascii
if "!HexCode:~%Counters%,2!" == "00" set /a Counter=%Counter% + 2 & set AsciiCode=!AsciiCode!{NUL}& set DecimalCode=!DecimalCode!0, & set BinaryCode=!BinaryCode!00000000
if "!HexCode:~%Counters%,2!" == "01" set /a Counter=%Counter% + 2 & set AsciiCode=!AsciiCode!SOH& set DecimalCode=!DecimalCode!1, & set   BinaryCode=!BinaryCode!00000001
if "!HexCode:~%Counters%,2!" == "02" set /a Counter=%Counter% + 2 & set AsciiCode=!AsciiCode!STX& set DecimalCode=!DecimalCode!2, & set   BinaryCode=!BinaryCode!00000010
if "!HexCode:~%Counters%,2!" == "03" set /a Counter=%Counter% + 2 & set AsciiCode=!AsciiCode!ETX& set DecimalCode=!DecimalCode!3, & set   BinaryCode=!BinaryCode!00000011
if "!HexCode:~%Counters%,2!" == "04" set /a Counter=%Counter% + 2 & set AsciiCode=!AsciiCode!EOT& set DecimalCode=!DecimalCode!4, & set   BinaryCode=!BinaryCode!00000100
if "!HexCode:~%Counter%,2!" == "05" set /a Counter=%Counter% + 2 & set AsciiCode=!AsciiCode!ENQ& set DecimalCode=!DecimalCode!5, & set   BinaryCode=!BinaryCode!00000101
if "!HexCode:~%Counter%,2!" == "06" set /a Counter=%Counter% + 2 & set AsciiCode=!AsciiCode!ACK& set DecimalCode=!DecimalCode!6, & set   BinaryCode=!BinaryCode!00000110
if "!HexCode:~%Counter%,3!" == "07" set /a Counter=%Counter% + 2 & set AsciiCode=!AsciiCode!BEL& set DecimalCode=!DecimalCode!7, & set   BinaryCode=!BinaryCode!00000111
if "!HexCode:~%Counter%,2!" == "08" set /a Counter=%Counter% + 2 & set AsciiCode=!AsciiCode!BS& set DecimalCode=!DecimalCode!8, &  set   BinaryCode=!BinaryCode!00001000
if "!HexCode:~%Counter%,2!" == "09" set /a Counter=%Counter% + 2 & set AsciiCode=!AsciiCode! & set DecimalCode=!DecimalCode!9, &   set   BinaryCode=!BinaryCode!00001001
if /i "!HexCode:~%Counter%,2!" == "0a" set /a Counter=%Counter% + 2 & set AsciiCode=!AsciiCode!{LF}& set DecimalCode=!DecimalCode!10, & set  BinaryCode=!BinaryCode!00001010
if /i "!HexCode:~%Counter%,2!" == "0b" set /a Counter=%Counter% + 2 & set AsciiCode=!AsciiCode!VT& set DecimalCode=!DecimalCode!11, & set  BinaryCode=!BinaryCode!00001011
if /i "!HexCode:~%Counter%,2!" == "0c" set /a Counter=%Counter% + 2 & set AsciiCode=!AsciiCode!FF& set DecimalCode=!DecimalCode!12, & set  BinaryCode=!BinaryCode!00001100
if /i "!HexCode:~%Counter%,2!" == "0d" set /a Counter=%Counter% + 2 & set AsciiCode=!AsciiCode!FF& set DecimalCode=!DecimalCode!13, & set  BinaryCode=!BinaryCode!00001101
if /i "!HexCode:~%Counter%,2!" == "0e" set /a Counter=%Counter% + 2 & set AsciiCode=!AsciiCode!FF& set DecimalCode=!DecimalCode!14, & set  BinaryCode=!BinaryCode!00001110
if /i "!HexCode:~%Counter%,2!" == "0f" set /a Counter=%Counter% + 2 & set AsciiCode=!AsciiCode!FF& set DecimalCode=!DecimalCode!15, & set  BinaryCode=!BinaryCode!00001111


:Finished
echo !HexCode! | find "p8UyY" >nul 2>nul && set HexCode=!HexCode:!
if "!HexCode:~0,1!" == "{" set AsciiCode={!AsciiCode!&set DecimalCode {
if "!HexCode:~-1,1!" == "}" set AsciiCode={!AsciiCode!}&set DecimalCode

echo Hex: !HexCode! 1>Hex2Ascii.tmp
echo Dec: !DecimalCode! 1>>Hex2Ascii.tmp
echo Dec: !AsciiCode! 1>>Hex2Ascii.tmp
echo Bin: !BinaryCode! 1>>Hex2Ascii.tmp
pause
start notepad "%temp%\Hex2Ascii.tmp"
echo Dec: !DecimalCode!
echo Ascii: !AsciiCode!    ^<- in CMD
echo Bin: !BinaryCode!
echo.
echo.
goto :Start

Re: Binary converter

Posted: 11 Apr 2012 10:06
by miskox
Looks like you can download the file here: http://www.batchlog.pytalhost.com/Hex2All.bat

Convert LF to CRLF.

Hope this helps,
Saso

Re: Binary converter

Posted: 11 Apr 2012 10:22
by doscode
Sure it helps! Thanks. What does mean "LF" and "CRLF" ?

Re: Binary converter

Posted: 11 Apr 2012 10:28
by miskox
Lines end with LineFeed (LF). Windows (and DOS) use CarriageReturn+LineFeed (CRLF). Looks like this file has Unix (LF only) format. But I found this file in here too (with correct CRLF):
I use GSAR for such conversions.

http://www.batchlog.pytalhost.com/Batchfiles.zip

(Check other files, too - there is a Monopoly game in batch).

Saso

Re: Binary converter

Posted: 11 Apr 2012 12:17
by doscode
Anyway what encoding is this:

Code: Select all

4D 00 61 00 70 00 73 00
32 00 42 00 67 00 6C 00
2E 00 65 00 78 00 65 00
00 00 49 00 3A 00 5C 00
5F 00 56 00 0C 01 6A 00
69 00 68 00 5C 00 5F 00
45 00 58 00 54 00 52 00
41 00 00 00


It is from Windows register (and its value is path). I use Windows1250 on my system and I don't know if the above encoding is CP1250 or Unix? The script probably converts to ASCII and it is not the same (I think). What should I notice if I want to convert this path saved in binary format, back to CP1250. Does the script work with this encoding?

Re: Binary converter

Posted: 11 Apr 2012 12:57
by miskox
Looks like Unicode.

Code: Select all

M a p s 2 B g l . e x e   I : \ _ V j i h \ _ E X T R A   


Saso

Re: Binary converter

Posted: 11 Apr 2012 13:07
by aGerman
It is unicode, more specific it is an array of two unicode strings represented by its HEX values.
The 2 members of the array are Maps2Bgl.exe and I:\_VČjih\_EXTRA.

@doscode
Have a look at this thread:
viewtopic.php?f=3&t=3089&start=0

Regards
aGerman

Re: Binary converter

Posted: 12 Apr 2012 00:28
by Aacini
miskox wrote:Looks like you can download the file here: http://www.batchlog.pytalhost.com/Hex2All.bat

Hope this helps,
Saso

Wow!!! I downloaded this 58 KB Batch file. :shock: I only can say that this and similar programs are the origin of the infamous "rudimentary and limited programming language" that Batch have in very many places. Although there are several ways to solve a problem, this one and similar Batch files chooses precisely the worst way.

However, is not that bad that some people write Batch files like this one; what is worst is that they post their files in the web and other people take them as the "model to follow". :cry:

The Batch file below achieve an equivalent conversion from hex digits to "Ascii characters"; perhaps you need to retype some characters that may be misleaded by the posting in this site. This version just convert the Hex digits given in the Batch parameter.

Code: Select all

@echo off
setlocal DisableDelayedExpansion
set Caret=^^
set ControlChar={SOH} {STX} {ETX} {EOT} {ENQ} {ACK} {BEL} {BS} {HT} {LF} {VT} {FF} {CR} {SO} {SI} {DLE} {XON}
set ControlChar=%ControlChar% {DC2} {XOFF} {DC4} {NAK} {SYN} {ETB} {CAN} {EM} {SUB} {ESC} {FS} {GS} {RS} {US}
set AsciiChar= !"#$%%&'()*+,-./0123456789:;<=>?
setlocal EnableDelayedExpansion
set AsciiChar=!AsciiChar!@ABCDEFGHIJKLMNOPQRSTUVWXYZ[\]!Caret!_`abcdefghijklmnopqrstuvwxyz{^|}~
set AsciiChar=!AsciiChar!€‚ƒ„…†‡ˆ‰Š‹ŒŽ‘’“”•–—˜™š›œžŸ ¡¢£¤¥¦§¨©ª«¬­®¯°±²³´µ¶·¸¹º»¼½¾¿
set AsciiChar=!AsciiChar!ÀÁÂÃÄÅÆÇÈÉÊËÌÍÎÏÐÑÒÓÔÕÖ×ØÙÚÛÜÝÞßàáâãäåæçèéêëìíîïðñòóôõö÷øùúûüýþÿ

set HexCode=%1
call :Conversion
echo Hex:   %1
echo Dec:   %DecimalCode%
echo Ascii: !AsciiCode!
echo Bin:   %BinaryCode%
goto :EOF


:Conversion
set DecimalCode=
set AsciiCode=
set BinaryCode=
:ConvertHex
   set /A Decimal=0x%HexCode:~0,2%, DecChar=Decimal-32
   set DecimalCode=%DecimalCode%%Decimal%,
   if %Decimal% lss 32 (
      if %Decimal% equ 0 (
         set AsciiCode=!AsciiCode!{NUL}
      ) else (
         for /F "tokens=%Decimal%" %%c in ("%ControlChar%") do set AsciiCode=!AsciiCode!%%c
      )
   ) else (   
      set AsciiCode=!AsciiCode!!AsciiChar:~%DecChar%,1!
   )
   call :DecToBin
   set BinaryCode=%BinaryCode%%Binary%,
   set HexCode=%HexCode:~2%
   if defined HexCode goto ConvertHex
exit /B

:DecToBin
set Binary=
for /L %%i in (1,1,8) do (
   set /A "Bit=Decimal&1, Decimal>>=1"
   set Binary=!Bit!!Binary!
)                           
exit /B

However, no one of these programs can accurately generate ALL the control characters (below 0x20 = dec 32 = Ascii space). I ignore if jeb or dave or another one of the experts had developed a method to generate all control characters in a Batch file...

Re: Binary converter

Posted: 12 Apr 2012 01:13
by Liviu
Aacini wrote:accurately generate ALL the control characters (below 0x20 = dec 32 = Ascii space).

https://sites.google.com/site/dbenhamfi ... ib_bat.txt

viewtopic.php?p=13255#p13255

Liviu

Re: Binary converter

Posted: 12 Apr 2012 01:32
by doscode
Aacini wrote:I ignore if jeb or dave or another one of the experts had developed a method to generate all control characters in a Batch file...


That was not my target. I wanted to find some program, that could enable me to read the binary path from the registry. People here told me it is unicode, so it is a little bit harder. One would need to incorporate charset for the requested language... However I am not going to try to develop it, because I already do not need it. Maybe in future when I would have the need again.

Re: Binary converter

Posted: 12 Apr 2012 08:49
by Aacini
You may convert a limited set of Unicode characters (those with a second byte equal zero) if you modify this line in my program:

Code: Select all

   set HexCode=%HexCode:~2%

... by this one:

Code: Select all

   set HexCode=%HexCode:~4%

For example:

Code: Select all

Hex:   4D006100700073003200420067006C002E00650078006500000049003A005C005F0056000C016A00690068005C005F00450058005400520041000000
Dec:   77,97,112,115,50,66,103,108,46,101,120,101,0,73,58,92,95,86,12,106,105,104,92,95,69,88,84,82,65,0,
Ascii: Maps2Bgl.exe{NUL}I:\_V{FF}jih\_EXTRA{NUL}
Bin:   01001101,01100001,01110000,01110011,00110010,01000010,01100111,01101100,00101110,01100101,01111000,01100101,00000000,
01001001,00111010,01011100,01011111,01010110,00001100,01101010,01101001,01101000,01011100,01011111,01000101,01011000,
01010100,01010010,01000001,00000000,