SQL SERVER中实现十六进制转换为字符的两种方法(二)

第二种方法:

先用VB写一个dll,比如叫intToHex,里面代码如下:

Option Explicit
Public Function IntToHex(intValue As Long, Optional intlength As Long = 2) As String
  IntToHex = Hex$(intValue)
  If Len(IntToHex) < intlength Then
     Dim i As Integer
     For i = 1 To intlength - Len(IntToHex)
         IntToHex = “0″ & IntToHex
     Next
  End If
End Function

其中intValue是要转化的数字,intlength是转化后要保留的字符的长度

将其编译成dll,然后在sql server中执行如下命令注册:

regsvr32 c:\intTohex.dll

然后自己写一个自定义函数,去实现这个功能:

CREATE FUNCTION dbo.fn_GetHexStr ( @intValue int,@paraValue int=4)
 RETURNS VARCHAR(10)
AS
BEGIN
DECLARE @object int
DECLARE @hr int
DECLARE @src varchar(255), @desc varchar(255),@strRet varchar(255)
EXEC @hr = sp_OACreate ‘IntToHex.InToHex’, @object OUT
IF @hr <> 0
BEGIN
   –EXEC sp_OAGetErrorInfo @object, @src OUT, @desc OUT
   –SELECT hr=convert(varbinary(4),@hr),
Source=@src, Description=@desc
    RETURN ‘error’
END
EXEC @hr=sp_OAMethod @object,’IntToHex’,@strRet output,@intValue,@paravalue
IF @hr <> 0
BEGIN
   –EXEC sp_OAGetErrorInfo @object, @src OUT, @desc OUT
   –SELECT hr=convert(varbinary(4),@hr),
Source=@src, Description=@desc
    RETURN ‘error’
END
EXEC sp_OADestroy @object
RETURN @strRet
END

最后在自己的sql语句中这样调用即可:

select dbo.fn_GetHexStr(intValue,3) from Int_To_Hex

相关连接:

SQL SERVER中实现十六进制转换为字符的两种方法(一)

SQL SERVER中实现十六进制转换为字符的两种方法(三)

  

No Comments

Be the first to comment on this entry.

Leave a comment

Name(required)
Mail (will not be published)(required)
Website

Fields in bold are required. Email addresses are never published or distributed.

Some HTML code is allowed:
<a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>
URLs must be fully qualified (eg: http://www.dbifan.com),and all tags must be properly closed.

Line breaks and paragraphs are automatically converted.

Please keep comments relevant. Off-topic, offensive or inappropriate comments may be edited or removed.

    搜索本站

    站点日历

    5月 2005
    « 4   6 »
     1
    2345678
    9101112131415
    16171819202122
    23242526272829
    3031  

    订阅本站

    文章分类

    最新日志

    热点文章

    日志存档

    常用标签