Skip to content
Advertisement

converting varchar to binary in mysql?

I have the following data in a table like so:

The data looks like this:

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

I tried these queries but getting the same result:

How to do this?

My expected result is should be like this:

Advertisement

Answer

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

TLDR;

The result can be achieved in one step:

Explanation

Starting point is a string containing 0b1011:

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

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.)

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

Explanation for hexadecimal

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

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

Or you can use CONV to get a numerical representation:

User contributions licensed under: CC BY-SA
6 People found this is helpful
Advertisement