ProgrammingThis forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
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?
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 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.
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.
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.