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-05 23:04
中国DOS联盟论坛 » DOS批处理 & 脚本技术(批处理室) » Decimal to Hexadecimal (Bit Manipulation Version) View 3,733 Replies 12
Original Poster Posted 2007-01-07 06:23 ·  中国 北京 联通
中级用户
★★
带走
Credits 435
Posts 88
Joined 2005-09-24 19:22
20-year member
UID 42793
Status Offline
The maximum number that can be correctly processed is 2147483647, which is a 31-bit binary number. An error occurs at the 32nd bit. The version of moderator 3742668 is the same. Entering 2147483648 causes an error. Although the precision can reach 4294967295 by judging the number of shifts, I don't encounter such a large number in application, so I won't bother much.

@echo off
setlocal enabledelayedexpansion
set hexstr=0 1 2 3 4 5 6 7 8 9 A B C D E F
set d=0
for %%i in (%hexstr%) do (set d!d!=%%i&set/a d+=1)
set/p scanf=The maximum number that can be correctly processed is , please enter the decimal number to be converted:
if not defined scanf exit/b
set dec=%scanf%
call :d2h
if not defined hex set hex=0
echo The hexadecimal of %dec% is: 0x%hex%
pause>nul
exit/b

:d2h
if %scanf% equ 0 exit/b
set/a tscanf=%scanf%"&"15
set/a scanf">>="4
set hex=!d%tscanf%!!hex!
goto :d2h
Recent Ratings for This Post ( 4 in total) Click for details
RaterScoreTime
redtek +5 2007-01-07 21:04
lxmxn +10 2007-01-08 04:28
ccwan +5 2007-01-12 06:57
electronixtar +20 2007-01-13 06:56
Floor 2 Posted 2007-01-07 06:24 ·  中国 北京 联通
中级用户
★★
带走
Credits 435
Posts 88
Joined 2005-09-24 19:22
20-year member
UID 42793
Status Offline
==Deleted==

[ Last edited by 0401 on 2007-1-25 at 09:19 AM ]
Floor 3 Posted 2007-01-07 08:46 ·  中国 四川 成都 教育网
铂金会员
★★★★
Credits 7,493
Posts 2,672
Joined 2005-09-02 00:00
20-year member
UID 42173
Gender Male
Status Offline
Floor 4 Posted 2007-01-07 09:24 ·  中国 北京 联通
中级用户
★★
带走
Credits 435
Posts 88
Joined 2005-09-24 19:22
20-year member
UID 42793
Status Offline
Originally posted by electronixtar at 2007-1-7 08:46:
The building owner is a strong person

I'm a noob, and I'm shy.
Post another decimal to binary conversion using the same method.
@echo off
setlocal enabledelayedexpansion
set/p scanf=The maximum number that can be correctly processed is , please enter the decimal number to be converted:
if not defined scanf exit/b
set dec=%scanf%
call :d2b
echo The binary of %dec% is: %bin%
pause>nul
exit/b

:d2b
set a=1
for /l %%i in (1,1,31) do (
set/a t=%scanf%"&"!a!
set/a a*=2
if !t! equ 0 (set bin=0!bin!) else (set bin=1!bin!)
)
if %bin% equ 0 set bin=0 & goto :eof
:cut0
if %bin:~0,1% equ 0 set bin=%bin:~1%&goto :cut0
Recent Ratings for This Post ( 2 in total) Click for details
RaterScoreTime
lxmxn +10 2007-01-08 04:28
ccwan +5 2007-01-12 06:57
Floor 5 Posted 2007-01-07 17:15 ·  中国 湖北 武汉 电信
版主
★★★★★
Credits 11,386
Posts 4,938
Joined 2006-07-23 17:10
20-year member
UID 59080
Status Offline

  A great person.
