I find myself in need of performing bit-level conversion on variables in PHP. In more detail, I have a bit stream that is read as an integer by hardware, and I need to do some operations on the bits to make it into what its actually supposed to be (a float). I have to do this a few times for different formats, and the functionality I need is
Being able to select and move individual bits in a variable
Being able to cast statically one type of variable to the other (ie. int to float)
I know php natively supports bitwise AND, OR, etc, and shift operations, but I was wondering if:
there may already be a library in php that does this sort of thing
I would be better off with delegating the calculations to some other language
Thanks,
Is it pack and unpack that you want?
For converting bit stream to whatever you need use unpack().
Once converted to integer, you can use bitwise operators. Note, that they don't support floats, so in case of float it'd first be casted to integer.
http://php.net/manual/en/language.operators.bitwise.php
Related
I need to ask that what is string in PHP. Is it an array in PHP or not. Please give true justifications.
A string in PHP is essentially a byte array (but not in the sense of a PHP's "array"); i.e., it's a buffer with only one piece of meta-data -- the size of the buffer.
An array in PHP is a double-linked hash table map, where the keys can be integers, strings, or a mixture of both.
In terms of PHP's type system, strings and arrays are two of the basic types.
You can read the documentation about php strings at
http://www.php.net/manual/en/language.types.string.php
http://php.net/manual/en/language.types.array.php
In PHP, a string is a primitive type, meaning it's not an array. See here for the other primitive types supported by PHP.
Is there a way to explicitly store my numbers in php as tinyint (1 byte instead of 4).
Or could i only enforce this by storing them 4 by 4 in an int? (using a few binary operations)
I generate these values by breaking a string using str_split and interpretting these bytes as ints via unpack( 'C' , .. ).
Currently i store these values in an array as invdividual integers but it could save alot of space if i could store them somehow as tinyints.
PHP has two data types that you may want to use here: integer and string.
PHP doesn't have any other types you could choose from (float wouldn't be a good choice for integers, the other types are not appropriate).
An int is usually 32 or 64 bits, a string is 1 byte per character.* I propose that unless you have a lot of numbers, you won't ever see any problem with 32 bit ints. If you absolutely positively want to safe space memory** and your numbers have a maximum of 3 digits, you could handle your numbers as strings. There's even the BCMath extension that'll let you operate on string numbers directly without needing to cast them back and forth. It's quite a lot of hassle for possibly very limited gain though.
Seeing that a MySQL TINYINT is usually used for boolean values though, please be aware PHP does have a boolean type...!
* One byte per one-byte character, that is.
** Since PHP scripts are usually only very temporary, you should only have problems with peak memory usage, not storage space. Getting more RAM may be the more efficient solution than playing with types.
People know all about storing binary data in database server as BLOBs. How would one accomplish the same thing in PHP?
In other words, how do i store blobs in a php variable?
As PHP doesn't have Unicode support you can safely use normal strings as binary storage. Most (all?) functions are null-safe, too, so you shouldn't get any problems because of that either.
PS: Theoretically you could prefix all binary strings with b (e.g. b'binary data'). This is a forward compatability token to make sure that strings that expect to be handled as binary will really be handled so even than Unicode support is available.
Easy - store it in a string. You can use all the normal string functions (strlen, substr, etc) - just remember that the PHP string functions work in single byte units, e.g. substr($binstr, 0, 1) gives you the first 8 bits of $binstr
Maybe as an array of bytes. After all binary data is nothing more.
I want to save a 11 digit number inside a variable. How can I do this in PHP?
The 64-bit version of PHP uses 64-bit integers natively, so that's plenty of bits for an 11-digit number. That said, if you need larger integers, I would use the BC Math extension.
The GMP library is also worth looking at.
Yes, mediumint and bigint is there.
I'm writing a PHP library that has a Number class that uses the bcmath extension for arbitrary precision.
I have two questions:
How much slower is bcmath compared to using the built-in int and float types?
bcmath has an optional scale argument (that defaults to 3 digits). For an general purpose Number class that anyone could use, what would be a good level of precision? How do languages like Perl (that have arbitrary precision numbers) deal with scale?
I would decide what range of numbers you need to support. The built in values will be faster than any value that requires calculation and conversion to/from some other format.
Built in integers are good until 32 bits on any system, some systems support 64 bit values. You can check what your system supports by checking the value of the constant PHP_INT_MAX and determine if you want to carry the overhead of the math library after that. For systems with 32 bit integers, anything above 32 bits will be converted to a float automatically. This isn't an issue unless you are using built in functions for things like round, printf, modulus etc.
I was bit by this using modulus to divide traffic coming to my site as well as with formatting integers using %d in sprintf: http://af-design.com/blog/2009/10/28/php-64-bit-integer-modulus-almost/