Hello Visitors,

TechKnow's dowload hosts are pretty much all down. :(

Sorry, but I couldn't pay the paid host any longer and the sponsored hosts all quit on me.


HcH

Author Topic: [BAT] ASCII2HEX, HEX2ASCII, HEX2BIN Converter  (Read 1678 times)

0 Members and 1 Guest are viewing this topic.

Offline HardcoreHacker

  • TechKnow Owner
  • Administrator
  • Uber Member
  • *****
  • Thank You
  • -Given: 3697
  • -Received: 1128
  • Posts: 9624
  • Karma: +2278/-106
[BAT] ASCII2HEX, HEX2ASCII, HEX2BIN Converter
« on: November 12, 2015, 03:01:11 PM »
Well, I needed to encode/decode some stuff and couldn't find some (sl)easy method, so...:

What I've done is quite nice! See the file "converter.bat" below, it's a self-contained record lookup tool. Instead of querying a external file, it queries itself and checks if a line inside is a record. Records are prefixed with 'R' and a <TAB>. In just a few lines of code, this script can easily lookup values for you inside itself. Basic stuff, yet useful!

Code: (converter.bat) [Select]
@echo off && echo Hexadecimal, Binary, ASCII Converter && set VALUE=%1 && if "%1"=="" set /p value=Enter value:
for /f "tokens=1-4 delims= " %%A in ('findstr /I "%VALUE%" converter.bat') do (if %%A==R echo HEX=%%B BIN=%%C ASCII=%%D)
if "%1"=="" pause
goto :EOF

