Update:

バッチ処理サンプル(その1)「文字列/10進数2進数変換他」

Windowsの標準コマンドで作成したプログラム(?)達です。

Windows 2000、XPで使えるようになった拡張機能を利用しています。
たいした処理はしていませんが、何かの参考や役に立てば幸いです。
※Windows 9x/meでは利用できません。ご了承ください。

文字列操作

文字列操作の基本パターンです。
コマンドプロンプトで使えると便利そうな文字列の長さと文字列の位置検索をサブルーチンにしてみました。
文字列の抽出と置換は環境変数の拡張機能でまかないます。

@title 文字列の操作
@echo off

set str=CommandLine Interface コマンドライン
echo set str=%str%

echo.
echo 文字列の長さ
echo call :strlen %str%
call :strlen "%str%"
echo str=%strlen%

echo.
echo 文字列の検索
echo call :strpos "%str%" "コマンド"
call :strpos "%str%" "コマンド"
echo match=%strpos%

echo.
echo 文字列の抽出
echo str=%str%
echo set substr=%str:~22,4% (%は半角で入力します。)
set substr=%str:~22,4%
echo substr=%substr%

echo.
echo 文字列の置き換え
echo %str:and= & % (%は半角で入力します。)
set str=%str:and= & %
echo str=%str%

echo.
echo on
@exit /b

REM----------------ここからサブルーチン
rem 文字列の長さを求めます。
:strlen
  rem echo on
  set strlen_string=%~1
  set strlen=0
  if "%strlen_string%" == "" goto :EOF
  :strlen_while
    call set ch=%%strlen_string:~%strlen%,1%%
    if "%ch%" == "" goto strlen_wend
    set /a strlen+=1
    goto strlen_while
  :strlen_wend
  rem @echo off
goto :EOF

rem 文字列の位置を求めます。
:strpos
  set source=%~2
  set source_len=0
  :strpos_source_len_while
    call set ch=%%source:~%source_len%,1%%
    if "%ch%" == "" goto strpos_source_len_wend
    set /a source_len+=1
    goto strpos_source_len_while
  :strpos_source_len_wend
  set target=%~1
  set pos=0
  call set wk=%%source:~0,1%%
  :compare_ch
    call set ch=%%target:~%pos%,1%%
    if "%ch%" == "" set pos=0&goto compare_ch_end
    if not "%ch%" == "%wk%" goto compare_ch_next
      call set substring=%%target:~%pos%,%source_len%%%
      if "%substring%" == "%source%" goto compare_ch_end
    :compare_ch_next
    set /a pos+=1
    goto compare_ch
  :compare_ch_end
  set strpos=%pos%
goto :EOF


ページトップへ戻る

文字種のチェック

コマンド引数の文字種をチェックします。
多バイト系文字(2バイト以上)のチェックは符号体系が複雑なので対象外とします。
半角英数字および一部の記号のみチェックをかけられます。

@title 文字種のチェック
@echo off
rem コマンド引数の受け取り
set arg1=%1
if "%1" == "" set /p arg1=引数1:
if "%arg1%" == "" goto help
if "%arg1%" == "/?" goto help

rem Shiftコマンドを使えば10個以上の引数を扱うこともできます。
set prev_arg=%arg1%
for /L %%I in (2, 1, 9) do call :set_arg %%I %%%%I
rem for /L %%I in (1, 1, 9) do call echo :set_arg %%I %%%%I

rem ここから引数のチェック

rem 数字チェック
for /L %%I in (1, 1, 9) do set chk_num[%%I]=
for /L %%I in (1, 1, 9) do call :chk_num_main %%I %%arg%%I%%

rem 英字チェック
for /L %%I in (1, 1, 9) do set chk_alph[%%I]=
for /L %%I in (1, 1, 9) do call :chk_alph_main %%I %%arg%%I%%

rem 英数字チェック
for /L %%I in (1, 1, 9) do set chk_alphnum[%%I]=
for /L %%I in (1, 1, 9) do call :chk_alphnum_main %%I %%arg%%I%%

rem 半角文字列チェック
rem CALLコマンドを使っているため記号「<>|&」はチェックできません。
rem コマンド引数の処理の関係上、記号「"= 」はチェックできません。
rem 環境変数の処理の関係上、記号「%」はチェックできません。
for /L %%I in (1, 1, 9) do set chk_bytestr[%%I]=
for /L %%I in (1, 1, 9) do call :chk_bytestr_main %%I %%arg%%I%%

echo.
echo 引数のリスト
for /L %%I in (1, 1, 9) do call :arg_lst %%I %%arg%%I%%

echo.
echo on
exit /b

REM----------------ここからサブルーチン
:set_arg
  if "%prev_arg%" == "" set arg%1=&goto :EOF
  rem echo arg%1=%2
  set arg%1=%2
  if "%2" == "" set /p arg%1=引数%1:
  call set prev_arg=%%arg%1%%
goto :EOF

:arg_lst
  if "%2" == "" goto :EOF
  call echo arg%1="%2" num=%%chk_num[%1]%% alph=%%chk_alph[%1]%% alphnum=%%chk_alphnum[%1]%% bytestr=%%chk_bytestr[%1]%%
goto :EOF

:chk_num_main
  call :chk_num %2
  set chk_num[%1]=%chk_num%
goto :EOF
:chk_num
  set string=%~1
  if "%string%" == "" set /a chk_num=0&goto :EOF
  if "%string:~0,1%" == "-" set string=%string:~1%
  set /a chk_num=1
  set dpsw=0
  set pos=0
  :chk_num_while01
    call set ch=%%string:~%pos%,1%%
    rem call echo set ch=%%string:~%pos%,1%%
    if "%ch%" == "" goto chk_num_wend01
    if "%ch%" == "0" goto chk_num_next01
    if "%ch%" == "1" goto chk_num_next01
    if "%ch%" == "2" goto chk_num_next01
    if "%ch%" == "3" goto chk_num_next01
    if "%ch%" == "4" goto chk_num_next01
    if "%ch%" == "5" goto chk_num_next01
    if "%ch%" == "6" goto chk_num_next01
    if "%ch%" == "7" goto chk_num_next01
    if "%ch%" == "8" goto chk_num_next01
    if "%ch%" == "9" goto chk_num_next01
    if "%ch%" == "." goto chk_num_dp01
    set /a chk_num=0
    goto chk_num_wend01
    :chk_num_dp01
      if %dpsw% EQU 1 set /a chk_num=0&goto :EOF
      set dpsw=1
    :chk_num_next01
    set /a pos+=1
    goto chk_num_while01
  :chk_num_wend01
goto :EOF

:chk_alph_main
  call :chk_alph %2
  set chk_alph[%1]=%chk_alph%
goto :EOF
:chk_alph
  set string=%~1
  set /a chk_alph=1
  if "%string%" == "" set /a chk_alph=0&goto :EOF
  set pos=0
  :chk_alph_while01
    call set ch=%%string:~%pos%,1%%
    rem call echo set ch=%%string:~%pos%,1%%
    if "%ch%" == "" goto chk_alph_wend01
    if "%ch%" == "a" goto chk_alph_next01
    if "%ch%" == "b" goto chk_alph_next01
    if "%ch%" == "c" goto chk_alph_next01
    if "%ch%" == "d" goto chk_alph_next01
    if "%ch%" == "e" goto chk_alph_next01
    if "%ch%" == "f" goto chk_alph_next01
    if "%ch%" == "g" goto chk_alph_next01
    if "%ch%" == "h" goto chk_alph_next01
    if "%ch%" == "i" goto chk_alph_next01
    if "%ch%" == "j" goto chk_alph_next01
    if "%ch%" == "k" goto chk_alph_next01
    if "%ch%" == "l" goto chk_alph_next01
    if "%ch%" == "m" goto chk_alph_next01
    if "%ch%" == "n" goto chk_alph_next01
    if "%ch%" == "o" goto chk_alph_next01
    if "%ch%" == "p" goto chk_alph_next01
    if "%ch%" == "q" goto chk_alph_next01
    if "%ch%" == "r" goto chk_alph_next01
    if "%ch%" == "s" goto chk_alph_next01
    if "%ch%" == "t" goto chk_alph_next01
    if "%ch%" == "u" goto chk_alph_next01
    if "%ch%" == "v" goto chk_alph_next01
    if "%ch%" == "w" goto chk_alph_next01
    if "%ch%" == "x" goto chk_alph_next01
    if "%ch%" == "y" goto chk_alph_next01
    if "%ch%" == "z" goto chk_alph_next01
    if "%ch%" == "A" goto chk_alph_next01
    if "%ch%" == "B" goto chk_alph_next01
    if "%ch%" == "C" goto chk_alph_next01
    if "%ch%" == "D" goto chk_alph_next01
    if "%ch%" == "E" goto chk_alph_next01
    if "%ch%" == "F" goto chk_alph_next01
    if "%ch%" == "G" goto chk_alph_next01
    if "%ch%" == "H" goto chk_alph_next01
    if "%ch%" == "I" goto chk_alph_next01
    if "%ch%" == "J" goto chk_alph_next01
    if "%ch%" == "K" goto chk_alph_next01
    if "%ch%" == "L" goto chk_alph_next01
    if "%ch%" == "M" goto chk_alph_next01
    if "%ch%" == "N" goto chk_alph_next01
    if "%ch%" == "O" goto chk_alph_next01
    if "%ch%" == "P" goto chk_alph_next01
    if "%ch%" == "Q" goto chk_alph_next01
    if "%ch%" == "R" goto chk_alph_next01
    if "%ch%" == "S" goto chk_alph_next01
    if "%ch%" == "T" goto chk_alph_next01
    if "%ch%" == "U" goto chk_alph_next01
    if "%ch%" == "V" goto chk_alph_next01
    if "%ch%" == "W" goto chk_alph_next01
    if "%ch%" == "X" goto chk_alph_next01
    if "%ch%" == "Y" goto chk_alph_next01
    if "%ch%" == "Z" goto chk_alph_next01
    set /a chk_alph=0
    goto chk_alph_wend01
    :chk_alph_next01
    set /a pos+=1
  goto chk_alph_while01
  :chk_alph_wend01
goto :EOF

:chk_alphnum_main
  call :chk_alphnum %2
  set chk_alphnum[%1]=%chk_alphnum%
goto :EOF
:chk_alphnum
  set string=%~1
  set /a chk_alphnum=1
  if "%string%" == "" set /a chk_alphnum=0&goto :EOF
  set pos=0
  :chk_alphnum_while01
    call set ch=%%string:~%pos%,1%%
    rem call echo set ch=%%string:~%pos%,1%%
    if "%ch%" == "" goto chk_alphnum_wend01
    if "%ch%" == "a" goto chk_alphnum_next01
    if "%ch%" == "b" goto chk_alphnum_next01
    if "%ch%" == "c" goto chk_alphnum_next01
    if "%ch%" == "d" goto chk_alphnum_next01
    if "%ch%" == "e" goto chk_alphnum_next01
    if "%ch%" == "f" goto chk_alphnum_next01
    if "%ch%" == "g" goto chk_alphnum_next01
    if "%ch%" == "h" goto chk_alphnum_next01
    if "%ch%" == "i" goto chk_alphnum_next01
    if "%ch%" == "j" goto chk_alphnum_next01
    if "%ch%" == "k" goto chk_alphnum_next01
    if "%ch%" == "l" goto chk_alphnum_next01
    if "%ch%" == "m" goto chk_alphnum_next01
    if "%ch%" == "n" goto chk_alphnum_next01
    if "%ch%" == "o" goto chk_alphnum_next01
    if "%ch%" == "p" goto chk_alphnum_next01
    if "%ch%" == "q" goto chk_alphnum_next01
    if "%ch%" == "r" goto chk_alphnum_next01
    if "%ch%" == "s" goto chk_alphnum_next01
    if "%ch%" == "t" goto chk_alphnum_next01
    if "%ch%" == "u" goto chk_alphnum_next01
    if "%ch%" == "v" goto chk_alphnum_next01
    if "%ch%" == "w" goto chk_alphnum_next01
    if "%ch%" == "x" goto chk_alphnum_next01
    if "%ch%" == "y" goto chk_alphnum_next01
    if "%ch%" == "z" goto chk_alphnum_next01
    if "%ch%" == "A" goto chk_alphnum_next01
    if "%ch%" == "B" goto chk_alphnum_next01
    if "%ch%" == "C" goto chk_alphnum_next01
    if "%ch%" == "D" goto chk_alphnum_next01
    if "%ch%" == "E" goto chk_alphnum_next01
    if "%ch%" == "F" goto chk_alphnum_next01
    if "%ch%" == "G" goto chk_alphnum_next01
    if "%ch%" == "H" goto chk_alphnum_next01
    if "%ch%" == "I" goto chk_alphnum_next01
    if "%ch%" == "J" goto chk_alphnum_next01
    if "%ch%" == "K" goto chk_alphnum_next01
    if "%ch%" == "L" goto chk_alphnum_next01
    if "%ch%" == "M" goto chk_alphnum_next01
    if "%ch%" == "N" goto chk_alphnum_next01
    if "%ch%" == "O" goto chk_alphnum_next01
    if "%ch%" == "P" goto chk_alphnum_next01
    if "%ch%" == "Q" goto chk_alphnum_next01
    if "%ch%" == "R" goto chk_alphnum_next01
    if "%ch%" == "S" goto chk_alphnum_next01
    if "%ch%" == "T" goto chk_alphnum_next01
    if "%ch%" == "U" goto chk_alphnum_next01
    if "%ch%" == "V" goto chk_alphnum_next01
    if "%ch%" == "W" goto chk_alphnum_next01
    if "%ch%" == "X" goto chk_alphnum_next01
    if "%ch%" == "Y" goto chk_alphnum_next01
    if "%ch%" == "Z" goto chk_alphnum_next01
    if "%ch%" == "0" goto chk_alphnum_next01
    if "%ch%" == "1" goto chk_alphnum_next01
    if "%ch%" == "2" goto chk_alphnum_next01
    if "%ch%" == "3" goto chk_alphnum_next01
    if "%ch%" == "4" goto chk_alphnum_next01
    if "%ch%" == "5" goto chk_alphnum_next01
    if "%ch%" == "6" goto chk_alphnum_next01
    if "%ch%" == "7" goto chk_alphnum_next01
    if "%ch%" == "8" goto chk_alphnum_next01
    if "%ch%" == "9" goto chk_alphnum_next01
    set /a chk_alphnum=0
    goto chk_alphnum_wend01
    :chk_alphnum_next01
    set /a pos+=1
    goto chk_alphnum_while01
  :chk_alphnum_wend01
goto :EOF

:chk_bytestr_main
  call :chk_bytestr %2
  set chk_bytestr[%1]=%chk_bytestr%
