0

I have a string like :

  $str = "01110001";

and I want to change its type to binary with :

$bin = (binary) $str;

( because I want to inverse it ) But, when I try to var_dump($bin), I get :

string(8) "01110001" 

Can anyone tell me what I'm missing ?

0

1 Answer 1

1

Binary is not datatype. Hence you can not cast with it.

However you can representation of binary to decimal using bindec function.

$bin = (string)bindec($str);

Note: Every number is stored as binary in memory. So you dont have specifically convert it to binary. Just use bindec and it'll convert it to number. Then it'll be stored in 4 byte integer as binary. If its string then for every digit it'll use 8 bytes.

Sign up to request clarification or add additional context in comments.

8 Comments

I don't want to change the type to integer. I have the binary mode of a variable but its type is string, I want to change the type to binary so I can inverse it
You can not change it to binary. You need to change it to numeric type. I have updated my answer.
but I need to act with this like binary, I want to inverse the variable : 1010001 --> 0101110 with ~
@shiplu.mokadd.im 's answer is correct but you don't need to cast to string if you need an integer. Just $bin = bindec($str); should do it.
@this.lau_ Are you guys listening to me? I dont want integer. I want binary, I want to inverse the variable like I said in the above comment
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.