Categories: MSDN / DotNet / Java / Scripts / Linux / PHP Ask - La ask - La Answer

Hex Conversion

Hi,

I am using SQL 2000 and have a query about hex conversion.

I have a int datatype which I need to convert to hex and store the result as a char.

I have been playing with casting the decimal type to Binary which works fine, however I can't then get what is displayed into a char field as the conversion from binary to char seems to be using the ascii chars to display the string.

For example:

SELECT CAST(379 AS VARBINARY)
returns:
0x0000017B

However, converting this to a Char field returns an empty string:

SELECT CAST(CAST(379 AS VARBINARY) AS VARCHAR)
returns

(1 row(s) affected)

Can anyone help?

Many thanks,

-Graham
[739 byte] By [GRussell31] at [2007-11-11 7:46:25]
# 1 Re: Hex Conversion
Mate,

take a look at teh (undocumented) function: master.dbo.fn_varbintohexstr

Example:
declare @s nvarchar(4000)

SELECT @s= master.dbo.fn_varbintohexstr(CAST(379 AS VARBINARY) )

select @s

HTH,

Cheers,

Paul
PaulMcM at 2007-11-11 23:47:40 >
# 2 Re: Hex Conversion
I would buy you a drink if I could!
:)

Many, many thanks!

-Graham
GRussell31 at 2007-11-11 23:48:41 >