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 !
Related
I've got a string representing an IPv4 address:
$ip = '\x7F\0\0\x01';
When I try to pass that to inet_ntop($ip) it's giving me grief:
PHP Warning: inet_ntop(): Invalid in_addr value
If I declare the variable manually using double quotes it works:
$ip = "\x7F\0\0\x01";
inet_ntop($ip); // "127.0.0.1"
However, I am not declaring these variables manually. I'm working with what is given to me in an object.
How can I convert '\x7F\0\0\x01' into a string that inet_ntop() will accept?
In other words, how can I make PHP parse a string literally as if I were manually declaring it with double quotes?
Some interesting facts:
gettype('\x7F\0\0\x01'); // string
gettype("\x7F\0\0\x01"); // string
ord('\x7F\0\0\x01'); // 92
ord("\x7F\0\0\x01"); // 127
implode(unpack('H*', '\x7F\0\0\x01')); // 5c7837465c305c305c783031
implode(unpack('H*', "\x7F\0\0\x01")); // 7f000001
mb_detect_encoding('\x7F\0\0\x01'); // ASCII
mb_detect_encoding("\x7F\0\0\x01"); // UTF-8
"\x7F\0\0\x01" == '\x7F\0\0\x01'; // false
// and for the haters
long2ip('\x7F\0\0\x01'); // PHP Warning: long2ip() expects parameter 1 to be integer, string given
One possibility is to parse the string into its component pieces (starting with \); convert them to the decimal equivalent and use chr to get back the original characters. These can then be joined into a string which is suitable for inet_ntop:
$ip = '\x7F\0\0\x01';
preg_match_all('/\\\x?([\dA-F]+)/', $ip, $parts);
$ip = implode('', array_map(function ($v) { return chr(hexdec($v)); }, $parts[1]));
echo inet_ntop($ip);
Another alternative is to use pack, after stripping out the \x parts and replacing \0 with 00:
$ip = '\x7F\0\0\x01';
$ip = pack('H*', str_replace(array('\x', '\0'), array('', '00'), $ip));
echo inet_ntop($ip);
In both cases the output is:
127.0.0.1
Demo on 3v4l.org
The problem is that you've got the literal ASCII output of a binary string and not the real binary value you expect it to be. I'm not sure how you got the literal ASCII value. There is a way to convert it, but you're not going to like it.
You can use eval() to accomplish what you're trying to do. All arguments for eval() being evil still apply.
$ip = '\x7F\0\0\x01';
eval("\$ip = \"$ip\";");
echo inet_ntop($ip);
This will print out 127.0.0.1.
Since binary doesn’t always result in literal ASCII characters, I worry you’ll see literal characters like � in the strings, and these won’t convert properly to the binary value you expect them to be.
For example, here are the characters printed to screen in Psysh:
>>> hex2bin('7f000001') // This is 127.0.0.1
=> "\x7F\0\0\x01"
>>> hex2bin('ffffffff') // This is 255.255.255.255
=> b"ÿÿÿÿ"
The first value looks familiar, right? That's the string literal that we can convert back into a binary string using eval(), like we did in the example above. But the binary value for ffffffff is a different story. If we try to convert it, it doesn't give us the 255.255.255.255 value we expect.
$ip = 'ÿÿÿÿ';
eval("\$ip = \"$ip\";");
echo inet_ntop($ip);
In this case, inet_ntop() returns false, but we know it should work:
>>> inet_ntop(hex2bin('ffffffff'));
=> "255.255.255.255"
So, I worry that any attempt to convert these values from string literals into binary strings is not going to work in all cases, whether using eval() or any of the other answers provided here.
However, if everything is coming to you in the format \0\0\0\0, where each "segment" is either a zero or a hex value in the format x00, then you should be in good shape, because these are the same:
>>> "\xFF\xFF\xFF\xFF"
=> b"ÿÿÿÿ"
You can make your own function like this
function convertStringToInAddr(string $string) {
$return = null;
$exploded = explode("\\", $string);
foreach($exploded as $hex) {
if( $hex != "" ) {
$return .= chr(hexdec(str_replace("x", "", $hex)));
}
}
return $return;
}
I am trying to write a regular expression such that if a number have decimal point then the zeros (0) at the end must be removed.
Example:
$value = 234.8076000
After Regexp Replace it should become
234.8076
I am trying the following regexp [0]+$ in preg_replace but the problem is that if the value does not have decimal point and it contain zero at the end then that zero is also removed.
Example:
$value = 2340
It becomes 234 but it should remain 2340
Any idea? Is there any in-built function in php that can do this?
Yes, you really can do it with regex:
$pattern = '/(\.\d*[^0])0+$/';
echo preg_replace($pattern, '$1', '2340'); // 2340
echo preg_replace($pattern, '$1', '2340.0'); // 2340.0
echo preg_replace($pattern, '$1', '2340.07600'); // 2340.076
... but the simplest way is just convert a string value into a float value.
echo (float)'2340'; // 2340
echo (float)'2340.0'; // 2340
echo (float)'2340.07600'; // 2340.076
Echoing floats that are really integer values drops the decimal part apparently - but it seems from your comments it's actually what you want.
You can do it without regexpes:
php > echo ((float)"21.40200")."\n";
21.402
/(\.\d*?)0+$/, works except /\d+\.0{n}/ cases
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.
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