Convert decimal to hex using PHP - php

How to convert decimal value to hex using php?
Dec : 68-55-230-06-04-89
I want hex value like this
Hex : 44-37-E0-06-04-59 but instead of this its display 44-37-E0-6-4-59
echo $test = dechex(68)."-".dechex(55)."-".dechex(230)."-".dechex(06)."-".dechex(04)."-".dechex(89);
It's give me output : 44-37-E0-6-4-59 // without 06-04
I want output something like 44-37-E0-06-04-59

You can try sprintf
sprintf('%02X-%02X-%02X-%02X-%02X-%02X', 68, 55, 230, 6, 4, 89);

For getting 44-37-E0-06-04-59 just add 0 as below,
sprintf('%02X-%02X-%02X-%02X-%02X-%02X', 68, 55, 230, 06, 04, 89);

$dec= "68-55-230-06-04-89";
$arr= split("-",$dec);
foreach($arr as $key => $num)
{
$arr[$key]=sprintf('%02X',dechex ($num));
}
//required results Array ( [0] => 44 [1] => 37 [2] => e6 [3] => 6 [04] => 4 [05] => 59 )
print_r($arr);

something like that should work if you assume that there are maximum of FF between the dashes:
$foo = explode("-", $dec);
foreach ($foo as $dec_value) {
echo ($dec_value > 16) ? dechex($dec_value) : "0".dechex($dec_value);
}

Related

Get parameters from hex string with defined lengths in php

How to get the 7 variables from this string "c0dbdc000081aa02000000000001c0", defining lengths for each one, some times it comes with voids in Zeros.
Definition to get the data from string:
0xc0->[SLIP Start char:C0]
0xdb,0xdc[C0]->Flag|Version:c,0
0x00->Reserved:0
0x00,0x81->Packet length:129
0xaa,0x02->Packet command:AA02[hex]
0x00,0x00->CRC check:0[hex]
0x00,0x00,0x00,0x01->Serial number:1
0xc0->[SLIP End char:C0]
Example: this are 3 strings with the same data, but 2 of them are shorter in the "Packet length" value, they dont have the 2 leading zeros. (added some spacing to show the missing zeros)
"c0000000 9aaa02000000000029c0"
"c0000000 85aa0200000000000ac0"
"c0dbdc000081aa02000000000001c0"
The code i have works with the last one, but the first ones will get messed up because of missing Zeros. Any ideas on how to manage this?
$inputSample = "C0DBDC000081AA02000000000001C0";
$header = array(
"start" => 2,
"flagVersion" => 4,
"reserved" => 2,
"packetLenght" => 4,
"packetCommand" => 4,
"CRCcheck" => 4,
"serialNumber" => 8,
"end" => 2
);
print_r(ParseIrregularString($inputSample, $header));
function ParseIrregularString($string, $lengths) {
$parts = array();
foreach ($lengths as $StringKey => $position) {
$parts[$StringKey] = substr($string, 0, $position);
$string = substr($string, $position);
}
return $parts;
}
Good Result "C0DBDC000081AA02000000000001C0"
Array
(
[start] => C0
[flagVersion] => DBDC
[reserved] => 00
[packetLenght] => 0081
[packetCommand] => AA02
[CRCcheck] => 0000
[serialNumber] => 00000001,
[end] => C0
)
Bad Result "c00000009aaa02000000000029c0"
Array
(
[start] => c0
[flagVersion] => 0000
[reserved] => 00
[packetLenght] => 9aaa
[packetCommand] => 0200
[CRCcheck] => 0000
[serialNumber] => 000029c0
[end] =>
)

PHP: Filtering too hight and too low numbers in array

