Monday 15 February 2010

sql server 2008 - SQL: Convert an integer into a hex string? -


How do you change an integer in a string of hex? I want to convert int to a format, which I can use as a color on my page as '#fif 0000'. For example:

  - this   

and I want to do something like this:

  '#' < / Code>  

But by changing a varbinary string it is converted to an ASCII character rather than returning the actual hex string.

Generating hex strings with binary values The function is created for

  SELECT '#' + sys.fn_varbintohexstr (convert (binary (3), 0)), '#' + sys.fn_varbintohexstr (convert (binary (3) 255))   

You need binary (3) to ensure the correct length of the output string - this is incorrect, you get 4 hex digits because 0 And 255 here is 4 byte int value

  SELECT '#' + sys.fn_varbintohexstr (convert (verbini (8) 0)), '#' + Sys.fn_varbintohexstr (convert (verbinetry (8), 255))    

No comments:

Post a Comment