将整数转换为十六进制并将十六进制转换为整数
因此,我在 Sybase 中运行了此查询(其中 signal_data
是一列),但在 Microsoft SQL Server 中不起作用:
HEXTOINT(SUBSTRING((INTTOHEX(signal_data)),5,2)) as Signal
我也在 Excel 中运行了该查询(其中 A1
包含值):
=HEX2DEC(LEFT(DEC2HEX(A1),LEN(DEC2HEX(A1))-2))
有谁知道我将如何在 SQL Server 中执行此操作?
So I have this query working (where signal_data
is a column) in Sybase but it doesn't work in Microsoft SQL Server:
HEXTOINT(SUBSTRING((INTTOHEX(signal_data)),5,2)) as Signal
I also have it in Excel (where A1
contains the value):
=HEX2DEC(LEFT(DEC2HEX(A1),LEN(DEC2HEX(A1))-2))
Does anyone know how I would do this in SQL Server?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(17)
将 INT 转换为十六进制:
将十六进制转换为 INT:
更新 2015-03-16
上面的示例有一个限制,即它仅在以整数字面量形式给出 HEX 值时才有效。 为了完整起见,如果要转换的值是十六进制字符串(例如在 varchar 列中找到的值),请使用:
注意: 该字符串必须包含偶数个十六进制数字。 奇数个数字将产生错误。
更多详细信息,请参阅CAST 和 CONVERT (Transact- SQL)。 我相信需要 SQL Server 2008 或更高版本。
Convert INT to hex:
Convert hex to INT:
Update 2015-03-16
The above example has the limitation that it only works when the HEX value is given as an integer literal. For completeness, if the value to convert is a hexadecimal string (such as found in a varchar column) use:
Note: The string must contain an even number of hex digits. An odd number of digits will yield an error.
More details can be found in the "Binary Styles" section of CAST and CONVERT (Transact-SQL). I believe SQL Server 2008 or later is required.
实际上,内置函数名为 master.dbo.fn_varbintohexstr。
例如:
给你
100 0x00000064
Actually, the built-in function is named master.dbo.fn_varbintohexstr.
So, for example:
Gives you
100 0x00000064
SQL Server 相当于 Excel 基于字符串的 DEC2HEX、HEX2DEC 函数:
SQL Server equivalents to Excel's string-based DEC2HEX, HEX2DEC functions:
可以使用 SQL Server 2012 及更高版本上可用的函数 FORMAT
结果:
It is possible using the function FORMAT available on SQL Server 2012 and above
Results in:
将 int 转换为十六进制:
SELECT FORMAT(512+255,'X')
Convert int to hex:
SELECT FORMAT(512+255,'X')
传统的 4 位十六进制非常直接。
十六进制字符串到整数(假设值存储在名为 FHexString 的字段中):
整数到十六进制字符串(假设值存储在名为 FInteger 的字段中):
需要注意的是,当您开始使用导致寄存器共享的位大小时,尤其是在在英特尔机器上,由于英特尔的小端特性,寄存器中的高、低、左和右将被交换。 例如,当使用 varbinary(3) 时,我们讨论的是 6 个字符的十六进制。 在这种情况下,您的位将按以下索引从右到左配对“54,32,10”。 在英特尔系统中,您会期望“76,54,32,10”。 由于您只使用了 8 个中的 6 个,因此您需要记住自己进行交换。 “76,54”将成为您的左侧,“32,10”将成为您的右侧。 逗号分隔你的最高点和最低点。 英特尔交换了高点和低点,然后交换了左边和右边。 因此,要进行转换...叹息,您必须自己交换它们,例如,以下转换 8 字符十六进制的前 6 个:
这有点复杂,所以我会尝试将转换保持为 8 字符十六进制 (varbinary (4))。
总而言之,这应该可以回答您的问题。 综合来说。
The traditonal 4 bit hex is pretty direct.
Hex String to Integer (Assuming value is stored in field called FHexString) :
Integer to Hex String (Assuming value is stored in field called FInteger):
Important to note is that when you begin to use bit sizes that cause register sharing, especially on an intel machine, your High and Low and Left and Rights in the registers will be swapped due to the little endian nature of Intel. For example, when using a varbinary(3), we're talking about a 6 character Hex. In this case, your bits are paired as the following indexes from right to left "54,32,10". In an intel system, you would expect "76,54,32,10". Since you are only using 6 of the 8, you need to remember to do the swaps yourself. "76,54" will qualify as your left and "32,10" will qualify as your right. The comma separates your high and low. Intel swaps the high and lows, then the left and rights. So to do a conversion...sigh, you got to swap them yourselves for example, the following converts the first 6 of an 8 character hex:
It's a bit complicated, so I would try to keep my conversions to 8 character hex's (varbinary(4)).
In summary, this should answer your question. Comprehensively.
这是 SQL Server 的函数,它将整数值转换为 varchar 的十六进制表示形式。 应该很容易适应其他数据库类型
例如:
SQL:
Here is the function for SQL server which converts integer value into its hexadecimal representation as a varchar. It should be easy to adapt to other database types
For example:
SQL:
使用
master.dbo.fnbintohexstr(16777215)
转换为varchar
表示形式。Use
master.dbo.fnbintohexstr(16777215)
to convert to avarchar
representation.Maksym Kozlenko 有一个很好的解决方案,其他人接近释放其全部潜力,但随后完全错过了意识到您可以定义任何字符序列,并使用它长度作为基础。 这就是为什么我喜欢他的解决方案的这个稍微修改的版本,因为它可以适用于基数 16 或基数 17 等。
例如,如果您想要字母和数字,但不喜欢我看起来像 1 和O 代表看起来像 0。 您可以通过这种方式定义任何序列。 下面是“Base 36”的一种形式,它跳过 I 和 O 以创建“修改的 Base 34”。 取消注释十六进制行,改为以十六进制运行。
我还将值移至工作值,然后根据个人喜好从工作值副本开始工作。
下面是针对任何序列的反转转换的附加内容,其基数定义为序列的长度。
Maksym Kozlenko has a nice solution, and others come close to unlocking it's full potential but then miss completely to realized that you can define any sequence of characters, and use it's length as the Base. Which is why I like this slightly modified version of his solution, because it can work for base 16, or base 17, and etc.
For example, what if you wanted letters and numbers, but don't like I's for looking like 1's and O's for looking like 0's. You can define any sequence this way. Below is a form of a "Base 36" that skips the I and O to create a "modified base 34". Un-comment the hex line instead to run as hex.
I also moved value over to a working value, and then work from the working value copy, as a personal preference.
Below is additional for reversing the transformation, for any sequence, with the base defined as the length of the sequence.
Maksym Kozlenko 的答案很好,可以稍微修改一下以将数值编码为任何代码格式。 例如:
因此,像 150,000,000 这样的大数字,只需要 6 个字符(150,000,000 =“MQGJMU”)
您还可以使用不同序列中的不同字符作为加密设备。 或者传入代码字符和字符长度并用作加密的加盐方法。
反之亦然:
The answer by Maksym Kozlenko is nice and can be slightly modified to handle encoding a numeric value to any code format. For example:
So, a big number like 150 million, becomes only 6 characters (150,000,000 = "MQGJMU")
You could also use different characters in different sequences as an encrypting device. Or pass in the code characters and length of characters and use as a salting method for encrypting.
And the reverse:
给定:
IntToHexStr:
HexStrToInt:
Given:
IntToHexStr:
HexStrToInt:
下面是两个函数: dbo.HexToInt 和 dbo.IntToHex,我使用它们进行此类转换:
第二个:
Below are two functions: dbo.HexToInt and dbo.IntToHex, I use them for such conversion:
And the second one:
作为字体颜色的表达对我有用
Is working for me as an expression in font colour
要将十六进制字符串转换为 INT,我过去曾使用过它。 实际上可以修改它以将任何基数转换为 INT(八进制、二进制等)
To convert Hex strings to INT, I have used this in the past. It can be modified to convert any base to INT in fact (Octal, Binary, whatever)
除了 DenNukem 的答案之外:
您可能需要输入大量数字才能使格式功能正常工作。
In addition to DenNukem's answer:
You might need to type large numbers for the format function to work.
从表中选择 to_hex(signal_data) 作为 signal_data
select to_hex(signal_data) as signal_data from table