China DOS Union

-- Unite DOS · Advance DOS · Grow DOS --

Union site: www.cn-dos.net Forum site: www.cn-dos.net/forum
DOS stands for freedom, openness and progress. Let us work hard, learn from the openness and GNU spirit of FreeDOS and Linux, and together build and grow a free GNU GPL world!

中国DOS联盟论坛
The time now is 2026-08-09 04:14
中国DOS联盟论坛 » DOS批处理 & 脚本技术(批处理室) » Conversion from decimal to hexadecimal: View 2,121 Replies 13
Original Poster Posted 2008-07-05 13:19 ·  中国 湖南 株洲 电信
金牌会员
★★★★
永远的学习者
Credits 3,105
Posts 1,276
Joined 2008-03-08 13:00
18-year member
UID 112398
Gender Male
Status Offline
```
@echo off
set code=0123456789abcdef
:enter
cls&set /p num=Please enter a decimal number:
echo %num%|findstr ""&&goto enter
set var=%num%
:again
set /a ws=%var%%%16
call,set ws=%%code:~%ws%,1%%
set /a var/=16
set str=%ws%%str%
if %var% geq 16 goto again
cls&echo The decimal number %num% is converted to hexadecimal as: %var%%str%
pause>nul
```

[ Last edited by zw19750516 on 2008-7-5 at 01:50 PM ]
Recent Ratings for This Post ( 1 in total) Click for details
RaterScoreTime
WANKOILZ +2 2008-07-07 14:53
批处理之家新域名:www.bathome.net
Floor 2 Posted 2008-07-05 13:39 ·  中国 重庆 九龙坡区 电信
版主
★★★★★
Credits 9,023
Posts 5,017
Joined 2007-05-31 19:39
19-year member
UID 89899
Gender Male
Status Offline
Previously saw others write

@echo off
rem The maximum number to process is 2147483647
setlocal ENABLEDELAYEDEXPANSION
set /p num=num?
set str=0123456789ABCDEF
call :A %num%
echo %T3%
echo %num%
pause
exit

:A
set T1=%1
SET T3=
:B
set/A T2=T1%%16
SET T2=!str:~%t2%,1!
SET/A T1=%T1%/16
SET T3=%T2%%T3%
IF %T1%==0 GOTO :EOF
GOTO B
Floor 3 Posted 2008-07-05 14:04 ·  中国 山东 淄博 联通
银牌会员
★★★
Credits 1,604
Posts 646
Joined 2008-04-13 23:39
18-year member
UID 115804
Gender Male
Status Offline
Originally posted by zw19750516 at 2008-7-5 01:19 PM:

@echo off
set code=0123456789abcdef
:enter
cls&set /p num=请输入一个十进制数字:
echo %num%|findstr ""&&goto enter
set var=%num%
:again
set /a ...


Sorry... I was shallow...

[ Last edited by pusofalse on 2008-7-5 at 02:17 PM ]
心绪平和,眼藏静谧,无比安稳的火... Purification of soul...Just a false...^_^
Floor 4 Posted 2008-07-05 14:19 ·  中国 山东 潍坊 联通
初级用户
★★
Credits 73
Posts 51
Joined 2007-08-31 10:04
18-year member
UID 96241
Gender Male
Status Offline
Strongly learn from the各位 upstairs.

(注:原句“强烈的向楼上各位学习。”中“强烈的”表述有误,应为“强烈地”,正确翻译为“Strongly learn from the各位 upstairs.” 但根据要求要保留原文错误的话就按错误的翻译,不过更准确的应该是“Strongly learn from everyone upstairs.”)
Floor 5 Posted 2008-07-05 14:23 ·  中国 湖南 株洲 电信
金牌会员
★★★★
永远的学习者
Credits 3,105
Posts 1,276
Joined 2008-03-08 13:00
18-year member
UID 112398
Gender Male
Status Offline
```
@echo off
set code=1 2 3 4 5 6 7 8 9 a b c d e f&set m=1
set /p str=Please enter a hexadecimal number:
:lp
set a=%str:~-1%
for %%i in (%code%) do set /a n+=1&if "%%i" equ "%a%" call,set a=%%n%%
set /a num+=m*a
set /a m*=16,n=0
if defined str set str=%str:~,-1%&goto lp
echo %num%&pause>nul


[ Last edited by zw19750516 on 2008-7-5 at 02:25 PM ]
```
批处理之家新域名:www.bathome.net
Floor 6 Posted 2008-07-05 14:42 ·  中国 湖南 娄底 电信
银牌会员
★★★
Credits 2,268
Posts 879
Joined 2006-12-19 16:23
19-year member
UID 73968
Gender Male
Status Offline
Hexadecimal to decimal: There is a simpler way.

