hex output -> output hex
Hi,
a guy asked me to output some hex instead of ascii - he is using it on a embedded device and so wants as little resource use as possible i'm using php like this <?php //set timezone date_default_timezone_set("Europe/Dublin"); //get time stamp $time = time(); //format date string $date_str = date("w,d,m,y,H,i,s",$time); //explode string into array and map ascii values to hex $date_a = array_map("myhex",explode(',',$date_str)); //count values and hex this value, append implode string $date_str = myhex(count($date_a)).'|'.implode($date_a); //output result echo $date_str; //myhex function function myhex($v){ $hex = sprintf('%02.x', $v); return '0x'.$hex; } ?> //ascii output looks like this 19|4,08,05,14,22,57,58 //hex output looks like this 0x07|0x040x080x050x0e0x160x3a0x2f Now he says I am still sending ascii! - he wants the hex in machine code (i.e. bytes) I am completey stumped - don't know what he is looking for any ideas about how to do this - is it even possible? do i need to use a different programming language? all help appreciated!!! jonny |
Well I'm confused (not that it takes much)
What is this embedded device and do you know what this guy is trying to accomplish? Requesting hex or machine code can mean different things to different people. The output in your program is the ASCII hex representation vs binary data. Do you need to write the data to a file? |
You are sending an ascii formatted date string which is about as compact as a formatted date string can be without information loss.
Sending as ascii hex bytes will be longer than the original ascii. Sending as the corresponding binary is just the ascii itself in the case given. Ask him for an example of ascii and the corresponding "hex" that he expects... I would be curious to see that. And by the way, you are sending it from PHP so can we assume it is via http? |
You need to explain to him the implications of the fact that you are necessarily using the HTTP protocol ... which is character-oriented. Even when you download other kinds of content, it is still encoded.
|
Quote:
The terms "binary" and "hex" unfortunately mean several contradictory things. In this case, I see your example is printing some numbers and formatting them into decimal. When someone asks for "hex", they could mean formatting into hexadecimal like you tried, but here they probably meant to get the numbers in some "raw" encoding, as opposed to text encoded. Maybe you want something like this: Code:
header('Content-type: application/octet-stream'); |
thanks for replies, on a slow connection, will get more info and continue discussion....
|
this is exactly what i was looking for, thanks
header('Content-type: application/octet-stream'); $timebytes = pack("I", $time()); echo $timebytes; |
All times are GMT -5. The time now is 10:29 AM. |