strtobin

Description

Converts a sequence of alphanumeric characters to their equivalent hexadecimal digits.

Syntax

select strtobin(“string of valid alphanumeric characters”)

Parameters

string of valid alphanumeric characters

is string of valid alphanumeric characters, which consists of [1 – 9], [a – f] and [A – F].

Examples

Example 1

Converts the alphanumeric string of “723ad82fe” to a sequence of hexadecimal digits:

select strtobin("723ad82fe")
go
-----------------------------------
0x0723ad82fe

The in-memory representation of the alphanumeric character string and its equivalent hexadecimal digits are:

Alphanumeric character string (9 bytes)

0

7

2

3

a

d

8

2

f

e

Hexadecimal digits (5 bytes)

0

7

2

3

a

d

8

2

f

e

The function processes characters from right to left. In this example, the number of characters in the input is odd. For this reason, the hexadecimal sequence has a prefix of “0” and is reflected in the output.

Example 2

Converts the alphanumeric string of a local variable called @str_data to a sequence of hexadecimal digits equivalent to the value of “723ad82fe”:

declare @str_data varchar(30)
select @str_data = "723ad82fe"
select strtobin(@str_data)
go
----------
0x0723ad82fe

Usage

Standards

ANSI SQL – Compliance level: Transact-SQL extension.

Permissions

Any user can execute strtobin.

See also

Function bintostr