Adding 2 hexadecimals as strings - php

Description
I'm trying to get the data for a decall image for a Team Fortress 2 item from steam
http://forums.steampowered.com/forums/showthread.php?t=2695676
Data used
steam inventory item attributes
"attributes": [
{
"defindex": 152,
"value": 160020469,
"float_value": 3.3154542658454145e-033
},
{
"defindex": 227,
"value": 142668139,
"float_value": 7.7604768692394667e-034
},
{
"defindex": 746,
"value": 1065353216,
"float_value": 1
},
{
"defindex": 292,
"value": 1115684864,
"float_value": 64
},
{
"defindex": 388,
"value": 1115684864,
"float_value": 64
}
]
My script
`
function Function_BigDecimal_to_Hexadecimal_Converter($dec) {
$hex = '';
do {
$last = bcmod($dec, 16);
$hex = dechex($last).$hex;
$dec = bcdiv(bcsub($dec, $last), 16);
} while($dec>0);
return $hex;
}
function Function_BigHexadecimal_to_Decimal_Converter($hex) {
$dec = 0; $len = strlen($hex);
for ($i = 1; $i <= $len; $i++) { $dec = bcadd($dec, bcmul(strval(hexdec($hex[$i - 1])), bcpow('16', strval($len - $i)))); }
return $dec;
}
$hex1 = Function_BigDecimal_to_Hexadecimal_Converter('142668139' ); // data from defindex 227
$hex2 = Function_BigDecimal_to_Hexadecimal_Converter('160020469'); // data from defindex 152
echo 'hex1 ',$hex1,'<br>'; // = 880f16b
echo 'hex2 ',$hex2,'<br>'; // = 989b7f5
$hex_append = $hex1.$hex2;
echo 'hex_append ',$hex_append,'<br>'; // = 880f16b989b7f5
$ugcid = Function_BigHexadecimal_to_Decimal_Converter($hex_append);
echo 'ugcid ',$ugcid,'<br>'; // = 38297187109156853
`
My problem
expected ucid value shoud be 612754991346202613 and I got 38297187109156853
Here i'm reversing this code to get hex_append from the expected answer
echo 'Reversed hex_append using expected answer '.Function_BigDecimal_to_Hexadecimal_Converter('612754991346202613');// = 880f16b0989b7f5
Conclusion
The difference in hex_append calculated 880f16b989b7f5 and the reversed answer 880f16b0989b7f5 is a 0
How can I fix this ?
I guess the a 0 needs to be added sometimes when adding 2 hexes as strings.
This problem was detected only for this item.
All this php code con be copyed in a php file and executed

Answer: The string must have 18 characters

Related

Notice: Undefined offset & Trying to get property of non-object

