0

I have binary stored in string

$str = "100001111111110000101010100010000001";

that I get from converting numbers array

$arrAsc = Array
(
    [0] => 33
    [1] => 63
    [2] => 48
    [3] => 42
    [4] => 34
    [5] => 1
)
$arrBinary = array_map(create_function('$a', 'return decbin($a);'), $arrAsc);

after breaking the value out into 6-bit chunks I have

Array
(
    [0] => 100001
    [1] => 111111
    [2] => 110000
    [3] => 101010
    [4] => 100010
    [5] => 1
)

my question is how to show zeros first even if there 5,6,7.... bit chunks

Array
(
    [0] => 100001
    [1] => 111111
    [2] => 110000
    [3] => 101010
    [4] => 100010
    [5] => 000001
)
2
  • I'm just curious, why would you do that? Commented Mar 1, 2012 at 0:01
  • @Jeff Pigarelli I prepare myself to php exam. try to build base64_decode() for better understanding php Commented Mar 1, 2012 at 0:04

5 Answers 5

2

You can use array_map() and str_pad() to add leading zeroes to each string. If you want only the last 6 bits (in cases where there might be 7 or more bits), you might also want to use substr():

<?
// Create our array
$chunks = array('100001', '111111', '110000', '101010', '100010', '1');

// Apply an anonymous function to each chunk
$chunks = array_map(
  function($x) {
    // Get only 6 last bits (might not be necessary, depending on your needs)
    $x = substr($x, -6);
    // Add leading zeroes
    $x = str_pad($x, 6, '0', STR_PAD_LEFT);
  },
  $chunks
);

print_r($chunks);
// Array
// (
//     [0] => 100001
//     [1] => 111111
//     [2] => 110000
//     [3] => 101010
//     [4] => 100010
//     [5] => 000001
// )
Sign up to request clarification or add additional context in comments.

Comments

1

To expand on Frxstrem's (first) answer, this can be done in one go, without using array_map to first convert the numbers, and then again to pad them:

$arrAsc = array(33, 63, 48, 42, 34, 1);

$array_binary = array_map(function($dec) {
    return str_pad(decbin($dec), 6, '0', STR_PAD_LEFT);
}, $arrAsc);

Array
(
  [0] => 100001
  [1] => 111111
  [2] => 110000
  [3] => 101010
  [4] => 100010
  [5] => 000001
)

Comments

0

What you are really asking to do is pad a string with leading zeros.

Use something like:

$paddedString = str_pad($oldString, 6, '0', STR_PAD_LEFT);

This would obviously be suitable for use in an array_map(), which you already know how to use.

Comments

0

You can use printf to format the string:

<?php

$str = "1111111000011010101001000000011";
$words = array();

foreach (str_split($str, 6) as $word) {
    $words[] = sprintf("%06s", $word);
}

print_r($words);

Which will output:

Array
(
    [0] => 111111
    [1] => 100001
    [2] => 101010
    [3] => 100100
    [4] => 000001
    [5] => 000001
)

Comments

0

This should do the trick.

if (strlen($a) < 6) {
    $i = (6-strlen($a));
    $add = '';
    for ($x = 0; $x <= $i; $i++) {
        $add .= '0';
    }
    $a = $add.$a;
}

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.