LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (https://www.linuxquestions.org/questions/programming-9/)
-   -   hex output -> output hex (https://www.linuxquestions.org/questions/programming-9/hex-output-output-hex-4175504371/)

jonnyog 05-08-2014 05:04 PM

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

michaelk 05-08-2014 06:08 PM

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?

astrogeek 05-08-2014 06:21 PM

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?

sundialsvcs 05-08-2014 07:08 PM

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.

ntubski 05-08-2014 09:36 PM

Quote:

Originally Posted by sundialsvcs (Post 5167343)
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.

HTTP headers are always text, but the content can be whatever encoding you want.

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');
$timebytes = pack("I", $time());
echo $timebytes;


jonnyog 05-12-2014 09:44 AM

thanks for replies, on a slow connection, will get more info and continue discussion....

jonnyog 06-24-2014 09:32 AM

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.