LinuxQuestions.org
Visit Jeremy's Blog.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 05-08-2014, 05:04 PM   #1
jonnyog
LQ Newbie
 
Registered: Sep 2013
Posts: 20

Rep: Reputation: Disabled
Exclamation 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
 
Old 05-08-2014, 06:08 PM   #2
michaelk
Moderator
 
Registered: Aug 2002
Posts: 25,680

Rep: Reputation: 5894Reputation: 5894Reputation: 5894Reputation: 5894Reputation: 5894Reputation: 5894Reputation: 5894Reputation: 5894Reputation: 5894Reputation: 5894Reputation: 5894
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?
 
Old 05-08-2014, 06:21 PM   #3
astrogeek
Moderator
 
Registered: Oct 2008
Distribution: Slackware [64]-X.{0|1|2|37|-current} ::12<=X<=15, FreeBSD_12{.0|.1}
Posts: 6,263
Blog Entries: 24

Rep: Reputation: 4194Reputation: 4194Reputation: 4194Reputation: 4194Reputation: 4194Reputation: 4194Reputation: 4194Reputation: 4194Reputation: 4194Reputation: 4194Reputation: 4194
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?

Last edited by astrogeek; 05-08-2014 at 06:23 PM.
 
Old 05-08-2014, 07:08 PM   #4
sundialsvcs
LQ Guru
 
Registered: Feb 2004
Location: SE Tennessee, USA
Distribution: Gentoo, LFS
Posts: 10,649
Blog Entries: 4

Rep: Reputation: 3934Reputation: 3934Reputation: 3934Reputation: 3934Reputation: 3934Reputation: 3934Reputation: 3934Reputation: 3934Reputation: 3934Reputation: 3934Reputation: 3934
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.
 
Old 05-08-2014, 09:36 PM   #5
ntubski
Senior Member
 
Registered: Nov 2005
Distribution: Debian, Arch
Posts: 3,780

Rep: Reputation: 2081Reputation: 2081Reputation: 2081Reputation: 2081Reputation: 2081Reputation: 2081Reputation: 2081Reputation: 2081Reputation: 2081Reputation: 2081Reputation: 2081
Quote:
Originally Posted by sundialsvcs View Post
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;
 
Old 05-12-2014, 09:44 AM   #6
jonnyog
LQ Newbie
 
Registered: Sep 2013
Posts: 20

Original Poster
Rep: Reputation: Disabled
thanks for replies, on a slow connection, will get more info and continue discussion....
 
Old 06-24-2014, 09:32 AM   #7
jonnyog
LQ Newbie
 
Registered: Sep 2013
Posts: 20

Original Poster
Rep: Reputation: Disabled
this is exactly what i was looking for, thanks

header('Content-type: application/octet-stream');
$timebytes = pack("I", $time());
echo $timebytes;
 
  


Reply

Tags
ascii, byte, php



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
[bash] ASCII to HEX and hex to ascii ////// Programming 17 05-08-2018 09:55 PM
Passable nouveau kernel driver bug (MMIO read of [hex l] FAULT at [hex l]) marbangens Linux - General 1 05-24-2013 01:35 AM
Looking for tcpdump syntax to start hex output at a given byte offset in the packet silentquasar Linux - Networking 1 08-06-2011 06:03 AM
Hex output of a hex/ascii input string mlewis Programming 35 04-10-2008 12:05 PM
Hex socks Linux - General 4 02-17-2005 12:05 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 03:47 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration