Tracking Forums, Newsgroups, Maling Lists
Home Scripts Tutorials Tracker Forums
 
  HOME    TRACKER    PHP




Binary Calculations


I'm not sure if PHP can do this but I have a client that would like to provide unlock codes for a CD they have. They have given me a document that describes the Binary calculation that is required to calculate the unlock code based on the CD serial number. They also refer to EX/OR or exclusive or. The also refer to hex data which I'm assuming is hexidecimal which I'm pretty sure PHP can deal with.

I'm interested in finding out if PHP can work with binary, ex/or and hexidecimal data. If not any other possible solution, ie. library or other language such as Perl. I would also appreciate any links to additional info.




View Complete Forum Thread with Replies

See Related Forum Messages: Follow the Links Below to View Complete Thread
Calculations
i want it to calculate a value that is in the db if the db has the value 6 pound(s) (i have a submit button) and the users enters in the input box a value for more pounds then how do i make it calculate in fractions or even divisions.. cause see this is what i have. PHP Code:

Calculations
<?php
echo 04 - 1;
?>

that echo's
3
I need it to echo 03, is there an easy way to do this?

Auto Calculations
I've seen on a few sites that in a shopping enviroment if you choose a price from a drop down and then maybe a delivery ares, the actual price shown for the product is updated instantly without page refresh.

Date Calculations
in my script i have this varaibles PHP Code:

Doing Time Calculations...
For a project I am working on right now I need to be able to calculate the
time difference between to given times.
(For example...The difference between 11:30 am and 1:45pm being 2:15)

Right now the input is setup as 2 text boxes, 2 dropdowns, and 2 radio
groups. The text boxes get the hour entered into them, the drop downs holdd
a list contaning 00, 15, 30, and 45 (the minutes) and the radio groups being
am or pm.

Is it possible to use that configuration and somehow get the time
difference? or do I need to change the input? The goal is to be able to
enter the two values and immediately see a calculated difference in a third
textbox on the page.

I'm thinking something like....
User enters hour in first text box(textHFrom) - 11
User selects minue value from second dropdown(cboMFrom) - 30
User selects am/pm from radio group(radioF) - am
User enters hour in second text box(textHTo) - 1
User selects minue value from second dropdown(cboMTo) - 45
User selects am/pm from radio group(radioT) - pm

onChange of second RadioGroup time is calculated and result in inserted into
third text box(textHours) - 2:15

I'm not sure if/how to bet do this.

Time Calculations
i have a field in mysql which contains a timestamp: 2007-07-25 22:52:14 i was going to add a new variable but adding 1 hour to it so it would read: 2007-07-25 23:52:14
im not sure how to format the date to read add 1 hour to the date stamp.

Calculations With Date
i've got 2 date values, both in the format yyyy-mm-dd, has anyone any ideas how to perform a calculation, so that I can take one date away from the other and be left with the days/difference between

Calculations Within A Field
i have a field which is float, it stored the price of an item, i need to create another field called points which times the price by 0.1 and stores that value.

how would i do this? can the sql database do it itself or would i need to create a function in php that will convert it then store the points?

Date Calculations
I'm doin the old calculate persons age from database routine. Had a good search around and found out about Unix timestamps. Been looking those up and trying to figure out how to use them properly.

My dates are of the d-m-Y format.. I.E.. today is 11-02-2007
I did it this way so i can use date("d-m-Y") to grab todays date elsewhere in the site and it's all the same format.

Problem is that I can't quite grasp timestamps.
I tired something like this..

$dobts = strtotime($dateofbirth);
$todaysdate = date("d-m-Y");
$todaysdatets = strtotime($todaysdate);

$agets = $todaysdatets - $dobts;

$age = date("d-m-Y", $agets);

Didn't work of course but it thought that was the point of these timestamps?

Date Calculations?
I'm having trouble with a simple date calculation. Basically I have to time stamps, $todays_date and $date_joined. I want to find out how long a member has been registered on my forum in days months years atm I'm taking  $date_joined off $todays_date but its giving me an actual date which is 1971-06-05 but I actually want something like 30 days or 2 months 3 days not the actual date if that makes any sense.

Heres my code:
$todays_date=time();
$date_joined=$member_row['user_joined'];
$join_time=$todays_date-$date_joined;
echo "Join time: ". date('Y-m-d', $join_time)."
";

