Convert ASCII

  • Hi All,

    Here i am facing the problem while converting Hexadecimal values to Ascii.

    If i give the qry : select ASCII(0x4F)

    Result : 79

    fine. i want 4F has to be passed from dynamically like :

    DECLARE @s-2 varchar(2)

    declare @s1 varchar(2)

    set @s-2='0x'

    set @s1='4F'

    select ASCII((CONVERT(VARCHAR, (@s + @s1))))

    Result is giving some thing diff.

    Is this correct way? or how ot do??

    Pls help me on this!!

  • Not Entirely sure what you want but try this:

    It Will give you the ascii number represented by the hexadecimal number

    declare @s1 varchar(2)

    ,@ii int

    ,@bin varbinary(10)

    set @s1='14'

    set @bin = 0

    set @ii = 1

    While @ii <= Len(@s1)Begin

    Select @Bin = @Bin +( Power( 16, @ii - 1 ) *

    Convert( Int, Substring( Reverse( @s1 ), @ii, 1 ) ) )

    Select @Bin

    Set @ii = @ii +1

    End

    Select Convert( int, @bin )

  • I am pretty srue there is a proper conversion however I cannot seem to find it right off. But here is a way that will work

    DECLARE @s-2 as VARCHAR(2)

    SET @s-2 = '4F'

    EXEC('SELECT ASCII(CAST(0x' + @s-2 + ' as varbinary(1)))')

Viewing 3 posts - 1 through 3 (of 3 total)

You must be logged in to reply to this topic. Login to reply