2

I'm trying to add a 1 in front of my binary code and this is how I'm going about it: if I have 0101, for example, then I'd add a number with 4 zeroes, like 10000 so it would become 10101. Here's my code:

$fill = strlen($string);
$number = '1';
$add = str_pad($number, $fill, '0', STR_PAD_RIGHT);
$m1 = $string + $add;

The problem is the output for that is something like 1.random number e+Random number

2 Answers 2

1

assuming $string is your "0101" string, you could just do $m1 = '1'.$string;

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

1 Comment

I haden't figured on that since I was assuming I had to use math.
0

My previous answer was wrong because the length of the string is potentially variable and str_pad requires you to know the length. This will work, but it doesn't look so elegant:

if (strpos($string, '0') === 0) {
   $string = '1' . $string;
}

Comments

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.