How To Do Calculations Using JOIN Statement?
I have three tables -:

Customer Table
AccountID field
Name field

Invoice Table
InvoiceID field
invoice_date field
CustomerID field
Amount field

Payment
InvoiceID field
Payment_Amount field
payment_date field

PHP Code:

PHP And MySQL Date Calculations
I have a date type in mySQL, and I want PHP to run a select where it grabs todays date, checks against my database, and returns all values that are new within 30 days. I cant seem to find any help anywhere. Here's what I got so far for it:

$current = getdate();
$this_month = $current["mon"];

that will at least get me the month, the problem is testing it against the date type in the select statement.

Date Calculations Advice
I am getting the date like this $date = strtotime(date());
ok, there you have it, a Unix time stamp of the time, date, and everything else.
From here, is this the best way to do calculations.

Like if I wanted to tell when 48 hours had passed from this date, how do I go about doing that. I know for example if you wanted to add a day, you check on how many "number's" here are in a day and put + that number onto the strength.
Can someone tell me some stuff about this, so I am more familiar with dates, before I start all of these calculations.

Simple Date Calculations
What I am trying to do is show the difference between two dates - one will be "today" and the other will be the date on which an entry was made into the database for that particular user.

I tried doing $difference = $today - $entrydate but this did not give me any kind of figure. Both dates are stored in the format of, for example, 21 Feb 07, 11:22am Would the time cause problems?

CGI Php Binary
I appologize in advance to purists who think the question is an
appache one, or a mysql question.

I have a virtual domain hosted by domainavenue.com, owned by wyith.net
in Hong Kong.

