JSI Tip 5842. How do I convert a decimal number to hexadecimal?
October 15, 2002
NOTE: See How can I convert a hexadecimal number to decimal, or a decimal number to hexadecimal, in a batch?
I have scripted a general purpose routine, Dec2Hex.bat, to convert a decimal number into a hexadecimal number.
The syntax for using Dec2Hex.bat is:
Dec2Hex DecimalNumber HexVariable HexNumberCharacters
where:
DecimalNumber | The decimal number to convert. It must be in the range of -2147483647 to 2147483647. |
---|---|
HexVariable | The call directed environment variable that will contain the hexadecimal number. |
HexNumberCharacters | The call directed numeric environment variable that will contain the number of characters in the hexadecimal number. If HexNumberCharacters has a data value of 0, a syntax error occurred. |
Examples:
call Dec2Hex 0 Hex length would set hex to 0 and length to the number 1.
call Dec2Hex 16 Hex length would set hex to 10 and length to the number 2.
call Dec2Hex 2147483647 Hex length would set hex to 7FFFFFFF and length to the number 8.
call Dec2Hex -1 Hex length would set hex to FFFFFFFF and length to the number 8.
Dec2Hex.bat contains:
@echo offsetlocalif {%3}
<h1><a name="_goto_syntax_set_work_1_set_neg_work_0_1_set_a_divid_1_if_neg_EQU_set_a_divid_work_1_10_1_if_neg_EQU_set_a_divid_0x7FFFFFFF_divid_set_a_cnt_8_set_dword0_0_set_dword1_0_set_dword2_0_set_dword3_0_set_dword4_0_set_dword5_0_set_dword6_0_set_dword7_0_set_Hex_0123456789ABCDEF_loop_set_a_mod_divid_16_set_a_quot_divid_16_set_a_divid_quot_set_a_cnt_cnt_1_call_set_dword_cnt_hex_mod_1_if_quot_EQU_0_goto_finish_goto_loop_syntax_echo_Syntax_Call_Dec2Hex_DecimalValue_HexName_HexChars_endlocal_If_not_3_" id="_goto_syntax_set_work_1_set_neg_work_0_1_set_a_divid_1_if_neg_EQU_set_a_divid_work_1_10_1_if_neg_EQU_set_a_divid_0x7FFFFFFF_divid_set_a_cnt_8_set_dword0_0_set_dword1_0_set_dword2_0_set_dword3_0_set_dword4_0_set_dword5_0_set_dword6_0_set_dword7_0_set_Hex_0123456789ABCDEF_loop_set_a_mod_divid_16_set_a_quot_divid_16_set_a_divid_quot_set_a_cnt_cnt_1_call_set_dword_cnt_hex_mod_1_if_quot_EQU_0_goto_finish_goto_loop_syntax_echo_Syntax_Call_Dec2Hex_DecimalValue_HexName_HexChars_endlocal_If_not_3_">{} goto syntaxset work=%1set neg=%work:~0,1%set /a divid=%1if "%neg%" EQU "-" set /a divid=%work:~1,10% - 1if "%neg%" EQU "-" set /a divid=0x7FFFFFFF - %divid%set /a cnt=8set dword0=0set dword1=0set dword2=0set dword3=0set dword4=0set dword5=0set dword6=0set dword7=0set Hex=0123456789ABCDEF:loopset /a mod=%divid% ^%% 16set /a quot=%divid% / 16set /a divid=%quot%set /a cnt=%cnt% - 1call set dword%cnt%=%%hex:~%mod%^,1%%if %quot% EQU 0 goto finishgoto loop:syntax@echo Syntax Call Dec2Hex DecimalValue HexName HexCharsendlocalIf not {%3}</a></h1>
{} set /a %3=0goto :EOF:finishif "%neg%" EQU "-" goto neg%dword0%:finish1set divid=%dword0%%dword1%%dword2%%dword3%%dword4%%dword5%%dword6%%dword7%set /a chars=8 - %cnt%call set ans=%%divid:~%cnt%^,%chars%%%endlocal&set %2=%ans%&set /a %3=%chars%goto :EOF:neg0set dword0=8goto nega:neg1set dword0=9goto nega:neg2set dword0=Agoto nega:neg3set dword0=Bgoto nega:neg4set dword0=Cgoto nega:neg5set dword0=Dgoto nega:neg6set dword0=Egoto nega:neg7set dword0=F:negaset /a cnt=0goto finish1
About the Author
You May Also Like