R Hex Binary ASCII
R 00 00000000 NUL
R 01 00000001 SOH
R 02 00000010 STX
R 03 00000011 ETX
R 04 00000100 EOT
R 05 00000101 ENQ
R 06 00000110 ACK
R 07 00000111 BEL
R 08 00001000 BS
R 09 00001001 HT
R 0A 00001010 LF
R 0B 00001011 VT
R 0C 00001100 FF
R 0D 00001101 CR
R 0E 00001110 SO
R 0F 00001111 SI
R 10 00010000 DLE
R 11 00010001 DC1
R 12 00010010 DC2
R 13 00010011 DC3
R 14 00010100 DC4
R 15 00010101 NAK
R 16 00010110 SYN
R 17 00010111 ETB
R 18 00011000 CAN
R 19 00011001 EM
R 1A 00011010 SUB
R 1B 00011011 ESC
R 1C 00011100 FS
R 1D 00011101 GS
R 1E 00011110 RS
R 1F 00011111 US
R 20 00100000 Space
R 21 00100001 !
R 22 00100010 "
R 23 00100011 #
R 24 00100100 $
R 25 00100101 %
R 26 00100110 &
R 27 00100111 '
R 28 00101000 (
R 29 00101001 )
R 2A 00101010 *
R 2B 00101011 +
R 2C 00101100 ,
R 2D 00101101 -
R 2E 00101110 .
R 2F 00101111 /
R 30 00110000 0
R 31 00110001 1
R 32 00110010 2
R 33 00110011 3
R 34 00110100 4
R 35 00110101 5
R 36 00110110 6
R 37 00110111 7
R 38 00111000 8
R 39 00111001 9
R 3A 00111010 :
R 3B 00111011 ;
R 3C 00111100 <
R 3D 00111101 =
R 3E 00111110 >
R 3F 00111111 ?
R 40 01000000 @
R 41 01000001 A
R 42 01000010 B
R 43 01000011 C
R 44 01000100 D
R 45 01000101 E
R 46 01000110 F
R 47 01000111 G
R 48 01001000 H
R 49 01001001 I
R 4A 01001010 J
R 4B 01001011 K
R 4C 01001100 L
R 4D 01001101 M
R 4E 01001110 N
R 4F 01001111 O
R 50 01010000 P
R 51 01010001 Q
R 52 01010010 R
R 53 01010011 S
R 54 01010100 T
R 55 01010101 U
R 56 01010110 V
R 57 01010111 W
R 58 01011000 X
R 59 01011001 Y
R 5A 01011010 Z
R 5B 01011011 [
R 5C 01011100 \
R 5D 01011101 ]
R 5E 01011110 ^
R 5F 01011111 _
R 60 01100000 `
R 61 01100001 a
R 62 01100010 b
R 63 01100011 c
R 64 01100100 d
R 65 01100101 e
R 66 01100110 f
R 67 01100111 g
R 68 01101000 h
R 69 01101001 i
R 6A 01101010 j
R 6B 01101011 k
R 6C 01101100 l
R 6D 01101101 m
R 6E 01101110 n
R 6F 01101111 o
R 70 01110000 p
R 71 01110001 q
R 72 01110010 r
R 73 01110011 s
R 74 01110100 t
R 75 01110101 u
R 76 01110110 v
R 77 01110111 w
R 78 01111000 x
R 79 01111001 y
R 7A 01111010 z
R 7B 01111011 {
R 7C 01111100 |
R 7D 01111101 }
R 7E 01111110 ~
R 7F 01111111 DEL
:EOF

This works fine for converting a few values. But what if we want to parse larger values? Then, we should cut up the large value into smaller pieces and feed those pieces to the converter. Obviously we are way too lazy to manually perform these actions, so:

Code: (ASCII2HEX.BAT) [Select]
@echo off
set /p input=Enter value ASCII2HEX:
set input=%INPUT%]
:EXTRACT
set i=%input:~0,1%
for /F "tokens=1-6 delims=^= " %%a in ('converter.bat %I%') do set ASCII=%%b
set STRING=%STRING%%ASCII%
set input=%input:~1%
if "%input%"=="]" goto :DONE
goto :EXTRACT
:DONE
echo Decoded: %STRING%
:EOF

Alright! This does the job very well with the least effort :)

Now, the same code from above again, but with some commentary to explain what's going on:

Code: (ASCII2HEX.bat extended) [Select]
echo off

set /p input=Enter value ASCII2HEX:

REM Add terminator charactor for input
set input=%INPUT%]

:EXTRACT

REM Extract the first characters
set i=%input:~0,1%

REM GET ASCII VALUE
for /F "tokens=1-6 delims=^= " %%a in ('converter.bat %I%') do set ASCII=%%b
echo %ASCII%
set STRING=%STRING%%ASCII%

REM Remove first characters from input
set input=%input:~1%

REM End MSG Termination
if "%input%"=="]" goto :DONE

goto :EXTRACT

:DONE
echo Decoded: %STRING%
echo Done.

:EOF

This way, we can easily convert between whatever is inside the table that's in the converter. By pairing groups of hexadecimal values and feeding them to the converter, we can easily batch convert values.

Here are some other examples of this batch code:

Code: (HEX2ASCII.BAT) [Select]
echo off
set /p input=Enter value HEX2ASCII:

set input=%INPUT%]]

:EXTRACT

REM Extract the first characters
set i=%input:~0,2%

REM GET ASCII VALUE
for /F "tokens=1-6 delims=^= " %%a in ('converter.bat %I%') do set ASCII=%%f
echo %ASCII%
set STRING=%STRING%%ASCII%

REM Remove first characters from input
set input=%input:~2%

REM End MSG Termination
if "%input%"=="]]" goto :DONE

goto :EXTRACT

:DONE
echo Decoded: %STRING%
echo Done.

:EOF

Code: (HEX2BIN.BAT) [Select]
echo off
set /p input=Enter value HEX2BIN:

set input=%INPUT%]]

:EXTRACT

REM Extract the first characters
set i=%input:~0,2%

REM GET ASCII VALUE
for /F "tokens=1-4 delims=^= " %%a in ('converter.bat %I%') do set ASCII=%%d
echo %ASCII%
set STRING=%STRING%%ASCII%

REM Remove first characters from input
set input=%input:~2%

REM End MSG Termination
if "%input%"=="]]" goto :DONE

goto :EXTRACT

:DONE
echo Decoded: %STRING%
echo Done.

:EOF

Enjoy

HcH
« Last Edit: November 12, 2015, 03:39:07 PM by HardcoreHacker »

Offline The Magician

  • Site Donor
  • Newbie
  • *
  • Thank You
  • -Given: 8
  • -Received: 4
  • Posts: 17
  • Karma: +4/-0
  • I want to help peoples
  • Tablet / Size / Model: A13
Re: [BAT] ASCII2HEX, HEX2ASCII, HEX2BIN Converter
« Reply #1 on: November 13, 2015, 04:38:52 PM »
Very nice!  :cool-android:

Offline cyberwind

  • Site Donor
  • Newbie
  • *
  • Thank You
  • -Given: 0
  • -Received: 12
  • Posts: 25
  • Karma: +12/-0
  • Uber-Noobie
  • Tablet / Size / Model: homemade/7/galaxy5
Re: [BAT] ASCII2HEX, HEX2ASCII, HEX2BIN Converter
« Reply #2 on: November 20, 2015, 10:01:34 PM »
OMG!
This completely is Fantastic ! This is best site ever when it comes to sharing of information and the talent on the board should be considered as an International Think Tank of  Creative and Technical Development . In life no one ever thanks people for kindness and knowledge they have given. Only in years people remember how helpful certain exposer to Knowledge has guided them in life.  This will be on of those sites.

Of Course there is the whole p*ssing off a couple countries due to Malware built in tablets. So let me tell you how to resolve the issue. Just take the countries off any travel plans and there ya go! Some Countries have no sense of humor.

Thanks Everyone!

Offline pechochina

  • Newbie
  • *
  • Thank You
  • -Given: 0
  • -Received: 1
  • Posts: 4
  • Karma: +1/-0
  • Uber-Noobie
Re: [BAT] ASCII2HEX, HEX2ASCII, HEX2BIN Converter
« Reply #3 on: October 12, 2016, 01:54:39 PM »
This is not working for me. I try to convert the word g100 to hex and i get:

Enter value ASCII2HEX: g100
Decoded: 67 7F 7F 7F

actually the result must be: 67 31 30 30

do you have any idea? thank you

Offline HardcoreHacker

  • TechKnow Owner
  • Administrator
  • Uber Member
  • *****
  • Thank You
  • -Given: 3697
  • -Received: 1128
  • Posts: 9624
  • Karma: +2278/-106
Re: [BAT] ASCII2HEX, HEX2ASCII, HEX2BIN Converter
« Reply #4 on: October 12, 2016, 02:30:28 PM »
This is not working for me. I try to convert the word g100 to hex and i get:

Enter value ASCII2HEX: g100
Decoded: 67 7F 7F 7F

actually the result must be: 67 31 30 30

do you have any idea? thank you
Oh yea that's a older version, here's a better one:

Code: (ASCII2HEXv2.bat) [Select]
@echo off
setlocal enabledelayedexpansion
color 0a
set DEBUG=0
if %DEBUG%==1 echo on
set string=
set /p input=Enter value ASCII2HEXv2:

set input=%INPUT%]

:EXTRACT

REM Extract the first characters
set i=%input:~0,1%

REM GET ASCII VALUE

for /F "tokens=1,2,3,4,5,6 delims=;" %%a in (ASCII2HEXv2.bat) do if /i ";%%d"==";%i%" set ASCII=%%b
if %DEBUG%==1 echo ascii=%ASCII%
if %DEBUG%==1 echo string1=%STRING%
if not %i%==00 set STRING=%STRING%%ASCII%
if %DEBUG%==1 echo string2=%STRING%

REM Remove first characters from input
set input=%input:~1%

REM End MSG Termination
if "%input%"=="]" goto :DONE

goto :EXTRACT

:DONE
echo Decoded: %STRING%
echo Done.
pause
goto :EOF
R;00;00000000;
R;20;00100000;#
R;21;00100001;!
R;22;00100010;"
X;23;00100011;#
R;24;00100100;$
R;25;00100101;%
R;26;00100110;&
R;27;00100111;'
R;28;00101000;(
R;29;00101001;)
R;2B;00101011;+
R;2C;00101100;,
R;2D;00101101;-
R;2E;00101110;.
R;2F;00101111;/
R;30;00110000;0
R;31;00110001;1
R;32;00110010;2
R;33;00110011;3
R;34;00110100;4
R;35;00110101;5
R;36;00110110;6
R;37;00110111;7
R;38;00111000;8
R;39;00111001;9
R;3A;00111010;:
R;3B;00111011;;
R;40;01000000;@
R;41;01000001;A
R;42;01000010;B
R;43;01000011;C
R;44;01000100;D
R;45;01000101;E
R;46;01000110;F
R;47;01000111;G
R;48;01001000;H
R;49;01001001;I
R;4A;01001010;J
R;4B;01001011;K
R;4C;01001100;L
R;4D;01001101;M
R;4E;01001110;N
R;4F;01001111;O
R;50;01010000;P
R;51;01010001;Q
R;52;01010010;R
R;53;01010011;S
R;54;01010100;T
R;55;01010101;U
R;56;01010110;V
R;57;01010111;W
R;58;01011000;X
R;59;01011001;Y
R;5A;01011010;Z
R;5B;01011011;[
R;5C;01011100;\
R;5D;01011101;]
R;5F;01011111;_
R;61;01100001;a
R;62;01100010;b
R;63;01100011;c
R;64;01100100;d
R;65;01100101;e
R;66;01100110;f
R;67;01100111;g
R;68;01101000;h
R;69;01101001;i
R;6A;01101010;j
R;6B;01101011;k
R;6C;01101100;l
R;6D;01101101;m
R;6E;01101110;n
R;6F;01101111;o
R;70;01110000;p
R;71;01110001;q
R;72;01110010;r
R;73;01110011;s
R;74;01110100;t
R;75;01110101;u
R;76;01110110;v
R;77;01110111;w
R;78;01111000;x
R;79;01111001;y
R;7A;01111010;z
R;7B;01111011;{
R;7D;01111101;}
R;7E;01111110;~
:EOF

tested with 'g100', outputs correctly! and this version of the script doesn't require converter.bat to be present, it's just a little bit nicer.

Note that if you change the filename you need to change the lookup location in the script.

HcH

Offline pechochina

  • Newbie
  • *
  • Thank You
  • -Given: 0
  • -Received: 1
  • Posts: 4
  • Karma: +1/-0
  • Uber-Noobie
Re: [BAT] ASCII2HEX, HEX2ASCII, HEX2BIN Converter
« Reply #5 on: October 12, 2016, 04:02:13 PM »
Super!! It works ;-)

Offline HardcoreHacker

  • TechKnow Owner
  • Administrator
  • Uber Member
  • *****
  • Thank You
  • -Given: 3697
  • -Received: 1128
  • Posts: 9624
  • Karma: +2278/-106
Re: [BAT] ASCII2HEX, HEX2ASCII, HEX2BIN Converter
« Reply #6 on: October 12, 2016, 04:47:37 PM »
Super!! It works ;-)
I'm glad to read it works.

Thanks for the feedback!

Cheers,

HcH

Offline pechochina

  • Newbie
  • *
  • Thank You
  • -Given: 0
  • -Received: 1
  • Posts: 4
  • Karma: +1/-0
  • Uber-Noobie
Re: [BAT] ASCII2HEX, HEX2ASCII, HEX2BIN Converter
« Reply #7 on: October 12, 2016, 07:50:57 PM »
Thank you

Offline pechochina

  • Newbie
  • *
  • Thank You
  • -Given: 0
  • -Received: 1
  • Posts: 4
  • Karma: +1/-0
  • Uber-Noobie
Re: [BAT] ASCII2HEX, HEX2ASCII, HEX2BIN Converter
« Reply #8 on: January 23, 2017, 04:30:57 PM »
Hello I'm again. I Need to convert some extended ASCII into Hex. i.e. å

if I add the line:

R;E5;11100101;å

i don't find the character. do you have any idea?

thank you

Offline HardcoreHacker

  • TechKnow Owner
  • Administrator
  • Uber Member
  • *****
  • Thank You
  • -Given: 3697
  • -Received: 1128
  • Posts: 9624
  • Karma: +2278/-106
Re: [BAT] ASCII2HEX, HEX2ASCII, HEX2BIN Converter
« Reply #9 on: February 23, 2017, 10:08:28 AM »
That would require us to change codepage of the console:

Run "chcp 65001" (change codepage) to get the console in UTF-8

WARNING! Because this is a text file, be sure to save the text that you use to lookup the character as UTF-8 format!

 

Powered by EzPortal