I want to use a web content management system
(http://www.phpwebsite.appstate.edu/) which requires php 4.2.2 or
higher. My provider offers perl561, php412, apache (I don't know
which), MySQL3.23.22-beta.
I believe my provider uses a standard linux redhat 7.x distribution,
but their /etc/redhat-release file is not user readable, and I don't
have access to gcc, make etc.

What is the best solution?

Below is an outline of my (unsuccessful) approach:

I compiled php4.3.3 at home (with mysql and static and
cgi.force_redirect) options. Using a '.htaccess' file, I redirect php
requests from the appache module, to my personal executable. Now,
phpwebsite is no longer complaining about insufficient php version,
but the database connectivity is broken, whether initiated by
phpwebsite, or phpMyAdmin. An phpwebsite8.3 error message is included
below:

"Warning: mysql_connect(): Can't connect to local MySQL server through
socket '/tmp/mysql.sock' (2) in /var/www/b1243/php83/dbconnect.php on
line 11
Unable to select database"

The phpMyAdmin message is included below:

Warning: Cannot modify header information - headers already sent by
(output started at /var/www/b1243/phpMyAdmin/libraries/auth/config.auth.lib.php:92)
in /var/www/b1243/phpMyAdmin/header.inc.php on line 23

Error

MySQL said:

Can't connect to local MySQL server through socket '/tmp/mysql.sock'
(2)

The 9.x version of php website simply says:

Unable to connect to your server's database.
Please reenter your settings.

// End error messages, begin questions:

What am I doing wrong? I know I probably don't have the exact same
c-libraries as the web host site, is that a requirement?

Is my executable (3.6MB) truly static, such that there is no
dependence on host libraries?

Do I need to add some mysql path options?

PHP Binary
Anyone know where the php binary should reside by default on a linux system? I've been told you should be able to run php from the command prompt, but I've failed so far.

Binary Calculation
I need some way of determining which bits are set in a binary number.

eg.

Decimal Number: 40
Binary Number: 00101000

How can I determine that the binary equivalent of 8 and 32 are set??

Binary Encryption
Im using md5 to encrypt and decrypt plain text, this works fine...

When i try to run the same function on a binary file, it does not
decrypt correctly. Is there a way to encrypt binary files correctly,
and be able to decrypt them?

in the perfect situation i would like to be able to store the
encrypted binary file into a mysql blob field, or at least be able to
save an encrypted version of the binary file on the server.

Im not lookign for a copy paste soultion (although i would take one) -
but maybe some reliable documention or examples?

What Is Binary Version Of Php?
i have read word binary version of php but i don't know what is it???

can you please tell me some about it?

PHP + Binary Files
I am persisting nested (tree) structures in C to binary file. I want to
be able to read/write this data using PHP - is this possible?. That is
to say, can PHP read/write C structures to file so that the contents may
be transferred to a C client app?

The idea is that I store the binary files at the server end and clients
can request for the file(s). Is this possible ?

PS- the client is (of course) a C++ client, capable of understanding the
returned structure. Another point worth mentioning is that the server
will be on Unix and the client on Windows. Can I force PHP to write the
binary file using Network byte order so that endianess is not an issue?

Binary Software With PHP
I was wondering if it were possible to create a binary program in PHP so that no one could see your code?

A company (which shall remain nameless) is selling a software prgram written in php that they sell in two packages, package one is about $200 and package two is $700. The only difference (so they say) is the second package comes with the source code and the first doesn't.... wierd eh?

I always thought that php could not be made into a binary, if so then this company is either not clear on their licenseing program or misleading customers into thinking that if they want to make changes they will have to pay $600.

Binary Strrpos?
I am having trouble getting the strrpos from a file on a linux machine.

someone says it has something to do with binary?
The code works fine on windows, but when i run it on a linux server it
gives me a different result

heres an examples:
<?
$haystack= file_get_contents("mysite.com");

$needle = "blabla";

$x = strrpos($haystack, $needle);

echo $x

//$x returns 201 on windows
//$x returns 300 on linux
?>

Binary File
I am wanting to write a php script that will output the binary value of a binary file. When I print the current chunk, I just get the following printed to the screen: "Resource id #3". And when I load the whole file contents into a string variable and then print that string, I just get jibberish printed to the screen. Could somebody please tell me how I could print the binary value of the file? Code:

Changing From ASCII To Binary
How do I change my upload from ASCII to Binary? it was a suggestion to get excel files to upload, but I don't know how to make the change.

Uploading Binary Files With Php
I have some trouble uploading a file? i followed the example on www.php.net on uploading and it works fine when it comes to text files or atleast files that are ascii, pure text files that is.
But when it comes to pdf files (these are the ones i want to upload) or exe files or any binary files at all i want to upload i get problem. i have put some debugs printing out info about the upload when uploading a textfile i get this:

39
text/plain
fluff.txt
/var/www/upload/phpnO63Fa

first is filesize, next is filetype, next is the name of the file and then comes the tempfile name
but when uploading a pdf (or other binary file) this is what i get:

0
application/pdf
calls2.pdf
none

Why is this? is there somewhere i specify that it should be a binary transfer and not an ascii?
here's my code:

upload.html:
<form enctype="multipart/form-data" action="uploaded.php" method="post">
<input type="hidden" name="MAX_FILE_SIZE" value="10000">
Send this file: <input name="userfile" type="file">
<p>
<input type="submit" value="Send the f00king File">
</p>
</form>

doupload.php:
<?
set_time_limit(0);

move_uploaded_file($_FILES['userfile']['tmp_name'], "/var/www/upload/" . $_FILES['userfile']['name']);

echo ($_FILES['userfile']['size']);
echo ("<br>");
echo ($_FILES['userfile']['type']);
echo ("<br>");
echo ($_FILES['userfile']['name']);
echo ("<br>");
echo ($_FILES['userfile']['tmp_name']);
?>

can someone please help me?
With regards.

Install Binary Or Module On IIS?
I want to install PHP on a windows NT 4 box running IIS4 web server. What PHP intallation type is best for this? Binary, or with the ISAPI module? Pro's, Con's?

Php, MySQL, And Binary Data
I'm trying to upload binary data and store it in mySQL as a mediumblob. Since there's always the possibility of quotes (along with any other character) within the binary stream, I assume I need to do something along the lines of AddSlashes to ensure that the sql request won't fail. The trouble is I'm finding that StripSlashes is blowing away the string when I retrieve it. I've tried rolling my own encoding, but ereg_replace("'", "'", $string) is truncating my data, apparantly at the first null character it hits.

So: what's a solution for safely getting binary data into mysql via php?

Concat 2 Binary Files?
I want to manipulate binary files.

this was just a test to concatinate 2 text files but it said
file_put_contents was undefined. the second parameter is suppoed to be
'mixed data', no idea what that is.

<?php
$t1 = file_get_contents('test1.txt');
$t2 = file_get_contents('test2.txt');
$result = file_put_contents('test3.txt', $t1 . $t2);
?>

But this was just a test anyway since file_get_contents uses strings,
can someone just give me the code to concat 2 binary files? I'll be
manipulating the files before I concatinate them, so putting the data
into 2 arrays of bytes 1st would be best.=

Convert Byte To Binary
I'm receiving 2 bytes of data from a socket connection.
They are shown as 2 unknown characters.

What I need to do is view their binary representation.

I've been playing around with unpack, but can't seem to get anywhere. Google hasn't helped a bit.

Like, if I just had "e" how could I convert that to "01100101"?

Upload Binary Error
not sure if this is a bug in PHP....

In first looking at this error, it looks like MYSQL error....

I get this error: MySQL server version for the right syntax to use near
'Aed`#0 .``d' at line 1,

I am using a multipart/form-data enc type and php seems to be messing up the
memory address space for my variables....

Sending Binary Data Via UDP
I'm trying to send a packet of binary data to a UDP server. If I send
a text string, it works fine. If I attempt to send binary data, it
sends a UDP packet with 0 bytes of data (just the headers). I can see
this because I'm running Ethereal and watching the packets.

I'm defining the packets as shown below:

$text_msg = "Hello, world
";
$binary_msg = chr(0x01).chr(0x02).chr(0x03).chr(0x00).chr(0xA0);
$binary_msg_size = 5;

Initially, I tried using
$fp = fsockopen("udp://" . $server,$port,$errno,$errstr)
and
fwrite($fp,$binary_msg,$binary_msg_size)

Since this didn't work, I then tried
$sock = socket_create(AF_INET,SOCK_DGRAM,SOL_UDP);
and
socket_sendto($sock,$binary_msg,$binary_msg_size,0 ,$server,$port);

For both methods, sending $text_msg worked, but sending $binary_msg
resulted in a UDP packet with 0 data bytes being sent.

I've found examples of sending binary data, but they used TCP, and
I've found examples of sending UDP, but they send text strings.

Anyone have experience with sending binary data to a UDP server?

Reading Binary Files
I made a little PHP thingy where you can search for any string of
numbers in the first 8 million digits of pi.

However, they're currently stored in ASCII, which means it's 2bytes for
a digit, and it's horribly inefficient. I used perl to pack it as 2
digits a byte, basically storing it as hex without A-F.

My problem is, I need a fast way to search the file by the half byte.
Asides from reading all 8 million digits and converting it to hex, how
else can I do it?

Configuring Binary PHP Installation
I have RedHat Linux 8.0 installed on my AMD system. I chose to install all of the web server apps when I installed RH. Apache works and so does PHP, but when I use a php function to call a mySQL functionality I get:

"Fatal error: Call to undefined function: mysql_connect in /../../ on line 2"

Is there a way to get PHP to recognize and call a mySQL function without reinstalling PHP and mySQL from source. NOTE: the mySQL package I am using is a binary and has no source folder, Apache and PHP are also binaries. Any suggections?

How To Pass Variables To PHP Binary (CGI)?
My system use to run PHP as an Apache module. And I pass in variables like this:
http://www.somesite.com/somefile.php?IDvar=1

Now my system runs PHP as a CGI. So I am running the same script like this:
/usr/local/bin/php /somepath/somefile.php

How do I pass in the variable IDvar and value 1?

Running Php4 As Cgi Binary?
i'm experiencing problems in calling php4 functions (session management functions...) on my web host... they have compiled php3 into apache, and they provide php4 support through cgi-bin.

i thought all i had to do to use php4 was just "use it". but apparently it dint' work. it says "calling unsupported functions session_start() ....". my web host said php4.0.3pl1 is installed in /usr/bin/php4

Handling Binary Strings
I am trying to handle binary strings in php. I get a binary output
initialization vector from mcrypt as such:

from mcrypt:
$iv = mcrypt_create_iv($iv_size, MCRYPT_RAND);

This output may have padded nulls at the end which are significant.

I would like to convert all characters including all padding nulls at
end and convert it in a string to be saved into a database in such a way
that I can reconstruct it back to binary again later.

base64_encode and decode is not doing the job. They don't seem to
preserve the original binary value after base64_decode().

Binary-safe Fget?
I understand that fget is not binary-safe before php version 4.3.3.
To me that means fget cannot read binary data (or clips it to 7 bits).
Is there another php function equiv. to fget that is binary safe. I
am not quite ready to upgrade to 4.3.3.

Different Binary Results With Different PHP Versions
I am using Windows XP + PHP 5.1.2 at home for development and using
Linux + PHP 4.4.2 at server side. In my code, I am doing some bitwise
operations. While doing this, I have come accross to different results
from my functions that uses bitwise xor (^) operator. When I digg into
the code, I have tried to print out the binary of a decimal number. The
result was so:

echo decbin(-5817441119);
// Prints 10100101010000001110010010100001 at local (PHP 5.1.2)
// Prints 10000000000000000000000000000000 at server (PHP 4.4.2)

Going Crazy About Binary Post
I'm tearing my hair out over this one.. I'm using EasyPHP, and trying to
write a script which receives binary POST data from a client program of
mine. I've put together a simplified example which demonstrates the
problem. The data looks like this:

(String showing the length of the following binary)(Character#10)(Binary data)

My PHP script looks like this:

<?
include('databasedef.php');
set_magic_quotes_runtime(0);
$chandle = mysql_pconnect($servername, $username, $password) or die("FAIL");
mysql_select_db($database, $chandle) or die ("FAIL");
$handle=fopen("php://input","rb") or die("FAIL");
$c=fgets($handle,20);
$picture=mysql_real_escape_string(fread($handle,$c ));
$query="INSERT INTO ".$table." (Picture) VALUES ('".$picture."')";
mysql_db_query($database, $query) or die("Failed Query");
fclose($handle);
mysql_close($chandle);
?>

Although I get the right number of bytes in my MySQL database, they're
corrupted! What setting am I missing here? The files being stored are
less than 40k in a MEDIUMBLOB field.

Deploying Binary PHP Extension
I have written a PHP extension that I want to be able to sell (as part
of a solution) and deploy in binary form. All examples and tutorials I
have found describes the process of deploying open-source extensions.
I have tried copying the library into the extensions directory defined
in php.ini and added an extension entry for it there as well. This...

String Representation Of Hex To Binary?
I'm trying to verify some data which is posted to a php script from an external server. One of the posted fields is a signed hex number, but its obviously a string as part of the $_POST vars. I need to convert this value to a binary number so that it can be encrypted and then verified. Code:

Binary To ASCII Conversion??
Is there a way to convert a Binary file to ASCII or txt file using a php script?

Convert Binary To String ?
what's the correct function for that cause i can't seem to find it no where !

Displaying A Binary Image
I would like to know if anyone knows how I could go about sending an image to the browser that is binary. Here is what I am doing right now...but it's not working:

header("Content-Type: image/jpeg");
header('Content-Disposition: inline; filename="image.jpg"');
$theImage = $thePage->GetAs("image.jpg", 350, 270, 60);
imagejpeg($theImage);

I have an ASP script that I am going off of to pull this together, and it uses "Response.BinaryWrite", is there an equivalent in PHP?

Receiving Binary Data Using Sockets
I'm using sockets to communicate with an external device. This device sends out 5 bytes that I wish to receive and analyze. The problem I have is that the response I get seem to be converted into unicode(?)

Sending Binary Data With PHP Sockets
Is there any mean to send binary data with PHP using sockets? What I find so far is only sending and receiving Strings. I would be grateful if you could help me. Thanks.

Passing Arguments To PHP Compiled As A CGI Binary?
I searched for the answer to this, but found nothing.

Lets say I have a file called test.php, and in it:
PHP Code:
#!/usr/bin/php

Emailing Binary Data Question
I'm trying to create an 'email this picture' form using a php email class ("phpmailer"), & wish to embed the image in the body of the email, as opposed to attaching the image file.

The images are binary files referenced by fields in a mysql table, and are displayed using a function call that produces code without a .jpg extension - e.g. "<img src=img.php?f(3e7b67c7dd510)+w(45%)+h(45%)>" (I've used this script).

For the life of me I can't work out how to embed such an image into an email, & am thinking that maybe a workaround would be to rename the image with a simple .jpg title, save it to a temp directory, & delete after sending each email. What I'd like to know is:is this the best way to go forward?if so, how can it be done?

Treating POST_VARS Array But Not Binary?
Would like to come up with a portable function that cleans user input for my database but don't want it to mess with binary data. anyone have ideas on this?

I think i need a function to dectect if the $val is binary or a way to rewrite the $vals i pass to the function. perhaps PHP Code:


Copyright © 2005-08 www.BigResource.com, All rights reserved