@echo off
set /p b=Please enter a hexadecimal number:
set /a a=0x%b%
echo %a%
pause
Recent Ratings for This Post ( 1 in total) Click for details
RaterScoreTime
bat-zw +3 2008-07-05 15:10
致精致简!
Floor 7 Posted 2008-07-05 14:45 ·  中国 山东 淄博 联通
银牌会员
★★★
Credits 1,604
Posts 646
Joined 2008-04-13 23:39
18-year member
UID 115804
Gender Male
Status Offline
Hexadecimal to decimal. I know a very simple method, which I learned from the post about getting ASCII codes I saw yesterday...

set/p num=
set/a a=0x%num%
echo %a%
pause
心绪平和,眼藏静谧,无比安稳的火... Purification of soul...Just a false...^_^
Floor 8 Posted 2008-07-05 15:07 ·  中国 湖南 株洲 电信
金牌会员
★★★★
永远的学习者
Credits 3,105
Posts 1,276
Joined 2008-03-08 13:00
18-year member
UID 112398
Gender Male
Status Offline
Originally posted by 26933062 at 2008-7-5 14:42:
Hexadecimal to decimal: There is a simpler method.

@echo off
set /p b=Please enter the hexadecimal number:
set /a a=0x%b%
echo %a%
pause

I just realized it's so simple. It seems that learning is endless.
批处理之家新域名:www.bathome.net
Floor 9 Posted 2008-07-05 15:58 ·  中国 重庆 九龙坡区 电信
版主
★★★★★
Credits 9,023
Posts 5,017
Joined 2007-05-31 19:39
19-year member
UID 89899
Gender Male
Status Offline
rem Three different data inputs, but all will be output in decimal.
rem Decimal
set /a 123
rem Octal
set /a 012
rem Hexadecimal
set /a 0xa
Floor 10 Posted 2008-07-06 00:03 ·  中国 广西 玉林 电信
金牌会员
★★★★
Credits 3,687
Posts 1,467
Joined 2005-08-08 12:00
21-year member
UID 44210
Status Offline
The conversion between number bases has actually been discussed before.
Floor 11 Posted 2008-07-06 17:02 ·  中国 江苏 常州 电信
银牌会员
★★★
Credits 2,404
Posts 946
Joined 2005-09-08 13:44
20-year member
UID 42345
Status Offline
It seems that there was one posted before. Adding a variable can realize decimal to any base conversion.
简单!简单!再简单!
Floor 12 Posted 2008-07-06 19:25 ·  中国 重庆 九龙坡区 电信
版主
★★★★★
Credits 9,023
Posts 5,017
Joined 2007-05-31 19:39
19-year member
UID 89899
Gender Male
Status Offline
Originally posted by terse at 2008-7-6 05:02 PM:
It seems that it was posted before
Adding a variable can realize decimal to any base conversion


The code at that time
@echo off&setlocal enabledelayedexpansion
set/p a=Please enter the decimal number to convert:
set aa=%a%
set/p b=Please enter the base to convert to:
set str=0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ
:hex
set/a m=!a!/!b!
set/a n=!a!%%!b!
set n=!str:~%n%,1!
set h=!n!!h!
if !m! geq !b! set a=!m! &goto hex
set m=!str:~%m%,1!
echo !aa! in base !b! is !m!!h!
Recent Ratings for This Post ( 1 in total) Click for details
RaterScoreTime
WANKOILZ +2 2008-07-08 22:11
Floor 13 Posted 2008-07-08 01:51 ·  中国 广东 江门 联通
初级用户
Credits 42
Posts 13
Joined 2007-08-03 23:26
19-year member
UID 94610
Gender Male
Status Offline
echo ibase=10;obase=16;10|bc
Floor 14 Posted 2008-07-08 02:17 ·  美国 惠普HP
版主
★★★★★
Credits 9,023
Posts 5,017
Joined 2007-05-31 19:39
19-year member
UID 89899
Gender Male
Status Offline
```
printf "%x" "10"
```

printf.exe
http://upload.cn-dos.net/img/580.zip
Forum Jump: