Laravel int 转 32 位二进制(反之亦然) [英] Laravel int to 32-bit binary (and vice versa)

查看:50
本文介绍了Laravel int 转 32 位二进制(反之亦然)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

尝试用 32 位二进制数创建列

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();

    });
}

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

当我通过 HeidiSQL 查看它时,我可以看到它是 BINARY 类型,大小为 32.

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

在创建播种机以填充所需数据时,我尝试了

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()
    ]);

事实证明,如果我在 binary_number 中使用 2 或 101,我将在 DB 中得到的结果分别为 2 或 101.

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.

当我尝试 000000000000000000000000000000000010(在 32 位二进制中等于 2)和 00000000000000000000000000000013 中等于 2 二进制 10 时

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

'binary_number' => 00000000000000000000000000000010,

然后我分别得到值 8 和 294977.

then I get the values 8 and 294977, respectively.

但是,我要寻找的是将 2 存储为 0000000000000000000000000000000000000010 并且将 101 存储为 00000000000000000000000p1000000000000

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

推荐答案

您看到的是以 8 为基数编码的数字,这是因为您在它前面加上了 0(后跟更多 0),这使 010 成为基数8 == 8 以 10 为底.

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.

在 PHP 中使用 0b10 表示一个数字是二进制的,所以你的代码是:

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

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

如果要将十进制数 10 存储在数据库中,只需使用:

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'

更多详细信息,请访问 https://www.php.net/手册/en/language.types.integer.php

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

这篇关于Laravel int 转 32 位二进制(反之亦然)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