Link to home
Start Free TrialLog in
Avatar of smotbd
smotbd

asked on

500: simply send hex

I am implementing a simple protocol and TWO bytes of this protocol represents the packet sequence number.....so this must increment by one on each packet sent. How can I increment a value and send it as hex?
I have started as follows:

$count = 0;   //set $count with initial value of 0

for ($i = 1; $i <= 1000; $i++) {     //for-loop to loop certain amount of times...

echo $seq = sprintf("%04x<P>", $count); //put $count in to variable $seq in 2-byte hex format
   
/*now, how should I send these hex values? When I send $seq as it is then it appears as ascii on the other side of the
network and then the ethereal shows up the hex equivalent of the ascii sent........but I want it sent as hex in the first place. echo $seq clearly shows $seq incrementing in 2-byte hex so why aren't the values sent as hex when I send $seq. WHat do I need to do to it. */

$count++; increment
}

Please complete the above or feel free to change the above completely....just aslong as it works!!

I need this resolved asap please...hence 500pnts
Thank you v much
Avatar of hernst42
hernst42
Flag of Germany image

Does the following not work:

<?php
$count = 0;   //set $count with initial value of 0

for ($i = 1; $i <= 1000; $i++) {     //for-loop to loop certain amount of times...
    echo chr(floor($count/256)) . chr($count%256);
    $count++;
}
My guess is, is that the echo construct converts the value to string to be able to echo it. Maybe using printf instead of sprintf directly will have better results:

$count = 0;   //set $count with initial value of 0

for ($i = 1; $i <= 1000; $i++) {     //for-loop to loop certain amount of times...

printf("%04x<P>", $count);
   
$count++; increment
}
Avatar of smotbd
smotbd

ASKER

But I need to send it across the network so that's why I have a variable $seq  i.e. $seq= sprintf("%04x<P>", $count);
I then send the $seq variable. I put the echo in just to proove that it's counting in hex - don't need(or want) echo.

So TeRRef, do you mean e.g. $seq = printf("%04x<P>", $count); as oppose to just printf("%04x<P>", $count);  ?????
and then send $seq?????? And same in your case hernst42

Printing it out on screen isn't my aim - I can do that....., my aim is to send this hex across the network - please read 1st question again if still unclear.
if you want to convert an unsigned 16bit int to a 2 bit hex code use
$seq = chr(floor($count/256)) . chr($count%256);
then use fwrite($handle, $seq, 2);
So then you left out some relevant code that might contain the quirk. Are you using a socket to send the data over the network? Show us that part of the code...
Avatar of smotbd

ASKER

Yes, ok I should have shown socket code. I am using stream_socket_sendto. This code is cut down a little to make it clearer for you but all the relevent code is there.......the sockets work fine. If $count is written as $c = "\x00\x00" then it is sent across the network as hex but I can't increment this string as hex.  Also "\x00\x00" is not the same as '\x'.'0'.'0''\x'.'0'.'0' and not the same as "\x" . "0" . "0" . "\x" . "0" . "0"     I can't understand why.


$socket1 = stream_socket_server("udp://my_ip:port", $errno, $errstr, STREAM_SERVER_BIND);

$count = 0; //this will increment in the loop below

/************************ Stream via UDP socket ************************************/
for ($i = 1; $i <= 1000; $i++) {     //for-loop to loop certain amount of times...

$seq = sprintf("%04x<P>", $count); //this does increment as 2 byte hex i.e. 0009,000a,0010 etc etc
        //$seq = "\x".$seq{0}.$seq{1}."\x".$seq{2}.$seq{3}; //sending this doesn't work
         //$seq = chr($seq{0}.$seq{1}).chr($seq{2}.$seq{3}); //sending this doesn't work
        //$seq = chr(floor($seq/256)).chr(floor($seq&255));   //sending this doesn't work

stream_socket_sendto($socket1, "$seq", "some_ip:some_port\n\r");
$count++;
}
First step would be to remove the double quotes around $seq in stream_socket_sendto() to see if that makes a diffrence.
Avatar of smotbd

ASKER

Made no difference I'm afraid
Try:
$socket1 = stream_socket_server("udp://my_ip:port", $errno, $errstr, STREAM_SERVER_BIND);
$count = 0; //this will increment in the loop below

/************************ Stream via UDP socket ************************************/
for ($i = 1; $i <= 1000; $i++) {     //for-loop to loop certain amount of times...
    $seq = chr(floor($count/256)) . chr($count%256);
    stream_socket_sendto($socket1, $seq, null, "some_ip:some_port\n\r");
     $count++;
}

You noticed that you do not use stream_socket_sendto correctly, the 4th parameter is the address not the third. The above code works.
Avatar of smotbd

ASKER

Yes the socket line of coded I posted did lack the 3rd parameter but I've always had the null in there.......I somehow deleated the null when I was changing the IP address for posting the message sorry.

I have found the solution and it's as shown below; it increments a decimal value and sends the equivalent 2-byte hex values through the socket - it works....:



$c = 0; //this will increment in the loop below
$count = pack("nC*", $c); //represent the decimal c in 2-byte hex format

for ($i = 1; $i <= 1000; $i++) {     //for-loop to loop certain amount of times...
stream_socket_sendto($socket1, "$seq", 0, "some_ip:some_port\n\r");
$c++; //increment c
}



Thanks for all your comments
No comment has been added to this question in more than 21 days, so it is now classified as abandoned.

I will leave the following recommendation for this question in the Cleanup topic area:
   PAQ with points refunded

Any objections should be posted here in the next 4 days. After that time, the question will be closed.

Huji
EE Cleanup Volunteer
ASKER CERTIFIED SOLUTION
Avatar of Computer101
Computer101
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial