先把整个代码粘上来。
Code
1CREATE FUNCTION [dbo].[ufn_HecToDec]
2(
3 @Hec varchar(50)
4)
5RETURNS int
6AS
7BEGIN
8 DECLARE @Dec int,@Count int,@BigBit int,@StrOne int
9
10 SET @BigBit = 0
11 SET @Count = 1
12 SET @Dec = 0
13
14 --Get the number of hexadecimal digits
15 WHILE @BigBit < LEN(@Hec)
16 BEGIN
17 SET @StrOne = ASCII(LOWER(SUBSTRING(@Hec,@Count,1)))
18 IF (@StrOne >= 48 AND @StrOne <= 57)
19 OR
20 (@StrOne >= 97 AND @StrOne <= 102)
21 BEGIN
22 SET @BigBit = @BigBit + 1
23 SET @Count = @Count + 1
24 CONTINUE
25 END
26 ELSE
27 BREAK
28 END
29
30 SET @Count = 1
31
32 WHILE @BigBit > 0
33 BEGIN
34 SET @StrOne =ASCII(LOWER(SUBSTRING(@Hec,@Count,1)))
35 SELECT @Dec = @Dec +
36 CAST(
37 CASE WHEN @StrOne >=48 AND @StrOne <= 57 THEN @StrOne-48
38 WHEN @StrOne = 97 THEN 10
39 WHEN @StrOne = 98 THEN 11
40 WHEN @StrOne = 99 THEN 12
41 WHEN @StrOne = 100 THEN 13
42 WHEN @StrOne = 101 THEN 14
43 WHEN @StrOne = 102 THEN 15
44 END
45 AS INT)*POWER(16,@BigBit-1)
46 SET @Count = @Count + 1
47 SET @BigBit = @BigBit - 1
48 END
49
50 RETURN @Dec
51END
52GO
1CREATE FUNCTION [dbo].[ufn_HecToDec]
2(
3 @Hec varchar(50)
4)
5RETURNS int
6AS
7BEGIN
8 DECLARE @Dec int,@Count int,@BigBit int,@StrOne int
9
10 SET @BigBit = 0
11 SET @Count = 1
12 SET @Dec = 0
13
14 --Get the number of hexadecimal digits
15 WHILE @BigBit < LEN(@Hec)
16 BEGIN
17 SET @StrOne = ASCII(LOWER(SUBSTRING(@Hec,@Count,1)))
18 IF (@StrOne >= 48 AND @StrOne <= 57)
19 OR
20 (@StrOne >= 97 AND @StrOne <= 102)
21 BEGIN
22 SET @BigBit = @BigBit + 1
23 SET @Count = @Count + 1
24 CONTINUE
25 END
26 ELSE
27 BREAK
28 END
29
30 SET @Count = 1
31
32 WHILE @BigBit > 0
33 BEGIN
34 SET @StrOne =ASCII(LOWER(SUBSTRING(@Hec,@Count,1)))
35 SELECT @Dec = @Dec +
36 CAST(
37 CASE WHEN @StrOne >=48 AND @StrOne <= 57 THEN @StrOne-48
38 WHEN @StrOne = 97 THEN 10
39 WHEN @StrOne = 98 THEN 11
40 WHEN @StrOne = 99 THEN 12
41 WHEN @StrOne = 100 THEN 13
42 WHEN @StrOne = 101 THEN 14
43 WHEN @StrOne = 102 THEN 15
44 END
45 AS INT)*POWER(16,@BigBit-1)
46 SET @Count = @Count + 1
47 SET @BigBit = @BigBit - 1
48 END
49
50 RETURN @Dec
51END
52GO
分段说一下
1. 先说一下变量的含义
Code
1DECLARE @Dec int,@Count int,@BigBit int,@StrOne int
2--@Dec: 存储最终计算出来的十进制数
3--@Count:获取单个字符的开始位置 (用于分析字符串)
4--@BigBit:十六进制数的最高位
5--@StrOne:存储获取的单个字符 (用于分析是否为合法的十六进制 & 十六进制具体值)
1DECLARE @Dec int,@Count int,@BigBit int,@StrOne int
2--@Dec: 存储最终计算出来的十进制数
3--@Count:获取单个字符的开始位置 (用于分析字符串)
4--@BigBit:十六进制数的最高位
5--@StrOne:存储获取的单个字符 (用于分析是否为合法的十六进制 & 十六进制具体值)
2. 获取十六进制数的位数
Code
1WHILE @BigBit < LEN(@Hec)
2 BEGIN
3 SET @StrOne = ASCII(LOWER(SUBSTRING(@Hec,@Count,1)))--获取单个字符,并转为ASCII值
4 IF (@StrOne >= 48 AND @StrOne <= 57) --判断是否为合法的十六进制数
5 OR -- (48-57: 0-9)
6 (@StrOne >= 97 AND @StrOne <= 102) -- (97-102: a-f)
7 BEGIN
8 SET @BigBit = @BigBit + 1 --如果是合法的十六进数,对十六进制数的位数进行累加
9 SET @Count = @Count + 1
10 CONTINUE
11 END
12 ELSE
13 BREAK --遇到不合法的十六进制数,退出循环
14 END
1WHILE @BigBit < LEN(@Hec)
2 BEGIN
3 SET @StrOne = ASCII(LOWER(SUBSTRING(@Hec,@Count,1)))--获取单个字符,并转为ASCII值
4 IF (@StrOne >= 48 AND @StrOne <= 57) --判断是否为合法的十六进制数
5 OR -- (48-57: 0-9)
6 (@StrOne >= 97 AND @StrOne <= 102) -- (97-102: a-f)
7 BEGIN
8 SET @BigBit = @BigBit + 1 --如果是合法的十六进数,对十六进制数的位数进行累加
9 SET @Count = @Count + 1
10 CONTINUE
11 END
12 ELSE
13 BREAK --遇到不合法的十六进制数,退出循环
14 END
3. 计算十六制数
Code
1WHILE @BigBit > 0 --以合法的十六进制数的位数做为循环条件
2 BEGIN
3 SET @StrOne =ASCII(LOWER(SUBSTRING(@Hec,@Count,1)))--获取单个的十六进制数
4 SELECT @Dec = @Dec +
5 CAST(
6 CASE WHEN @StrOne >=48 AND @StrOne <= 57 THEN @StrOne-48
7 WHEN @StrOne = 97 THEN 10 --a = 10
8 WHEN @StrOne = 98 THEN 11 --b = 11
9 WHEN @StrOne = 99 THEN 12 --c = 12
10 WHEN @StrOne = 100 THEN 13 --d = 13
11 WHEN @StrOne = 101 THEN 14 --e = 14
12 WHEN @StrOne = 102 THEN 15 --f = 15
13 END
14 AS INT)*POWER(16,@BigBit-1) --单个的十六进制数*16的位数立方 (累加)
15 SET @Count = @Count + 1
16 SET @BigBit = @BigBit - 1
17 END
1WHILE @BigBit > 0 --以合法的十六进制数的位数做为循环条件
2 BEGIN
3 SET @StrOne =ASCII(LOWER(SUBSTRING(@Hec,@Count,1)))--获取单个的十六进制数
4 SELECT @Dec = @Dec +
5 CAST(
6 CASE WHEN @StrOne >=48 AND @StrOne <= 57 THEN @StrOne-48
7 WHEN @StrOne = 97 THEN 10 --a = 10
8 WHEN @StrOne = 98 THEN 11 --b = 11
9 WHEN @StrOne = 99 THEN 12 --c = 12
10 WHEN @StrOne = 100 THEN 13 --d = 13
11 WHEN @StrOne = 101 THEN 14 --e = 14
12 WHEN @StrOne = 102 THEN 15 --f = 15
13 END
14 AS INT)*POWER(16,@BigBit-1) --单个的十六进制数*16的位数立方 (累加)
15 SET @Count = @Count + 1
16 SET @BigBit = @BigBit - 1
17 END
传入的十六进制数的位数不受限制(但你要考虑数据类型的范围)。
有不对的地方请大家指教。