Page 1 of 1

Converting some C to PHP

Posted: Sun Apr 23, 2006 12:15 pm
by madru
feyd | Please use

Code: Select all

,

Code: Select all

and [syntax="..."] tags where appropriate when posting code. Your post has been edited to reflect how we'd like it posted. Please read:  [url=http://forums.devnetwork.net/viewtopic.php?t=21171]Posting Code in the Forums[/url] to learn how to do it too.[/color]


Hi all,

can somebody give me a helping hand here, please?

I have the following C code and I am not able to convert this to PHP 

 [syntax="c"]
#include <stdio.h>

  int main()
    {
	const int DC1 = 0x11;
	const int ESC = 0x1b;
       int BCC;
	
  //BCC = (DC1,0x07+ESC+'G'+'R'+0+0+100+100);//%256;
         BCC = (DC1+0x07+ESC+'G'+'R'+0+0+100+100);
	  printf("%d\n", BCC);
  //	  printf("%d\n", BCC%256);
  //  return -1;
    }
the result from this math will be 404

THX

Madru
[url=http://forums.devnetwork.net/viewtopic.php?t=30037]Forum Rules[/url] Section 1.1 wrote:2. Use descriptive subjects when you start a new thread. Vague titles such as "Help!", "Why?" are misleading and keep you from receiving an answer to your question.
[url=http://forums.devnetwork.net/viewtopic.php?t=30037]Forum Rules[/url] Section 1.1 wrote:1. Select the correct board for your query. Take some time to read the guidelines in the sticky topic.

feyd | Please use[/syntax]

Code: Select all

,

Code: Select all

and [syntax="..."] tags where appropriate when posting code. Your post has been edited to reflect how we'd like it posted. Please read:  [url=http://forums.devnetwork.net/viewtopic.php?t=21171]Posting Code in the Forums[/url] to learn how to do it too.[/color]

Posted: Sun Apr 23, 2006 12:54 pm
by madru
Hi,

that I don't know how to convert the 'G' and 'R" into a number and how to make the math x+y+.... I always get a wrong result :-(

Posted: Sun Apr 23, 2006 1:00 pm
by timvw
You got me there (don't know if it's possible to cast string to an integer)..

In case there is no such native function, the following could do it too:

Code: Select all

function getCharVal($char) {
  $values = array();
  $values['A'] = 65;
  $values['B'] = 66;

  return $values[$char];
}

Posted: Sun Apr 23, 2006 1:41 pm
by feyd
ahem: ord()

Posted: Sun Apr 23, 2006 2:09 pm
by R4000
Heh, your maths seems pretty stupid to be honest, (why add 2 zeros?)
But here ya go:

Code: Select all

$DC1 = 0x11;
$ESC = 0x1b;
$BCC = ($DC1+0x07+$ESC+ord('G')+ord('R')+0+0+100+100);
printf("%d\n", $BCC);

Alot of C functions exist in PHP (proberly because PHP is coded in C? I dunno :P)

Posted: Sun Apr 23, 2006 3:32 pm
by madru
no, it is not stupid, 0,0 was onlu a example..........

Code: Select all

short drawRect(short x1, short x2, short y1, short y2) 
{
	char BCC;
	
	BCC=(DC1+0x07+ESC+'G'+'R'+x1+x2+y1+y2)%256;
	SPI_write(DC1);
	SPI_write(0x07);
	SPI_write(ESC);
	SPI_write('G');
	SPI_write('R');
	SPI_write(x1);
	SPI_write(x2);
	SPI_write(y1);
	SPI_write(y2);
	SPI_write(BCC);

	read=SPI_read();
	return read;
}

Posted: Sun Apr 23, 2006 3:54 pm
by timvw
/me crawls under stone.

Posted: Sun Apr 23, 2006 4:07 pm
by R4000

Code: Select all

function drawRect($x1,$x2,$y1,$y2){
   $DC1 = 0x11;
   $ESC = 0x1b; 
   $BCC=($DC1+0x07+$ESC+ord('G')+ord('R')+$x1+$x2+$y1+$y2)%256
   SPI_write($DC1);
   SPI_write(0x07);
   SPI_write($ESC);
   SPI_write(ord('G'));
   SPI_write(ord('R'));
   SPI_write($x1);
   SPI_write($x2);
   SPI_write($y1);
   SPI_write($y2);
   SPI_write($BCC);
   $read = SPI_read();
   return $read;
}
What exactly does this do?