echo intval(chr(255));
I don't understand...
The chr() function turns a byte into its ASCII equivalent and intval() function gets the integer value of a variable.
If we were to break the statement into two different lines, this would be:
$a = chr(255); // $a is now a string
echo intval($a);
If you check intval()'s documentation you will notice that:
Strings will most likely return 0
although this depends on the leftmost
characters of the string. The common
rules of integer casting apply.
That's why the result is zero.
The byte 0xFF does not represent a digit in either octal, decimal or hexadecimal what intval is looking for. You probably wanted the ord function.
To output 255, you need:
echo intval(ord(chr(255)));
There are 128 ordinal numbers in ASCII, the 255 comes out to be ÿ so when you convert it to a number with intval, it will be 0.
Because chr delivers a string, in this case with just one character, the character 0xFF, or better known as ÿ.
intval on the other hand does a conversion from a string to an integer based on the content of the string, and not the characters.
echo intval("33"); // will print 33
echo intval("10", 8); // will print 8
echo intval("0xFF", 16); // will print 255
echo intval("m"); // will print zero...
//you can't convert letters like that to numbers.
chr(255)
returns a character corresponding to ASCI 255
and intval try to bring out integer part from a variable
since chr(255) returns a non-numeric character so intval get no int value and return 0
Related
I am trying to convey $string to float. The type of $string is string in the format of "0.0111455667". I have the following code in php.I have tried all these methods and I got 0 for all of them. how can I convert the string to float?why I always get 0?
PS: please do not assume my question as duplicate, I have already tried all the methods in the similar questions and none of them worked for me.
$float = (float) $string;
//$float2 = $string + 0.0; //this works as well.
$floatval = floatval($string);
$double = (double) $string;
// TEST
echo $string;
echo $float;
//echo $float2;
echo $floatval;
echo $double;
Your problem is the content of $string.
In PHP, every time you convert a string to a float, be it by casting (float) $string, or floatval($string), you will always get 0 if the first character of the string is not numeric.
From PHP docs: String conversion to numbers
The value is given by the initial portion of the string. If the string starts with valid numeric data, this will be the value used. Otherwise, the value will be 0 (zero)
Double-check the content of $string. Probably you have some spurious characters at its beginning.
I need to convert a dec to Hex like following:
echo $val = dechex(-29338);
result = ffff8d66
May I know how to "throw away all those ffff and finally return me 8d66? I am using bitwise like following but it doesn't help
echo $val & 0xFFFF ;
dechex() result is as string so bitwise operation will not work. Use string function like str_replace() or substr()
In your case, a negative number is a signed integer so the FFFF are legit. Removing it will result in 8d66, and 8d66 is equal to 36198 !
I have the hex representation and I'm trying to convert this to the type of hex. For example if you execute:
echo "\xFF\xAA";
echo '<br>';
$first = "FF";
$second = "AA";
echo "\x$first\x$second";
die();
The result you get is:
So the first line is weird symbols. This indicates to me this is actually of type hex. Where the second result although is valid representations of hex, they are in fact not actually hex.
So my question is how can I make the actual result hex? I want to be able to use a variable as the representation but actually convert it to hex. (I want line two to show up as symbols)
hex2bin — Decodes a hexadecimally encoded binary string
<?php
$hex = hex2bin("6578616d706c65206865782064617461");
echo $hex;
?>
Output:
example hex data
Source: http://php.net/manual/en/function.hex2bin.php
So just use
echo hex2bin("$first$second");
I have hardware unit, that when requested some data, returns a string, that when exploded on space, returns array of values:
$bytes = array(
'03',
'80',
'A0',
'01' // and others, total of 240 entries
);
These actually, depict bytes: 0x03, 0x80, 0xA0, 0x01. I need to transform them into their actual values.
I have tried in a loop, to: $value = 0x{$byte}, $value = {'0x' . $byte} and others, to no avail.
Also tried unpack, but don't know what format to apply, am kind of clueless about bytes.
Seems like a basic issue, yet cannot wrap my head around it.
How can I dynamically, transform them into their actual integer values?
use chr if you want a string
$value = chr($byte);
use hexdec if you want an integer
$value = hexdec($byte);
In PHP, bytes are the same as one-character long strings, with the following escaping:
$byte = "\x03";
There is a function that can help you, which is chr().
This function take as parameter the ASCII code of the byte you want to obtain. As it can be either a numeric string or an integer, you can use
$code = "03";
$byte = chr("0x" . $code);
to obtain the '\x03' byte, with the parameter to chr being interpreted as an hexadecimal integer.
On the other hand, as mentionned by #chumkiu, if you are trying to obtain integer values, the following code will work:
$code = "03";
$int = hexdec($code);
I think something like this will be sufficient:
foreach($bytes as byte)
{
echo hexdec($byte);
}
See also the hexdec manual.
If $string is the raw data (hex digits separated by spaces), then you can extract the binary data like this:
$binary = pack('H*',str_replace(' ','',$string));
I have the follow code:
<?
$binary = "110000000000";
$hex = dechex(bindec($binary));
echo $hex;
?>
Which works fine, and I get a value of c00.
However, when I try to convert 000000010000 I get the value "10". What I actually want are all the leading zeros, so I can get "010" as the final result.
How do I do this?
EDIT: I should point out, the length of the binary number can vary. So $binary might be 00001000 which would result it 08.
You can do it very easily with sprintf:
// Get $hex as 3 hex digits with leading zeros if required.
$hex = sprintf('%03x', bindec($binary));
// Get $hex as 4 hex digits with leading zeros if required.
$hex = sprintf('%04x', bindec($binary));
To handle a variable number of bits in $binary:
$fmt = '%0' . ((strlen($binary) + 3) >> 2) . 'x';
$hex = sprintf($fmt, bindec($binary));
Use str_pad() for that:
// maximum number of chars is maximum number of words
// an integer consumes on your system
$maxchars = PHP_INT_SIZE * 2;
$hex = str_pad($hex, $maxchars, "0", STR_PAD_LEFT);
You can prepend the requisite number of leading zeroes with something such as:
$hex = str_repeat("0", floor(strspn($binary, "0") / 4)).$hex;
What does this do?
It finds out how many leading zeroes your binary string has with strspn.
It translates this to the number of leading zeroes you need on the hex representation. Whole groups of 4 leading zero bits need to be translated to one zero hex digit; any leftover zero bits are already encoded in the first nonzero hex digit of the output, so we use floor to cast them out.
It prepends that many zeroes to the result using str_repeat.
Note that if the number of input bits is not a multiple of 4 this might result in one less zero hex digit than expected. If that is a possibility you will need to adjust accordingly.