Floor 6 Posted 2007-01-07 21:04 ·  中国 北京 联通
金牌会员
★★★★
Credits 2,902
Posts 1,147
Joined 2006-09-21 12:00
19-year member
UID 63324
Gender Male
Status Offline
Super top! Appreciate ~~ : )
    Redtek,一个永远在网上流浪的人……

_.,-*~'`^`'~*-,.__.,-*~'`^`'~*-,._,_.,-*~'`^`'~*-,._,_.,-*~'`^`'~*-,._
Floor 7 Posted 2007-01-12 06:55 ·  中国 上海 嘉定区 电信
初级用户
Credits 100
Posts 46
Joined 2006-12-19 04:57
19-year member
UID 73922
Gender Male
Status Offline
It's really refreshing to use, amazing.
Floor 8 Posted 2007-01-12 06:57 ·  中国 河北 廊坊 三河市 移动
金牌会员
★★★★
Credits 2,725
Posts 1,160
Joined 2006-09-23 12:00
19-year member
UID 63486
From 河北廊坊
Status Offline
That's really amazing. I'm supporting it.
三人行,必有吾师焉。 学然后知不足,教然后知困,然后能自强也。
Floor 9 Posted 2007-03-13 07:50 ·  中国 甘肃 张掖 电信
金牌会员
★★★★
Credits 4,103
Posts 1,744
Joined 2006-01-20 13:00
20-year member
UID 49241
Gender Male
From 甘肃.临泽
Status Offline
Brother 0401 on the 4th floor, the decimal to binary conversion can still be improved. The tag :cut0 at the end is redundant. It's better to be like this:


:d2b
set a=1
for /l %%i in (1,1,31) do (
set/a t=%scanf%"&"!a!
set/a a*=2
if !t! equ 0 (set bin=0!bin!) else (set bin=1!bin!)
if %scanf% lss !a! exit/b
)
if %bin% equ 0 set bin=0 & goto :eof


Modifications are as follows:
1: 1,1,30 is changed because for /l %%i in (1,1,31) do (set/a a*=a) will already have an error.
2: Added a line if %scanf% lss !a! exit/b, because the operation of number a and a larger number is always 0. Since this is the case, it's better to directly end with if %scanf% lss !a! exit/b, and then the preceding 0s won't be removed later.
Floor 10 Posted 2007-03-13 08:22 ·  中国 安徽 马鞍山 电信
中级用户
★★
Credits 493
Posts 228
Joined 2007-02-16 00:38
19-year member
UID 79596
Gender Male
From 安徽
Status Offline
Add a "stamp" first, otherwise it won't be found next time!
Floor 11 Posted 2007-11-02 12:44 ·  中国 云南 昆明 联通
初级用户
Credits 90
Posts 43
Joined 2007-10-31 11:00
18-year member
UID 101268
Gender Male
Status Offline
I will reply to post 9

if %scanf% lss !a! exit/b
There are obvious drawbacks in your code.
Every time the loop is executed, a judgment is made, which reduces the program efficiency
Floor 12 Posted 2007-11-02 12:57 ·  中国 云南 昆明 联通
初级用户
Credits 90
Posts 43
Joined 2007-10-31 11:00
18-year member
UID 101268
Gender Male
Status Offline
There is another question
"1,1,30 Because you for /l %%i in (1,1,31) do (set/a a*=a) has already gone wrong"

When %%i = 31 here, a = 2^(30), there is no mistake. It will be wrong when %%i = 32, at this time a = 2^(31)

So the code on floor 9 is correct!
Floor 13 Posted 2010-08-10 09:03 ·  中国 四川 广元 联通
高级用户
★★★
Credits 609
Posts 374
Joined 2006-08-02 22:38
20-year member
UID 59720
Status Offline
I used the method of remainder to write a decimal to hexadecimal conversion, and confirmed that the maximum limit processing is indeed 2147483647, that is, the hexadecimal limit is 7FFFFFFF, and the reversal also illustrates this problem. It should be said to be the limit of CMD.
Forum Jump: