<?xml version="1.0" encoding="utf-8"?>
<feed xmlns="http://www.w3.org/2005/Atom">
	<title type="html"><![CDATA[Серый форум &mdash; CMD/BAT: Преобразование системы счисления]]></title>
	<link rel="self" href="https://forum.script-coding.com/extern.php?action=feed&amp;tid=11607&amp;type=atom" />
	<updated>2016-05-20T09:39:31Z</updated>
	<generator>PunBB</generator>
	<id>https://forum.script-coding.com/viewtopic.php?id=11607</id>
		<entry>
			<title type="html"><![CDATA[CMD/BAT: Преобразование системы счисления]]></title>
			<link rel="alternate" href="https://forum.script-coding.com/viewtopic.php?pid=103971#p103971" />
			<content type="html"><![CDATA[<p>Код : </p><div class="codebox"><pre><code>@echo off
setlocal enabledelayedexpansion
if /i &quot;%1&quot;==&quot;-d2h&quot; Call :dec2hex %2 result
if /i &quot;%1&quot;==&quot;-h2d&quot; Call :hex2dec %2 result
if &quot;%3&quot;==&quot;&quot; (echo !result!&amp;endlocal&amp;goto :eof) else (endlocal&amp;set %3=%result%)
exit /b

:hex2dec
setlocal
set txt=%1
Call :strlen len %1
set min=0
set /a &quot;max=0&quot;
set /a &quot;bstr=len - 1&quot;
set long[0]=0
for /l %%i in (0,1,!bstr!) do (
	for /l %%a in (!min!,1,!max!) do set /a long[%%a]*=16
	Call :verifyDecVal
	set /a long[!min!]+=0x!txt:~%%i,1!
)
Call :verifyDecVal
set q=
for /l %%i in (!max!,-1,!min!) do (
	set t=0000!long[%%i]!
	set q=!q!!t:~-4!
)
endlocal&amp;set result=%q%
goto :eof

:verifyDecVal
set again=false
for /l %%a in (!min!,1,!max!) do (
	if &quot;%%a&quot;==&quot;!max!&quot; (
		if !long[%%a]! geq 10000 (
			set again=true
			set /a max+=1
			set /a &quot;long[!max!]=long[%%a] / 10000&quot;
			set /a &quot;long[%%a]=long[%%a] %% 10000&quot;
		)
	)
)
set /a &quot;mmax=max - 1&quot;
for /l %%i in (!min!,1,!mmax!) do (
	set /a next=%%i + 1
	if !long[%%i]! geq 10000 (
		set again=true
		set /a &quot;long[!next!]+=long[%%i] / 10000&quot;
		set /a &quot;long[%%i]=long[%%i] %% 10000&quot;
	)
)
if !again!==true goto verifyDecVal
goto :eof

:dec2hex
setlocal
set txt=%1
Call :strlen len %1
set min=0
set /a &quot;max=0&quot;
set /a &quot;bstr=len - 1&quot;
set long[0]=0
for /l %%i in (0,1,!bstr!) do (
	for /l %%a in (!min!,1,!max!) do set /a long[%%a]*=10
	Call :verifyHexVal
	set /a long[!min!]+=!txt:~%%i,1!
)
Call :verifyHexVal
set result=
for /l %%i in (!max!,-1,!min!) do (
	Call :toHex !long[%%i]! q
	set result=!result!!q:~-4!
)
endlocal&amp;set result=%result%
goto :eof

:verifyHexVal
set again=false
for /l %%a in (!min!,1,!max!) do (
	if &quot;%%a&quot;==&quot;!max!&quot; (
		if !long[%%a]! geq 65536 (
			set again=true
			set /a max+=1
			set /a &quot;long[!max!]=long[%%a] &gt;&gt; 16&quot;
			set /a &quot;long[%%a]=long[%%a] &amp; 0xFFFF&quot;
		)
	)
)
set /a &quot;mmax=max - 1&quot;
for /l %%i in (!min!,1,!mmax!) do (
	set /a next=%%i + 1
	if !long[%%i]! geq 65536 (
		set again=true
		set /a &quot;long[!next!]+=long[%%i] &gt;&gt; 16&quot;
		set /a &quot;long[%%i]=long[%%i] &amp; 0xFFFF&quot;
	)
)
if !again!==true goto verifyHexVal
goto :eof

:strlen &lt;resultVar&gt; &lt;stringVar&gt;
(   
    setlocal EnableDelayedExpansion
    set &quot;s=%~2#&quot;
    set &quot;len=0&quot;
    for %%P in (4096 2048 1024 512 256 128 64 32 16 8 4 2 1) do (
        if &quot;!s:~%%P,1!&quot; NEQ &quot;&quot; ( 
            set /a &quot;len+=%%P&quot;
            set &quot;s=!s:~%%P!&quot;
        )
    )
)
( 
    endlocal
    set &quot;%~1=%len%&quot;
    exit /b
)

:toHex dec hex -- convert a decimal number to hexadecimal, i.e. -20 to FFFFFFEC or 26 to 0000001A
::             -- dec [in]      - decimal number to convert
::             -- hex [out,opt] - variable to store the converted hexadecimal number in
::Thanks to &#039;dbenham&#039; dostips forum users who inspired to improve this function
:$created 20091203 :$changed 20110330 :$categories Arithmetic,Encoding
:$source http://www.dostips.com
SETLOCAL ENABLEDELAYEDEXPANSION
set /a dec=%~1
set &quot;hex=&quot;
set &quot;map=0123456789ABCDEF&quot;
for /L %%N in (1,1,8) do (
    set /a &quot;d=dec&amp;15,dec&gt;&gt;=4&quot;
    for %%D in (!d!) do set &quot;hex=!map:~%%D,1!!hex!&quot;
)
rem !!!! REMOVE LEADING ZEROS by activating the next line, e.g. will return 1A instead of 0000001A
rem for /f &quot;tokens=* delims=0&quot; %%A in (&quot;%hex%&quot;) do set &quot;hex=%%A&quot;&amp;if not defined hex set &quot;hex=0&quot;
( ENDLOCAL &amp; REM RETURN VALUES
    IF &quot;%~2&quot; NEQ &quot;&quot; (SET %~2=%hex%) ELSE ECHO.%hex%
)
EXIT /b
</code></pre></div><p>toHex из <a href="http://www.dostips.com/DtCodeCmdLib.php#Function.toHex">http://www.dostips.com/DtCodeCmdLib.php#Function.toHex</a></p>]]></content>
			<author>
				<name><![CDATA[torus]]></name>
				<uri>https://forum.script-coding.com/profile.php?id=33003</uri>
			</author>
			<updated>2016-05-20T09:39:31Z</updated>
			<id>https://forum.script-coding.com/viewtopic.php?pid=103971#p103971</id>
		</entry>
</feed>
