--创建16进制转换字符串函数
CREATE function fn_hex_to_char (
  @x varbinary(100), -- binary hex value
  @l int -- number of bytes
  ) returns varchar(200)
 as 
-- Written by: Gregory A. Larsen
-- Date: May 25, 2004
-- Description:  This function will take any binary value and return 
--               the hex value as a character representation.
--               In order to use this function you need to pass the 
--               binary hex value and the number of bytes you want to
--               convert.
begindeclare @i varbinary(10)
declare @digits char(16)
set @digits = '0123456789ABCDEF'
declare @s varchar(100)
declare @h varchar(100)
declare @j int
set @j = 0 
set @h = ''
-- process all  bytes
while @j < @l
begin
  set @j= @j + 1
  -- get first character of byte
  set @i = substring(cast(@x as varbinary(100)),@j,1)
  -- get the first character
  set @s = cast(substring(@digits,@i%16+1,1) as char(1))
  -- shift over one character
  set @i = @i/16 
  -- get the second character
  set @s = cast(substring(@digits,@i%16+1,1) as char(1)) + @s
  -- build string of hex characters
  set @h = @h + @s
end
return(@h)
end--调用
declare @T1 nvarchar(4000)
        ,@tmp varchar(20)
set @T1=N'國'
select @tmp='0x'+substring(dbo.fn_hex_to_char(convert(varbinary(16),@T1),16),3,2)
exec('select convert(int,'+@tmp+')')--结果
/*
----------- 
87
*/

解决方案 »

  1.   

    to: zlp321002,你只要帮我把0x0B57后面的2位57转化为十进制87即可,没有什么规则!
        其实是有,我只是想问题简单一点,只问了一块而已!估计 vivianfdlpw应该可以满足我的需求!我要消化一下再说谢谢大家罗;-)
      

  2.   

    declare @T1 nvarchar(4000)
    declare @result int,@tmp int
    set @T1=N'國'
    set @tmp=cast(convert(varbinary(8000),@T1)as int)
    set @result=@tmp%power(16,2)
    select @result as 'result',@tmp as '十制制'
    /*
    result      十制制         
    ----------- ----------- 
    87          2903(所影响的行数为 1 行)*/
      

  3.   

    vivianfdlpw给的是我要的, wgsasd311你的不完全符合我的要求!我只要87,你的十进制哪来得!十进制就是我们用的1,2,3,4,5呀!不过你有87也是ok!好感谢大家!
      

  4.   

    wgsasd311你的不完全符合我的要求!我只要87,你的十进制哪来得!十进制就是我们用的1,2,3,4,5呀!不过你有87也是ok!
    -------------你要87,只要RESULT就行了,十进制是中间变量,只是为了过渡的你要的结果.
      

  5.   

    --to MorningTea(一勺抹茶),下面帮你封装成函数,可直接调用.
    create function f_1
    (@str nvarchar,--原字符串
    @num int --取最右边的位数
    )
    returns  int
    as 
    begin
    declare @tmp int,@result int
    set @tmp=cast(convert(varbinary,@str)as int)
    set @result=(@tmp%power(16,2))
    return @result
    end
    go
    select dbo.f_1('國',2)
    drop function dbo.f_1
      

  6.   

    --不好意思,上面函数,忘了把2改成参数@num
    --to MorningTea(一勺抹茶),下面帮你封装成函数,可直接调用.
    create function f_1
    (@str nvarchar,--原字符串
    @num int --取最右边的位数
    )
    returns  int
    as 
    begin
    declare @tmp int,@result int
    set @tmp=cast(convert(varbinary,@str)as int)
    set @result=@tmp%power(16,@num)
    return @result
    end
    go
    select dbo.f_1('國',1)
    drop function dbo.f_1
      

  7.   

    太好了!
    谢谢各位!其实我是要做识别有没有输入繁体文!
    不过不知道可以做到不,我不这样问,免得误导大家!
    因为繁体文的第二字节会从127~255,简体文是160~255
    所以我判断小雨127就是有输入繁体文!我已经找到一位做过的人,现在就等他看他的email啦~~~