goto :EOF
:chk_bytestr
  set string=%~1
  set /a chk_bytestr=1
  if "%string%" == "" set /a chk_bytestr=0&goto :EOF
  set pos=0
  :chk_bytestr_while01
    call set ch=%%string:~%pos%,1%%
    rem if "^%ch%^" == "^"^" set ch=^"^
    rem call echo set ch=%ch%
    if "%ch%" == "" goto chk_bytestr_wend01
    if "%ch%" == "a" goto chk_bytestr_next01
    if "%ch%" == "b" goto chk_bytestr_next01
    if "%ch%" == "c" goto chk_bytestr_next01
    if "%ch%" == "d" goto chk_bytestr_next01
    if "%ch%" == "e" goto chk_bytestr_next01
    if "%ch%" == "f" goto chk_bytestr_next01
    if "%ch%" == "g" goto chk_bytestr_next01
    if "%ch%" == "h" goto chk_bytestr_next01
    if "%ch%" == "i" goto chk_bytestr_next01
    if "%ch%" == "j" goto chk_bytestr_next01
    if "%ch%" == "k" goto chk_bytestr_next01
    if "%ch%" == "l" goto chk_bytestr_next01
    if "%ch%" == "m" goto chk_bytestr_next01
    if "%ch%" == "n" goto chk_bytestr_next01
    if "%ch%" == "o" goto chk_bytestr_next01
    if "%ch%" == "p" goto chk_bytestr_next01
    if "%ch%" == "q" goto chk_bytestr_next01
    if "%ch%" == "r" goto chk_bytestr_next01
    if "%ch%" == "s" goto chk_bytestr_next01
    if "%ch%" == "t" goto chk_bytestr_next01
    if "%ch%" == "u" goto chk_bytestr_next01
    if "%ch%" == "v" goto chk_bytestr_next01
    if "%ch%" == "w" goto chk_bytestr_next01
    if "%ch%" == "x" goto chk_bytestr_next01
    if "%ch%" == "y" goto chk_bytestr_next01
    if "%ch%" == "z" goto chk_bytestr_next01
    if "%ch%" == "A" goto chk_bytestr_next01
    if "%ch%" == "B" goto chk_bytestr_next01
    if "%ch%" == "C" goto chk_bytestr_next01
    if "%ch%" == "D" goto chk_bytestr_next01
    if "%ch%" == "E" goto chk_bytestr_next01
    if "%ch%" == "F" goto chk_bytestr_next01
    if "%ch%" == "G" goto chk_bytestr_next01
    if "%ch%" == "H" goto chk_bytestr_next01
    if "%ch%" == "I" goto chk_bytestr_next01
    if "%ch%" == "J" goto chk_bytestr_next01
    if "%ch%" == "K" goto chk_bytestr_next01
    if "%ch%" == "L" goto chk_bytestr_next01
    if "%ch%" == "M" goto chk_bytestr_next01
    if "%ch%" == "N" goto chk_bytestr_next01
    if "%ch%" == "O" goto chk_bytestr_next01
    if "%ch%" == "P" goto chk_bytestr_next01
    if "%ch%" == "Q" goto chk_bytestr_next01
    if "%ch%" == "R" goto chk_bytestr_next01
    if "%ch%" == "S" goto chk_bytestr_next01
    if "%ch%" == "T" goto chk_bytestr_next01
    if "%ch%" == "U" goto chk_bytestr_next01
    if "%ch%" == "V" goto chk_bytestr_next01
    if "%ch%" == "W" goto chk_bytestr_next01
    if "%ch%" == "X" goto chk_bytestr_next01
    if "%ch%" == "Y" goto chk_bytestr_next01
    if "%ch%" == "Z" goto chk_bytestr_next01
    if "%ch%" == "0" goto chk_bytestr_next01
    if "%ch%" == "1" goto chk_bytestr_next01
    if "%ch%" == "2" goto chk_bytestr_next01
    if "%ch%" == "3" goto chk_bytestr_next01
    if "%ch%" == "4" goto chk_bytestr_next01
    if "%ch%" == "5" goto chk_bytestr_next01
    if "%ch%" == "6" goto chk_bytestr_next01
    if "%ch%" == "7" goto chk_bytestr_next01
    if "%ch%" == "8" goto chk_bytestr_next01
    if "%ch%" == "9" goto chk_bytestr_next01
    if "%ch%" == "!" goto chk_bytestr_next01
    rem if "%ch%" == "^"^" goto chk_bytestr_next01
    if "%ch%" == "#" goto chk_bytestr_next01
    if "%ch%" == "$" goto chk_bytestr_next01
    if "^%ch%" == "^%" goto chk_bytestr_next01
    rem if "%ch%" == "^&" goto chk_bytestr_next01
    if "^%ch%" == "^'" goto chk_bytestr_next01
    if "%ch%" == "(" goto chk_bytestr_next01
    if "%ch%" == ")" goto chk_bytestr_next01
    rem if "%ch%" == "=" goto chk_bytestr_next01
    if "%ch%" == "~" goto chk_bytestr_next01
    rem if "%ch%" == "^|" goto chk_bytestr_next01
    if "%ch%" == "-" goto chk_bytestr_next01
    if "%ch%" == "^^" goto chk_bytestr_next01
    if "^%ch%" == "^\" goto chk_bytestr_next01
    if "%ch%" == "^`" goto chk_bytestr_next01
    if "%ch%" == "{" goto chk_bytestr_next01
    if "%ch%" == "}" goto chk_bytestr_next01
    rem if "%ch%" == "^<" goto chk_bytestr_next01
    rem if "%ch%" == "^>" goto chk_bytestr_next01
    if "%ch%" == "?" goto chk_bytestr_next01
    if "%ch%" == "_" goto chk_bytestr_next01
    if "%ch%" == "+" goto chk_bytestr_next01
    if "%ch%" == "*" goto chk_bytestr_next01
    if "%ch%" == "}" goto chk_bytestr_next01
    if "%ch%" == "[" goto chk_bytestr_next01
    if "%ch%" == "]" goto chk_bytestr_next01
    if "%ch%" == ";" goto chk_bytestr_next01
    if "%ch%" == ":" goto chk_bytestr_next01
    if "%ch%" == "," goto chk_bytestr_next01
    if "%ch%" == "." goto chk_bytestr_next01
    if "%ch%" == "/" goto chk_bytestr_next01
    if "%ch%" == "@" goto chk_bytestr_next01
    set /a chk_bytestr=0
    goto chk_bytestr_wend01
    :chk_bytestr_next01
    set /a pos+=1
    goto chk_bytestr_while01
  :chk_bytestr_wend01