I have an array like this: 102, 97, 101, 1, 107, 95, 555.
I need to exclude numbers, which very differ from other. So array should be: 102, 97, 101, 107, 95.
How can I do this in php?
function getAverageArray($min, array $arr){
$arr2 = array($arr[0]);
foreach(array_slice($arr,1) as $val)
if ($val - $arr[0] < $min && $arr[0] - $val < $min)
$arr2[] = $val;
return $arr2;
}
//the minimum difference necessary
$min = 90;
$arr = array(102, 97, 101, 1, 107, 95, 555);
//Array ( [0] => 102 [2] => 97 [3] => 101 [4] => 107 [5] => 95 )
print_r(getAverageArray($min,$arr));
That's possible but you have to set the threshold.
get the average of all the values
exclude values whose `abs(value-avergage) > your_threshold``

PHP: Most frequent value in array

So I have this JSON Array:
[0] => 238
[1] => 7
[2] => 86
[3] => 79
[4] => 55
[5] => 92
[6] => 55
[7] => 7
[8] => 254
[9] => 9
[10] => 75
[11] => 238
[12] => 89
[13] => 238
I will be having more values in the actual JSON file. But by looking at this I can see that 238 and 55 is being repeated more than any other number. What I want to do is get the top 5 most repeated values in the array and store them in a new PHP array.
$values = array_count_values($array);
arsort($values);
$popular = array_slice(array_keys($values), 0, 5, true);
array_count_values() gets the count of the number of times each item appears in an array
arsort() sorts the array by number of occurrences in reverse order
array_keys() gets the actual value which is the array key in the results from array_count_values()
array_slice() gives us the first five elements of the results
Demo
$array = [1,2,3,4,238, 7, 86, 79, 55, 92, 55, 7, 254, 9, 75, 238, 89, 238];
$values = array_count_values($array);
arsort($values);
$popular = array_slice(array_keys($values), 0, 5, true);
array (
0 => 238,
1 => 55,
2 => 7,
3 => 4,
4 => 3,
)
The key is to use something like array_count_values() to tally up the number of occurrences of each value.
<?php
$array = [238, 7, 86, 79, 55, 92, 55, 7, 254, 9, 75, 238, 89, 238];
// Get array of (value => count) pairs, sorted by descending count
$counts = array_count_values($array);
arsort($counts);
// array(238 => 3, 55 => 2, 7 => 2, 75 => 1, 89 => 1, 9 => 1, ...)
// An array with the first (top) 5 counts
$top_with_count = array_slice($counts, 0, 5, true);
// array(238 => 3, 55 => 2, 7 => 2, 75 => 1, 89 => 1)
// An array with just the values
$top = array_keys($top_with_count);
// array(238, 55, 7, 75, 89)
?>

PHP - Set first array's values to second array's iterations

I'm trying to make one array set to the iterations of another array. I'm working on a hash algorithm that takes in a user value of the order they want the array. It takes their code and breaks it down into 40 blocks of binary to be converted into hexadecimal. So far I'm able to change the iteration order, but it only takes the last value of the first array and sets as the value for each iteration of the second array.
The first array looks like this (Showing only 10 of the 40 to save space):
Array
(
[0] => 0111
[1] => 1000
[2] => 0110
[3] => 0010
[4] => 0011
[5] => 0001
[6] => 0011
[7] => 0010
[8] => 0011
[9] => 0101
)
The second one is like this:
Array
(
[3] => 0101
[2] => 0101
[1] => 0101
[6] => 0101
[5] => 0101
[4] => 0101
[9] => 0101
[8] => 0101
[7] => 0101
[0] => 0101
)
And here is the PHP code:
$arrayDump = $test->binarySplit($name);
$ordered = array();
$orderKey = array(3, 2, 1, 6, 5, 4, 9, 8, 7, 12, 11, 10, 15, 14, 13, 18, 17, 16, 21, 20, 19, 24, 23, 22, 27, 26, 25, 30, 29, 28, 33, 32, 31, 36, 35, 34, 39, 38, 37, 0);
foreach ($orderKey as $key) {
for ($i = $key; $i < count($arrayDump); $i++) {
$ordered[$key] = $arrayDump[$i];
}
}
The class call above isn't too important for this problem that I can tell. The $arrayDump is the first array; $ordered is the second. As you can tell, the second array changes the iteration to be what I want, but it only contains the last value from the first array. I threw it through a loop to try and get each value, but I'm at a loss. Any help would be appreciated.
You don't need the second loop, try this:
foreach ($orderKey as $key => $value) {
$ordered[$key] = $arrayDump[$value];
}

How to remove integers in array less than X?

I have an array with integers of values from 0 to 100. I wish to remove integers that are less than number X and keep the ones that are equal or greater than number X.
A little ugly using the clunky create_function, but straight forward:
$filtered = array_filter($array, create_function('$x', 'return $x >= $y;'));
For PHP >= 5.3:
$filtered = array_filter($array, function ($x) { return $x >= $y; });
Set $y to whatever you want.
Smarter than generating an array that is too big then cutting it down to size, I recommend only generating exactly what you want from the very start.
range() will do this job for you without the bother of an anonymous function call iterating a condition.
Code: (Demo)
$rand=rand(0,100); // This is your X randomly generated
echo $rand,"\n";
$array=range($rand,100); // generate an array with elements from X to 100 (inclusive)
var_export($array);
Potential Output:
98
array (
0 => 98,
1 => 99,
2 => 100,
)
Alternatively, if you truly, truly want to modify the input array that you have already generated, then assuming you have an indexed array you can use array_slice() to remove elements using X to target the starting offset and optionally preserve the indexes/keys.
Code: (Demo)
$array=range(0,100);
$rand=rand(0,100); // This is your X randomly generated
echo $rand,"\n";
var_export(array_slice($array,$rand)); // reindex the output array
echo "\n";
var_export(array_slice($array,$rand,NULL,true)); // preserve original indexes
Potential Output:
95
array (
0 => 95,
1 => 96,
2 => 97,
3 => 98,
4 => 99,
5 => 100,
)
array (
95 => 95,
96 => 96,
97 => 97,
98 => 98,
99 => 99,
100 => 100,
)

Categories