Convert number to byte in PHP

php pack
php unpack
php string to bytes
php integer to bytes
php convert number to base 64
php convert number to letter
php convert file to byte array
convert image to byte array in php

In Java, I can easily cast a number to a byte, for example:

System.err.println((byte) 13020);

the result will be

-36

Now how can I achieve the same in PHP?

Maybe do a modulo 256 expression (if unsigned) or modulo 256 and minus 128.

$val = ($val % 256) - 128

This work if you only want a value. If you want a real-one-byte, maybe pack() function will help here.

Edit: Right, 0 would be 128, so maybe this solution do will works:

$val = (($val+128) % 256) - 128

decbin - Manual, Parameters �. number. Decimal value to convert $binstring = (($i == count($ bytes) - 1) ? decbin($bytes[$i]) : str_pad(decbin($bytes[$i]), 8, "0",� number: Required. Specifies the number to convert: frombase: Required. Specifies the original base of number. Has to be between 2 and 36, inclusive. Digits in numbers with a base higher than 10 will be represented with the letters a-z, with a meaning 10, b meaning 11 and z meaning 35: tobase: Required. Specifies the base to convert to.

echo ($a&0x7f) + ($a&0x80?-128:0);

edit this mimics what should actually happen for a signed 8-bit value. When the MSB (bit 7) is zero, we just have the value of those 7 bits. With the MSB set to 1, we start at -128 (i.e. 1000000b == -128d).

You could also exploit the fact that PHP uses integer values internally:

$intsize = 64; // at least, here it is...
echo ($a<<($intsize-8))/(1<<($intsize-8));

so you shift the MSB of the byte to the MSB position of the int as php sees it, i.e. you add 56 zero bits on the right. The division "strips off" those bits, while maintaining the sign of the value.

base_convert - Manual, PHP 5, PHP 7). base_convert — Convert a number between arbitrary bases VARNING! If the last byte is incomplete, it is supplemented with zeros as string! Returns the formatted number: PHP Version: 4+ Changelog: As of PHP 5.4, this function supports multiple bytes in the parameters decimalpoint and separator. Only the first byte of each separator was used in older versions.

You can't. PHP has no byte data type like Java does.

How to convert a decimal integer to bytes with php? - PHP, I need to get how much bytes is a decimal integer with php. For example how can I know if 256379 is 3-bytes with php? I need a php function to� In this step we create a form to enter bytes and select unit to convert on.After submitting the form we get the value of size and units and call convert function to convert bytes into particular unit and display in browser.You may also like convert currency using PHP.

PHP unpack() Function, to use HTML, CSS, JavaScript, SQL, PHP, Python, Bootstrap, Java and XML. little endian byte order); i - signed integer (machine dependent size and byte� Warning. base_convert() may lose precision on large numbers due to properties related to the internal "double" or "float" type used. Please see the Floating point numbers section in the manual for more specific information and limitations.

size_format( int|string $bytes, int $decimals ), Convert number of bytes largest unit bytes will fit into. If you need to place the larger size then what PHP integer type will hold, then use a� End Function Public Function ToDecimal(provider As IFormatProvider) As Decimal _ Implements IConvertible.ToDecimal If signBit = signBit.Negative Then Dim byteValue As SByte = SByte.Parse(byteString, NumberStyles.HexNumber) Return Convert.ToDecimal(byteValue) Else Dim byteValue As Byte = Byte.Parse(byteString, NumberStyles.HexNumber) Return Convert.ToDecimal(byteValue) End If End Function Public Function ToDouble(provider As IFormatProvider) As Double _ Implements IConvertible.ToDouble If

Converting bytes to human readable values (KB, MB, GB, TB, PB , <?php /** * Converts a long string of bytes into a readable format e.g KB, MB, GB, TB, YB * * @param {Int} num The number of bytes. */ function� Leave a comment on PHP Function to Convert Bytes to a Specified Unit, Whether Kilobytes, Megabytes, or Gigabytes Originally posted February 11, 2018. PHP Code Snippets. This function does not choose for you what units to convert to, but rather lets you specify what unit type you want to convert Bytes into.

Comments
  • (13020 % 256) - 128 will not give back a -36 , i am getting -128 instead
  • @shay: You should at least get 92 which is -36 modulo 128
  • I saw this has become the accepted answer. However, there's still some nasty corner cases which are not handled propery. As an example, putting in $val = -512; gives the surprising result of -256 instead of 0 (since -512 decimal == 0xfffffe00 in 32 bit) This is especially funny since -256 cannot be represented in 8 bits. So I'll make it explicit: don't use the modulo operator to do bit operations, ever. It's asking for trouble.
  • don't tell me you can't do that in PHP. it might not be straightforward but it most definitely is doable.
  • @Peter: How do you cast to a data type that doesn't exist?
  • He doesn't care about the actual data type used. He wants the number handled as a byte-cast would in java
  • he doesnt ask you to CAST it, just convert it.