goto :EOF

:help
echo --------------------------------------------------
echo %0 の使い方
echo %0 引数1 [ 引数2 [ 引数3 ・・・ [ 引数9 ] ]・・・]
echo 与えられた引数1から引数9までの文字種をチェックして表示します。
echo 値が""(空)の引数があれば、そこで表示を終了します。
echo 引数の値が数字のみであれば「num=1」をそれ以外は「num=0」となります。
echo 引数の値が英字のみであれば「alph=1」をそれ以外は「alph=0」となります。
echo 引数の値が英字と数字であれば「alphnum=1」をそれ以外は「alphnum=0」となります。
echo 引数の値が半角文字であれば「bytestr=1」をそれ以外は「bytestr=0」となります。
echo.
goto :EOF

ページトップへ戻る


10進数2進数変換

10進数を2進数に変換する処理です。
情報処理では基本的な処理ですが、コマンドプロンプトでは結構大変です。
10進数を2進数の文字列(0と1の並び)として表示します。
表示する2進数は32ビットの2の補数表現として扱っていて、負の数は32ビットで表示されますが正の数または0の場合は上位桁の0は表示していません。

@TITLE 10進数→2進数変換
@echo off
rem 数値の設定と説明画面への分岐
set dec=%~1
if "%dec%" == "" set /p dec=10進数(8桁以内)を入力してください。:
if "%dec%" == "/?" goto :help
if "%dec%" == "" goto :help
rem 変換処理へ
call :conv10-2 %dec%
rem 変換結果の表示
echo %dec%=(%bin%)2
echo on
@exit /b
rem ---------------------------------------------------------
rem 変換処理を開始します。
:conv10-2
rem 2進数で使う数字(0,1)を設定
set str=01
rem 変換結果の格納用変数
set bin=
rem 変換作業用の変数
set wk_num=%~1
rem 負の数の場合は、正の数に変えて変換します。
if %~1 LSS 0 set /a wk_num=0-wk_num
:loop_conv10-2
  rem 変換する数値が0になるまで以下を繰り返します。
  if %wk_num% LEQ 0 goto exit_loop_conv10-2
  rem 数値を2で割って余りを求める処理です。
  set /a q=wk_num / 2
  set /a r=%wk_num% - %q% * 2
  rem 余りの数を文字に変換します。
  call set ch=%%str:~%r%,1%%
  rem 結果を格納します。
  set bin=%ch%%bin%
  rem 次に変換する数を設定します。
  set wk_num=%q%
goto loop_conv10-2
rem 繰り返しの終端です。
:exit_loop_conv10-2
rem 負の数であった場合、2の補数表現に変換します。
if %~1 LSS 0 call :invert %bin%
goto :EOF

:invert
rem 数値を32桁に揃えます。
set inv=00000000000000000000000000000000%~1
set inv=%inv:~-32%
set invert=
set /a p=31
rem 桁上がりを「1」に設定する。
set carry=1
:loop_invert
  if %p% LSS 0 goto exit_loop_invert
  rem 2進数文字列の右から順に1文字を取り出します。
  call set /a i=1-%%inv:~%p%,1%%
  rem 取り出した文字は「1」か?
  if "%i%" == "1" (
    rem はい:
    rem 桁上がりが続いていれば、この桁を「0」にする。
    if "%carry%" == "1" set i=0
    rem 桁上がりが続いていないなら、何もしない。
  ) else (
    rem いいえ:
    rem 桁上がりが続いていれば、この桁を「1」にする。
    if "%carry%" == "1" set i=1
    rem 桁上がりが続いていないなら、何もしない。
    rem 桁上がりを、「0:終了」にする。
    set carry=0
  )
  rem 変換結果を格納し、次の桁位置を設定します。
  set invert=%i%%invert%
  set /a p-=1
