1

In an attempt to create a column with 32-bit binary numbers

/**
 * Run the migrations.
 *
 * @return void
 */
public function up()
{
    Schema::create('test_binary', function (Blueprint $table) {
        $table->increments('id');
        $table->char('binary_number', 32)->charset('binary'); // From: https://stackoverflow.com/a/62615777/5675325
        $table->timestamps();

    });
}

namely

$table->char('binary_number', 32)->charset('binary');

When I view it through HeidiSQL I can see that it is of type BINARY with size 32.

enter image description here

When creating the seeder to populate with the desired data, I tried

/**
 * Run the database seeds.
 *
 * @return void
 */
public function run()
{
   DB::table('test_binary')->insert([
    'id' => 1,
    'binary_number' => 2,
    'created_at' => now(),
    'updated_at' => now()
    ]);

It turns out that if I use 2 or 101 in binary_number, I will get the result in DB to be 2 or 101, respectively.

When I try 000000000000000000000000000000000010 (which equals 2 in 32-bit binary) and 000000000000000000000000000001100101 (which equals 101 in 32-bit binary)

'binary_number' => 00000000000000000000000000000010,

then I get the values 8 and 294977, respectively.

However, what I am looking for is for the 2 to be stored as 0000000000000000000000000000000000000010 and the 101 to be 000000000000000000000000000001100101.

Tiago Martins Peres
  • 14,289
  • 18
  • 86
  • 145

1 Answers1

2

what you are seeing is the number being encoded in base 8, this is because you are prefixing it with a 0 (followed by more 0s) which makes 010 base 8 == 8 base 10.

In PHP to indicate that a number is binary use 0b10 so your code would be:

'binary_number' => 0b00000000000000000000000000000010, // this is 2 in decimal

If you want the decimal number 10 to be stored in the database just use:

'binary_number' => 10,

Note that it appears that the translation to binary is happening on the database end so you don't actually need to pass the binary number if you don't want to.

If you want to force the number into a binary string of the given length you can use

$binaryString = str_pad(base_convert(2, 10, 2),32,'0',STR_PAD_LEFT);  // '00000000000000000000000000000010'

More details at https://www.php.net/manual/en/language.types.integer.php

apokryfos
  • 38,771
  • 9
  • 70
  • 114
  • what if I wanted `2` to be stored as `0000000000000000000000000000000000000010`? – Tiago Martins Peres Mar 26 '21 at 09:51
  • Thank you. You're welcome to answer [here](https://es.stackoverflow.com/q/438484/108660) and [here](https://pt.stackoverflow.com/q/501266/116751) and I'll translate to both Spanish and Portuguese (in case you don't know how). – Tiago Martins Peres Mar 26 '21 at 10:02
  • 1
    I think you can just copy this answer there and maybe link back here if you want. By the way just to note that there may have been a misunderstanding on what the `BINARY` charset actually means in HeidiSQL. It's not intended to be used for binary numbers, but binary strings, that is strings that don't have any character encoding associated with them. – apokryfos Mar 26 '21 at 10:08
  • I've just done that and linked it here. What'd you suggest instead? – Tiago Martins Peres Mar 26 '21 at 10:17
  • 1
    I think [bit](https://mariadb.com/kb/en/bit/) is what you need here, though I'm not fully sure that Laravel supports that so you might need to use `DB::raw("b'000000010'")` to insert data to bit columns – apokryfos Mar 26 '21 at 11:23
  • Thank you! [Just asked a question specific to that topic](https://stackoverflow.com/q/66816511/5675325) – Tiago Martins Peres Mar 26 '21 at 11:59