Karan Gupta
Karan Gupta

Reputation: 529

converting varchar to binary in mysql?

I have the following data in a table like so:

create table tutor(
   id int,
   accessto varchar(8)
);

The data looks like this:

+-------+----------+
| id    | accessto |
+-------+----------+
|    69 | b'1011'  |
|   162 | b'1011'  |
|   232 | b'1011'  |
|   257 | b'0010'  |
|   258 | b'1011'  |
|   258 | b'1011'  |
|   258 | b'1011'  |
|   258 | b'1011'  |
|   258 | b'1011'  |
|   258 | b'1011'  |
|   258 | b'1110'  |
|   258 | b'1001'  |
|   258 | b'0011'  |
|   258 | b'1001'  |
+-------+----------+

I want to convert this into binary. How is it possible?

I tried these queries but getting the same result:

select id, cast(accessto as BINARY) from tutor;
select id, convert(accessto,binary) from tutor;

How to do this?

My expected result is should be like this:

 +-------+----------+
    | id    | accessto |
    +-------+----------+
    |    69 | 11  |
    |   162 | 11  |
    |   232 | 11  |
    |   257 | 2  |
    |   258 | 11  |
    |   258 | 11  |
    |   258 | 11  |
    |   258 | 11  |
    |   258 | 11  |
    |   258 | 11  |
    |   258 | 14  |
    |   258 | 9  |
    |   258 | 3  |
    |   258 | 9  |
    +-------+----------+

Upvotes: 0

Views: 13824

Answers (4)

Ulrich Thomas Gabor
Ulrich Thomas Gabor

Reputation: 6654

Since the accepted answer is unnecessarily complex, here a concise answer:

TLDR;

The result can be achieved in one step:

SELECT CONV(TRIM("'" FROM SUBSTRING("0b'1011'", 3)), 2, 16);

Explanation

Starting point is a string containing 0b1011:

SET @string = "0b'1011'"; -- (string) 0b'1011'

We can apply string operations to get it closer to a number:

SET @plainnumber = TRIM("'" FROM SUBSTRING(@string, 3)); -- (string) 1011

We can then convert the string into a real number, without sign because there is none: (You cannot use binary as a type here, because it is a string type. This step is optional because MySQL will cast implicitly.)

SET @number = CAST(@plainnumber AS UNSIGNED); -- (unsigned) 1011

You can now do whatever you want with it. The OP wanted to get the decimal representation which can be acquired with CONV:

SELECT CONV(@number, 2, 10);

Explanation for hexadecimal

If your starting point is a string containing a hexadecimal representation:

SET @string = "41"; -- 0x41

You can use the UNHEX function to get a binary string:

SELECT UNHEX(@string);

Or you can use CONV to get a numerical representation:

SELECT CONV(@string, 16, 10); -- decimal (65)
SELECT CONV(@string, 16, 2); -- binary (0100 0001)

Upvotes: 2

P.Salmon
P.Salmon

Reputation: 17665

You may have to do this the 'hard' way and create a bit field either in tutor or another table, internal conversion seems to work better than cast and convert

drop table if exists t;
create table t(
   id int,
   accessto varchar(8),
   abit bit(8)
);


insert into t(id,accessto) values
(    69 , b'1011') , 
(   162 , b'1011') , 
(   232 , b'1011') , 
(   257 , b'0010') , 
(   258 , b'1011')  ,
(   258 , b'1011') , 
(   258 , b'1011') , 
(   258 , b'1011') , 
(   258 , b'1011') , 
(   258 , b'1011') , 
(   258 , b'1110') , 
(   258 , b'1001') , 
(   258 , b'0011') , 
(   258 , b'1001') ;

update t
set abit = accessto;

    MariaDB [sandbox]> select bin(abit),cast(abit  as unsigned) from t;
+-----------+-------------------------+
| bin(abit) | cast(abit  as unsigned) |
+-----------+-------------------------+
| 1011      |                      11 |
| 1011      |                      11 |
| 1011      |                      11 |
| 10        |                       2 |
| 1011      |                      11 |
| 1011      |                      11 |
| 1011      |                      11 |
| 1011      |                      11 |
| 1011      |                      11 |
| 1011      |                      11 |
| 1110      |                      14 |
| 1001      |                       9 |
| 11        |                       3 |
| 1001      |                       9 |
+-----------+-------------------------+
14 rows in set (0.00 sec)

Upvotes: 1

Jan de Nysschen
Jan de Nysschen

Reputation: 127

-- Replace 'binary' with 'UNSIGNED'

select id, cast(accessto as UNSIGNED) from tutor;

Upvotes: -1

kellymandem
kellymandem

Reputation: 1769

You should using CONV mysql function

select id, CONV(BINARY(accessto), 2, 10) from tutor;

Upvotes: -1

Related Questions