[Leaf function sharing 34] convert string to hexadecimal Function

Source: Internet
Author: User

/****************************

Convert string to hexadecimal

Author: not idle

QQ: 75492895

Email: appleak46@yahoo.com.cn

****************************/

-- Create a function (suiyunonghen (not idle ))

Create Function varchartohex (@ STR varchar (400 ))

Returns varchar (800)

As

Begin

Declare @ I int, @ ASI int, @ mod int, @ res varchar (800), @ Len int, @ CRES varchar (4), @ tempstr varbinary (400)

Select @ I = 1, @ res = '', @ Len = datalength (@ Str), @ tempstr = convert (varbinary, @ Str)

While @ I <= @ Len

Begin

Select @ ASI = substring (@ tempstr, 1,1), @ CRES =''

While @ ASI <> 0

Begin

Select @ mod = @ ASI % 16,

@ CRES = case when (@ mod> 9) then Char (ASCII ('A') + @ Mods-10) + @ CRES else cast (@ MoD as varchar (4 )) + @ CRES end,

@ ASI = @ ASI/16

End

Select @ res = @ res + @ CRES, @ tempstr = substring (@ tempstr, 2, @ len-1), @ I = @ I + 1

End

Return @ res

End

 

Go

-- Test example

Select DBO. varchartohex ('leaf ')

 

-- Running result

/*

D2b6d7d3

*/

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.