goto loop_invert
rem 繰り返しの終端です。
:exit_loop_invert
set bin=%invert%
goto :EOF

:help
echo ------------------------------------------------------------------
echo %0 の使い方
echo %0 nnnn (nnnn:8桁以内の10進数)
echo  与えられた10進数を2進数の文字列として表示します。
echo  引数が与えられなかったときは、キーボードから入力します。
echo  キーボードから数字の入力が無かった場合は、説明画面(この画面)を表示します。
echo  入力文字のチェックは行っていないので数字以外の文字が入った場合の動作は
echo  保証がありません。
echo.
exit /b

ページトップへ戻る


10進数16進数変換

10進数を16進数に変換する処理です。
10進数を16進数の文字列(0-9,a-fの並び)として表示します。
表示する16進数は8桁の16の補数表現として扱っていて、負の数は8桁で表示されますが正の数または0の場合は上位桁の0は表示していません。

@TITLE 10進数→16進数変換
@echo off
rem 数値の設定と説明画面への分岐
set dec=%~1
if "%dec%" == "" set /p dec=10進数(8桁以内)を入力してください。:
if "%dec%" == "/?" goto :help
if "%dec%" == "" goto :help
rem 変換処理へ
call :conv10-16 %dec%
rem 変換結果の表示
echo %dec%=(%hex%)16
echo on
@exit /b
rem ---------------------------------------------------------
rem 変換処理を開始します。
:conv10-16
rem 16進数で使う数字(0,1,2,3,4,5,6,7,8,9,a,b,c,d,e,f)を設定
set str=0123456789abcdef
rem 変換結果の格納用変数
set hex=
rem 変換作業用の変数
set wk_num=%~1
rem 負の数の場合は、正の数に変えて変換します。
if %~1 LSS 0 set /a wk_num=0-wk_num
:loop_conv10-16
  rem 変換する数値が0になるまで以下を繰り返します。
  if %wk_num% LEQ 0 goto exit_loop_conv10-16
  rem 数値を16で割って余りを求める処理です。
  set /a q=wk_num / 16
  set /a r=%wk_num% - %q% * 16
  rem 余りの数を文字に変換します。
  call set ch=%%str:~%r%,1%%
  rem 結果を格納します。
  set hex=%ch%%hex%
  rem 次に変換する数を設定します。
  set wk_num=%q%
goto loop_conv10-16
rem 繰り返しの終端です。
:exit_loop_conv10-16
rem 負の数であった場合、16の補数表現に変換します。
if %~1 LSS 0 call :invert %hex%
goto :EOF

:invert
rem 16進数を10進数に変換するためのハッシュを定義します。
set hash[0]=0
set hash[1]=1
set hash[2]=2
set hash[3]=3
set hash[4]=4
set hash[5]=5
set hash[6]=6
set hash[7]=7
set hash[8]=8
set hash[9]=9
set hash[a]=10
set hash[b]=11
set hash[c]=12
set hash[d]=13
set hash[e]=14
set hash[f]=15
rem 数値を8桁に揃えます。
set inv=00000000%~1
set inv=%inv:~-8%
set invert=
set up=0123456789abcdef
set /a p=7
rem 桁上がりを「1」に設定します。
set carry=1
:loop_invert
  if %p% LSS 0 goto exit_loop_invert
  rem 16進数文字列の右から順に1文字を取り出し10進数に変換します。
  call set j=%%inv:~%p%,1%%
  call set /a i=15 - %%hash[%j%]%%
  rem 取り出した数字は15か?
  if %i% EQU 15 (
    rem はい:
    if "%carry%" == "1" (
      rem 桁上がりが続いていれば、この桁を「0」にします。
      set i=0
    ) else (
      rem 桁上がりが続いていないなら、16進数に変換します。
      call set i=%%up:~%i%,1%%
    )
  ) else (
    rem いいえ:
    rem 桁上がりが続いていれば、この桁をi+1にします。
    if "%carry%" == "1" set /a i+=1
    rem 16進数に変換します。
    call set i=%%up:~%i%,1%%
    rem 桁上がりを、「0:終了」にします。
    set carry=0
  )
  rem 変換結果を格納し、次の桁位置を設定します。
  set invert=%i%%invert%
  set /a p-=1
