-2

I have a hexadecimal number that I want to store in a database (PostgreSQL) using Eloquent

I tried the following

Migration

$table->bigInteger('battery')->nullable()->default(null);

Storing

$model->battery = hexdec($value)

This only works when $value such as e.g. 1000000010,000000000

But it doesn't work when $value = FFFFFFF0002D4004

How can I store this kind of data

Emmanuel Mtali
  • 4,383
  • 3
  • 27
  • 53
  • 1
    A hex number would have to go in a text, char column – RiggsFolly Jun 08 '22 at 15:23
  • 1
    `bigInteger` is an integer only. If you're going to use hex values (which includes letters), the column should be a string, char, or binary. See https://stackoverflow.com/questions/47166199/best-datatype-to-store-hexidecimal-and-hex-characters-in-the-database – aynber Jun 08 '22 at 15:23

1 Answers1

0

A number is a number no matter if it is in hex format or decimal one, what matters is in which format you want to save it in.

Now, in PHP hexdec() can return an integer or a float.

The float version of the result is returned when the result exceeds the size on a signed integer.

for example FFFF FFF0 002D 4004 exceeds the limit 7FFF FFFF FFFF FFFF of the signed integer 9223372036854775807 (in 64bit version of PHP) and will be returned as a float.

To check the limit of your php installation, do echo PHP_INT_MAX;

once a float, you can't save it in an integer field in your database.

If your value is supposed to reach the unsiged 64bit integer limit, then i suggest you save it as a string.

N69S
  • 16,110
  • 3
  • 22
  • 36