I am trying to pull data from the json_decode array, I have done it before but this time I am getting extra issues.
This is what the json looks like.
{
"achievementpercentages": {
"achievements": [
{
"name": "TF_SCOUT_LONG_DISTANCE_RUNNER",
"percent": 54.668815612792969
},
{
"name": "TF_HEAVY_DAMAGE_TAKEN",
"percent": 47.104038238525391
},
{
"name": "TF_GET_CONSECUTIVEKILLS_NODEATHS",
"percent": 44.668777465820312
},
{
"name": "TF_PYRO_CAMP_POSITION",
"percent": 36.480117797851563
},
{
"name": "TF_KILL_NEMESIS",
"percent": 34.392494201660156
},
{
"name": "TF_BURN_PLAYERSINMINIMUMTIME",
"percent": 33.580135345458984
},
{
"name": "TF_PYRO_BURN_MEDICPAIR",
"percent": 32.748367309570312
},
And my code is basically this
$achName = $decodedAch->achievementpercentages->achievements[$achCounter]->name;
$achPercent = $decodedAch->achievementpercentages->achievements[$achCounter]->percent;
The $achCounter is present in a while statement, in an effort to get all the achievements seperately. I don't think that part is wrong, just how I am trying to access the data in the array. I don't see what is wrong with it though.
Any help would be appreciated.
FULL WHILE STATEMENT AS REQUESTED
while($achCounter < $responseCount)
{
$appid = $decodedSteam->response->games[$achCounter]->appid;
$achievementURL = 'http://api.steampowered.com/ISteamUserStats/GetGlobalAchievementPercentagesForApp/v0002/?gameid='.$appid.'&format=json';
$jsonAch = file_get_contents($achievementURL);
$decodedAch = json_decode($jsonAch);
//var_dump($decodedAch);
$achName = $decodedAch->achievementpercentages->achievements[$achCounter]->name;
$achPercent = $decodedAch->achievementpercentages->achievements[$achCounter]->percent;
//echo $achievementURL;
$sql = "INSERT INTO steamid".$steamid."(name, percent) VALUES ('".$achName."','".$achPercent."')";
$achCounter++;
}
You need to iterate again after you fetch data from your url. Your $achCounter is usefull only for $decodedSteam, not for $decodedAch:
while($achCounter < $responseCount)
{
$appid = $decodedSteam->response->games[$achCounter]->appid;
$achievementURL = 'http://api.steampowered.com/ISteamUserStats/GetGlobalAchievementPercentagesForApp/v0002/?gameid='.$appid.'&format=json';
$jsonAch = file_get_contents($achievementURL);
$decodedAch = json_decode($jsonAch);
//var_dump($decodedAch);
foreach($decodedAch->achievementpercentages->achievements as $achievement) { //HERE
$achName = $achievement->name;
$achPercent = $achievement->percent;
//echo $achievementURL;
$sql = "INSERT INTO steamid".$steamid."(name, percent) VALUES ('".$achName."','".$achPercent."')";
}
$achCounter++;
}

JSON to MySql using PHP

Hi and I need your help
I'm working on a project for class, I'm looking to write from JSON to a MySql database.
The JSON looks like this
{
"offset": 0,
"results": [
{
"aq1": 22,
"aq3": 27,
"aq2": 27,
"ateam/_source": "/nba/teams/page/NO/new-orleans-pelicans",
"aq4": 24,
"hqf/_source": "108",
"hq2": 27,
"hq1": 22,
"hq4": 36,
"hq3": 23,
"aqf/_source": "100",
"ateam/_text": "New Orleans",
"aq3/_source": "27",
"hq4/_source": "36",
"hteam": "http://www.cbssports.com/nba/teams/page/BOS/boston-celtics",
"aq2/_source": "27",
"hteam/_text": "Boston",
"aq1/_source": "22",
"hteam/_source": "/nba/teams/page/BOS/boston-celtics",
"ateam": "http://www.cbssports.com/nba/teams/page/NO/new-orleans-pelicans",
"aqf": 100,
"hq1/_source": "22",
"hq3/_source": "23",
"hq2/_source": "27",
"hqf": 108,
"aq4/_source": "24"
},
],
"cookies": [],
"connectorVersionGuid": "dgsadjgofsjkgkfsmghjfhf",
"connectorGuid": "fbcjsdbgfjdjgkf",
"pageUrl": "http://xsftbfbjjjsfaf.com/"
}
and my php reads like this
<?php
$server = "localhost";
$username = "lies";
$password = "madeup";
$database = "dgjsfdgjv";
mysql_connect($server,$username,$password) or die("Failed");
mysql_select_db($database) or die("Database Failed");
$url = "https://api.import.io/store/data/jjjjj-ec0f-4553-bda5-def61ca1756c/_query?input/webpage/url=http%3A%2F%2Fwww.jjjjj.com%2Fnba%2Fscoreboard%2F20150111&_user=f1508a78-05c5-4487-9219-51a990391329&_apikey=aLZu2wmRCvUlBwopi%2F6Kj%2F4wnscvZRh7DYQf80LE2e3hL22rtAp0nJ3lujy10cyx9JC9Ed73xb3zGp3aArhYDQ%3D%3D";
$string = file_get_contents($url);
$arr = json_decode($string, true);
foreach($arr as $item){
$aq1 = $item['aq1'];
$aq3 = $item['aq3'];
$aq2 = $item['aq2'];
$ateam/_source = $item['aqeam/_source'];
$aq4 =$item['aq4'];
$hqf/_source =$item['aqf/_source'];
$hq2 =$item['aq2'];
$hq1 =$item['aq1'];
$hq4 =$item['aq4'];
$hq3 =$item['aq3'];
$aqf/_source =$item['aqf/_source'];
$ateam/_text =$item['aqeam/_text'];
$aq3/_source =$item['aq3/_source'];
$hq4/_source =$item['aq4/_source'];
$hteam =$item['aqeam'];
$aq2/_source =$item['aq2/_source'];
$hteam/_text =$item['aqeam/_text'];
$aq1/_source =$item['aq1/_source'];
$hteam/_source =$item['aqeam/_source'];
$ateam =$item['aqeam'];
$aqf =$item['aqf'];
$hq1/_source =$item['aq1/_source'];
$hq3/_source =$item['aq3/_source'];
$hq2/_source =$item['aq2/_source'];
$hqf =$item['aqf'];
$aq4/_source =$item['aq4/_source'];
mysql_query("INSERT INTO createdb (aq1,aq3,aq2,ateam/_source,aq4,hqf/_source,hq2,hq1,hq4,hq3,aqf/_source,ateam/_text,aq3/_source,hq4/_source,hteam,aq2/_source, hteam/_text,aq1/_source,hteam/_source,ateam,aqf,hq1/_source,hq3/_source,hq2/_source,hqf,aq4/_source) VALUES('$aq1','$aq3','$aq2','$ateam/_source','$aq4','$hqf/_source','$hq2','$hq1','$hq4','$hq3','$aqf/_source','$ateam/_text', '$aq3/_source','$hq4/_source','$hteam','$aq2/_source','$hteam/_text','$aq1/_source','$hteam/_source','$ateam','$aqf','$hq1/_source','$hq3/_source','$hq2/_source','$hqf','$aq4/_source')") or die ("Failed");
}
I dont trigger any of the error messages to appear but no data ends up in the database. What am I doing wrong? Is it the php code reading the json string correctly???
By carefully, looking at your problem , I have realized that , the problem was inside foreach() Loop. It should be
foreach($arr as $item){
echo $aq1 = $item[0]['aq1']; # note that [0] here
echo $aq3 = $item[0]['aq3'];
#...........Rest of Loop
}
And also rename variables like this $aqf/_source to $aqf_source , and so on .
I got it working.
foreach ($phpArray['results'] as $key => $value) {
// This is to make the data to be saved into the database
foreach ($value as $k => $v) {
$aq1 = $value['aq1'];
$aq3 = $value['aq3'];
$aq2 = $value['aq2'];
$ateam_source = $value['ateam/_source'];
$aq4 =$value['aq4'];
$hqf_source =$value['hqf/_source'];
$hq2 =$value['aq2'];
$hq1 =$value['aq1'];
$hq4 =$value['aq4'];
$hq3 =$value['aq3'];
$aqf_source =$value['aqf/_source'];
$ateam_text =$value['ateam/_text'];
$aq3_source =$value['aq3/_source'];
$hq4_source =$value['hq4/_source'];
$hteam =$value['hteam'];
$aq2_source =$value['aq2/_source'];
$hteam_text =$value['hteam/_text'];
$aq1_source =$value['aq1/_source'];
$hteam_source =$value['hteam/_source'];
$ateam =$value['ateam'];
$aqf =$value['aqf'];
$hq1_source =$value['hq1/_source'];
$hq3_source =$value['hq3/_source'];
$hq2_source =$value['hq2/_source'];
$hqf =$value['hqf'];
$aq4_source =$value['hq4/_source'];
}

PHP DateTime: 'previous week' or 'last week'?

I want to get the date of the first day of the last week:
$date = new DateTime(NULL, new DateTimeZone('Pacific/Wake'));
$date = $date->modify('previous week');
$date = $date->format('Y-m-d');
and
$date = new DateTime(NULL, new DateTimeZone('Pacific/Wake'));
$date = $date->modify('last week');
$date = $date->format('Y-m-d');
Both work.
But is there any difference?
They are the same, you can confirm this for yourself if you want, the PHP source code is available on github.
https://github.com/php/php-src/blob/master/ext/date/php_date.c#L1443
PHP_FUNCTION(strtotime)
{
...
t = timelib_strtotime(initial_ts, strlen(initial_ts), NULL, DATE_TIMEZONEDB, php_date_parse_tzfile_wrapper); /* we ignore the error here, as this should never fail */
So it calls timelib_strtotime, where can this be found? Well luckily that's online too
https://github.com/php/php-src/blob/master/ext/date/lib/parse_date.c#L24743
timelib_time* timelib_strtotime(char *s, int len, struct timelib_error_container **errors, const timelib_tzdb *tzdb, timelib_tz_get_wrapper tz_get_wrapper)
{
...
do {
t = scan(&in, tz_get_wrapper);
#ifdef DEBUG_PARSER
printf("%d\n", t);
#endif
} while(t != EOI);
...
Which relies on scan:
https://github.com/php/php-src/blob/master/ext/date/lib/parse_date.c#L835
static int scan(Scanner *s, timelib_tz_get_wrapper tz_get_wrapper)
{
...
while(*ptr) {
i = timelib_get_relative_text((char **) &ptr, &behavior);
Which makes a call to timelib_get_relative_text
https://github.com/php/php-src/blob/master/ext/date/lib/parse_date.c#L561
static timelib_sll timelib_get_relative_text(char **ptr, int *behavior)
{
while (**ptr == ' ' || **ptr == '\t' || **ptr == '-' || **ptr == '/') {
++*ptr;
}
return timelib_lookup_relative_text(ptr, behavior);
}
Which makes a call to timelib_lookup_relative_text:
https://github.com/php/php-src/blob/master/ext/date/lib/parse_date.c#L536
static timelib_sll timelib_lookup_relative_text(char **ptr, int *behavior)
{
...
for (tp = timelib_reltext_lookup; tp->name; tp++) {
if (strcasecmp(word, tp->name) == 0) {
value = tp->value;
*behavior = tp->type;
}
}
...
}
Which in turn uses a structure set called timelib_reltext_lookup defined at the top of the file:
https://github.com/php/php-src/blob/master/ext/date/lib/parse_date.c#L248
static timelib_lookup_table const timelib_reltext_lookup[] = {
{ "first", 0, 1 },
{ "next", 0, 1 },
{ "second", 0, 2 },
{ "third", 0, 3 },
{ "fourth", 0, 4 },
{ "fifth", 0, 5 },
{ "sixth", 0, 6 },
{ "seventh", 0, 7 },
{ "eight", 0, 8 },
{ "eighth", 0, 8 },
{ "ninth", 0, 9 },
{ "tenth", 0, 10 },
{ "eleventh", 0, 11 },
{ "twelfth", 0, 12 },
{ "last", 0, -1 },
{ "previous", 0, -1 },
{ "this", 1, 0 },
{ NULL, 1, 0 }
};
Hopefully this is sufficient proof that they are, in every way, the same.
The word "previous" is supported since PHP5, "last" since PHP4. The result of both is the same and both are still valid.

Recursive function in async tree in php gets with "Fatal error: Allowed memory size of 134217728"

i have tried to do an async tree but when i call a function inside the function she get me an memory error. I have read many things how to change the limit of the php memory to run that and i tried that and it doesn't result. But what i realy want is a way to "stop" the function, to not call that infinitely times. Can anyone help me?
function treeview()
{
$i = 0;
$result = array();
$rs = mysql_query("select * from dados3 where nivel = 1");
while($row = mysql_fetch_array($rs))
{
$node = array();
$node['id'] = $row['id'];
$node['text'] = $row['descricao'];
$node['state'] = 'closed';
$node['children'] = has_child($row['id']);
array_push($result, $node);
}
echo json_encode($result);
}
function has_child($id){
$result2 = array();
$node_2 = array();
$rs = mysql_query("select * from dados3 where id_nivel_superior=$id");
$row = mysql_fetch_array($rs);
$node_2['text'] = $row['descricao'];
$node_2['state'] = 'closed';
$node_2['children'] = has_child($id);
array_push($result2, $node_2);
return $result2;
}
I tried the above way and that returns me all the children null and at level 3 repeats me the level 1. What you think it can be wrong?
function treeview()
{
$i = 0;
$result = array();
$rs = mysql_query("select * from dados3 where nivel = 1");
while($row = mysql_fetch_array($rs))
{
$node = array();
$node['id'] = $row['id'];
$node['text'] = $row['descricao'];
$node['state'] = 'closed';
$node['children'] = has_child($row['id']);
array_push($result, $node);
}
echo json_encode($result);
}
function has_child($id){
$result2 = array();
$node_2 = array();
$rs = mysql_query("select * from dados3 where id_nivel_superior=$id");
while($row = mysql_fetch_array($rs))
{
$node_2['text'] = $row['descricao'];
$node_2['state'] = 'closed';
$node_2['children'] = has_child($row['id']);
array_push($result2, $node_2);
return $result2;
}
}
Here at the image you can see the database structure anda data:
image of database
I have already put the return out of the loop but the json that returns me it's with the cildren all null. And at level 4 (where i want to show something) it continuos repeatedly all the levels again.
[{"id":"1","text":"1 - Cat.","state":"closed","children":[{"text":"1.1 - Sub.","state":"closed","children":[]},{"text":"1.2 - Sub.","state":"closed","children":[]},{"text":"1.3 - Sub.","state":"closed","children":[]}]},{"id":"2","text":"2 - Cat.","state":"closed","children":[{"text":"2.1 - Sub.","state":"closed","children":[{"text":"2.1.1 - Sub_sub","state":"closed","children":[{"text":"Faq 1 - categoria 1","state":"closed","children":[]}]}]},{"text":"2.2 - Sub.","state":"closed","children":[]},{"text":"2.3 - Sub.","state":"closed","children":[]}]},{"id":"4","text":"3- Cat.","state":"closed","children":[{"text":"3.1 - Sub.","state":"closed","children":[]},{"text":"3.2 - Sub.","state":"closed","children":[]},{"text":"3.3 - Sub.","state":"closed","children":[]}]},{"id":"5","text":"4 - Cat.","state":"closed","children":[{"text":"4.1 - Sub.","state":"closed","children":[]},{"text":"4.2 - Sub.","state":"closed","children":[]},{"text":"4.3 - Sub.","state":"closed","children":[]}]}]
If you can help me understand why the children doesn't appears i will appreciate.
Best regards.
It looks like the problem is your recursive call to has_child() is passing in $id, i.e. the parameter originally passed to the function. So, what's happening is because the function is being called with the same parameter every time, it will recursively call itself endlessly.
I suspect the line
$node_2['children'] = has_child($id);
should actually read
$node_2['children'] = has_child($row['id']);
Also, I suspect you probably want a loop in has_child() as well, in the same way as in treeview() otherwise you'll only get the first item at each level of the tree.
EDIT
Actually, sorry, I may have misunderstood what's going on here. Earlier I assumed that "id_nivel_superior" was the parent item's ID, but am I right in saying that actually that's the parent's level within the tree? If so, is there something that does contain the parent item's ID, as that's the column your SQL statement in has_child() needs to use. It might help if you could post your table structure and a sample of your data.
Also, your return statement in has_child() needs to be outside the loop.
OK, to answer your latest edit, I might be missing something here, but it looks like the JSON output you've posted is correct according to the data you have. Its a bit tricky to read on one line, but if you run it through the formatter at http://jsonformatter.curiousconcept.com/ you'll see this is what it looks like:
[
{
"id":"1",
"text":"1 - Cat.",
"state":"closed",
"children":[
{
"text":"1.1 - Sub.",
"state":"closed",
"children":[
]
},
{
"text":"1.2 - Sub.",
"state":"closed",
"children":[
]
},
{
"text":"1.3 - Sub.",
"state":"closed",
"children":[
]
}
]
},
{
"id":"2",
"text":"2 - Cat.",
"state":"closed",
"children":[
{
"text":"2.1 - Sub.",
"state":"closed",
"children":[
{
"text":"2.1.1 - Sub_sub",
"state":"closed",
"children":[
{
"text":"Faq 1 - categoria 1",
"state":"closed",
"children":[
]
}
]
}
]
},
{
"text":"2.2 - Sub.",
"state":"closed",
"children":[
]
},
{
"text":"2.3 - Sub.",
"state":"closed",
"children":[
]
}
]
},
{
"id":"4",
"text":"3- Cat.",
"state":"closed",
"children":[
{
"text":"3.1 - Sub.",
"state":"closed",
"children":[
]
},
{
"text":"3.2 - Sub.",
"state":"closed",
"children":[
]
},
{
"text":"3.3 - Sub.",
"state":"closed",
"children":[
]
}
]
},
{
"id":"5",
"text":"4 - Cat.",
"state":"closed",
"children":[
{
"text":"4.1 - Sub.",
"state":"closed",
"children":[
]
},
{
"text":"4.2 - Sub.",
"state":"closed",
"children":[
]
},
{
"text":"4.3 - Sub.",
"state":"closed",
"children":[
]
}
]
}
]
which as far as I can tell matches the database. If there's something not right about this then can you please let me know what output you're expecting to get?

MongoDB php Spatial Astronomy Data What's wrong with this?

I want to retrieve data within the bounding box ((ra_min,dec_min),(ra_max,dec_max)).
Could anyone tell me what's wrong with below?
php Code and structure of data are below. Thanks......
Structure :
{
"filter": "E",
"loc": {
"dec": 130,
"ra": 15
},
"path": "00015+00130E.jpeg"
},
{
"filter": "Z",
"loc": {
"dec": 130,
"ra": 15
},
"path": "00015+00130Z.jpeg"
},
Code :
$lowerLeft = array("ra"=>$RA_min - $ra_offset, "dec"=>$DEC_min - $dec_offset);
$upperRight= array("ra"=>$RA_max + $ra_offset, "dec"=>$DEC_max + $dec_offset);
$cond = array("loc" => array('$within' => array('$box' => $lowerLeft, $upperRight)));
$cursor = $collection->find($cond);
i Think problem is on $box Usage
$box Usage Example:
$cursor = $collection->find( array( 'loc'=>array('$within'=> array('$box'=>array(array(0,0), array(100,100) ) ) ) ));
This will return all the documents that are within the box having points at: [0,0], [0,100], [100,0], and [100,100].
use print_r($cond); before find(); for see the array value that show you where is problem

Categories