goto loop_invert
rem 繰り返しの終端です。
:exit_loop_invert
set hex=%invert%
goto :EOF

:help
echo ------------------------------------------------------------------
echo %0 の使い方
echo %0 nnnn (nnnn:8桁以内の10進数)
echo  与えられた10進数を16進数の文字列として表示します。
echo  引数が与えられなかったときは、キーボードから入力します。
echo  キーボードから数字の入力が無かった場合は、説明画面(この画面)を表示します。
echo  入力文字のチェックは行っていないので数字以外の文字が入った場合の動作は
echo  保証がありません。
echo.
exit /b

ページトップへ戻る


16進数10進数変換

16進数を10進数に変換する処理です。
16進数の文字列(0-9,a-fの並び)を10進数に変換して表示します。
入力する16進数は8桁の16の補数表現として扱っていますので(7fffffff)16を超えた数、(80000000)以上の数は負の数として表示されます。


@TITLE 16進数→10進数変換
@echo off
rem 数値の設定と説明画面への分岐
set hex=%~1
if "%hex%" == "" set /p hex=16進数(8桁以内)を入力してください。:
if "%hex%" == "/?" goto :help
if "%hex%" == "" goto :help
rem 変換処理へ
call :conv16-10 %hex%
rem 変換結果の表示
echo (%hex%)16=%dec%
echo on
@exit /b
rem ---------------------------------------------------------
rem 変換処理を開始します。
:conv16-10
rem 16進数を10進数に変換するためのハッシュを定義します。
set hash[0]=0
set hash[1]=1
set hash[2]=2
set hash[3]=3
set hash[4]=4
set hash[5]=5
set hash[6]=6
set hash[7]=7
set hash[8]=8
set hash[9]=9
set hash[a]=10
set hash[b]=11
set hash[c]=12
set hash[d]=13
set hash[e]=14
set hash[f]=15
rem 変換結果の格納用変数
set dec=0
rem 変換作業用の変数
set wk_num=%~1
set wk_num=00000000%wk_num%
set wk_num=%wk_num:~-8%
set p=0
:loop_conv16-10
  rem 変換する数値が0になるまで以下を繰り返します。
  if %p% GEQ 8 goto exit_loop_conv16-10
  rem 16進数文字列の左から順に1文字を取り出し10進数に変換します。
  call set j=%%wk_num:~%p%,1%%
  call set /a i=%%hash[%j%]%%
  rem 数値を16倍して10進数に変換する処理です。
  set /a dec=dec * 16 + i
  rem 次に変換する桁位置を設定します。
  set /a p+=1
goto loop_conv16-10
rem 繰り返しの終端です。
:exit_loop_conv16-10
goto :EOF

:help
echo ------------------------------------------------------------------
echo %0 の使い方
echo %0 xxxx (xxxx:8桁以内の16進数=0-9,a-fの並び)
echo  与えられた16進数を10進数の文字列として表示します。
echo  16進数は8桁の補数形式で与えられるものとして変換処理を行います。
echo  (つまり、ffffffff は10進数の「-1」を表すものとします。)
echo  引数が与えられなかったときは、キーボードから入力します。
echo  キーボードから数字の入力が無かった場合は、説明画面(この画面)を表示します。
echo  入力文字のチェックは行っていないので数字以外の文字が入った場合の動作は
echo  保証がありません。
echo.
exit /b

※ちなみに、純粋に16進数を10進数に直したいだけなら、コマンドプロンプトで
「set /a x=0x13579bdf(ENTER)」のように16進数を入力すれば
10進数で「324508639」という表示を得られます。

ページトップへ戻る


【ページ内見出し】
【サイト内リンク】

−広告エリア−



元のページに戻る  ページを閉じる

連絡先:お問い合わせフォーム