将十六进制转换为 INT,反之亦然
- 作者: 我的丁丁比我的名字还要长好多好多
- 来源: 51数据库
- 2023-02-07
问题描述
我将创建一个由十六进制值组成的序列号
I will be creating a sequential Serial Number made from Hexadecimal values
使用这种格式:
XX-XX-XX-YYYY Which XX-XX-XX is default value And YYYY is the incrementing hexa decimal value
现在要根据十六进制值创建序列号,我需要将 6 添加到最后生成的十六进制值
Now to create the serial number based on hex value I need Add 6 to the last generated hex value
MIN: 2D41 + 6 = 2D47 2D47 + 6 ... and so on MAX: 4100 generation of serial will stop when I meet the MAX value.
我已经在 c# 中创建了它,但我需要在 SQL 上进行
I already created it in c# but I need to do it on SQL
int num1 = int.Parse("2D41", NumberStyles.HexNumber); //Convert hex to int int result = num1 + 6; //Add + 6 for increment string myHex = result.ToString("X"); //Convert result to hex MessageBox.Show(myHex); // result 2D47
如何在 T-SQL 中做到这一点?
How can this be done in T-SQL?
推荐答案
希望对你有帮助
declare @seed varchar(max) = '2D41'; declare @limit varchar(max) = '4100'; select convert(int, convert(varbinary(max), '0x'+@seed,1)), convert(int, convert(varbinary(max), '0x'+@limit,1)); ;with seedlimit(seed, limit) as ( select convert(int, convert(varbinary(max), '0x'+@seed,1)), convert(int, convert(varbinary(max), '0x'+@limit,1)) ) select SerialNumber = 'XX-XX-XX-' + right(convert(varchar(10),cast(s.seed + 6 * v.number as varbinary(max)),1),4) from seedlimit s join master.dbo.spt_values v on type='p' where s.seed + 6 * v.number <= s.limit;
您可以根据答案创建视图/过程/函数的基本成分,
The basic ingredients are in there for you to create a view/procedure/function out of the answer,
输出:
SerialNumber ------------- XX-XX-XX-2D41 XX-XX-XX-2D47 ... XX-XX-XX-40F7 XX-XX-XX-40FD
推荐阅读
热点文章
检查拆分键盘
0
带有“上一个"的工具栏和“下一个"用于键盘输入AccessoryView
0
Activity 启动时显示软键盘
0
UIWebView 键盘 - 摆脱“上一个/下一个/完成"酒吧
0
在 iOS7 中边缘滑动时,使键盘与 UIView 同步动画
0
我的 iOS 应用程序中的键盘在 iPhone 6 上太高了.如何在 XCode 中调整键盘的分辨率?
0
android:inputType="textEmailAddress";- '@' 键和 '.com' 键?
0
禁用 iPhone 中键盘的方向
0
Android 2.3 模拟器上的印地语键盘问题
0
keyDown 没有被调用
0