29

I have an array of bytes that I'd like to map to their ASCII equivalents.

How can I do this?

alex
  • 479,566
  • 201
  • 878
  • 984
Damir
  • 54,277
  • 94
  • 246
  • 365
  • 2
    Could we see some code ? What have you tried already ? – Johann du Toit Mar 29 '11 at 13:10
  • 3
    what do you mean by convert? turning asciivalues into their character counter parts? – Fredrik Mar 29 '11 at 13:11
  • 1
    What is your question? Is it related to your previous one? http://stackoverflow.com/questions/5470030/how-to-define-array-of-bytes-in-php – fabrik Mar 29 '11 at 13:11
  • 1
    thought this was a duplicate of http://stackoverflow.com/questions/885597/string-to-byte-array-in-php BUT IT'S NOT, SORRY (can't remove the post) – fsonmezay Mar 29 '11 at 13:14

5 Answers5

88

If by array of bytes you mean:

$bytes = array(255, 0, 55, 42, 17,    );

array_map()

Then it's as simple as:

$string = implode(array_map("chr", $bytes));

foreach()

Which is the compact version of:

$string = "";
foreach ($bytes as $chr) {
    $string .= chr($chr);
}
// Might be a bit speedier due to not constructing a temporary array.

pack()

But the most advisable alternative could be to use pack("C*", [$array...]), even though it requires a funky array workaround in PHP to pass the integer list:

$str = call_user_func_array("pack", array_merge(array("C*"), $bytes)));

That construct is also more useful if you might need to switch from bytes C* (for ASCII strings) to words S* (for UCS2) or even have a list of 32bit integers L* (e.g. a UCS4 Unicode string).

mario
  • 144,265
  • 20
  • 237
  • 291
  • @alex: I'm okay with the whole badge chasing thing, but these kind of strategies shouldn't be allowed. Changing the title to please search engines while improving a bit a bad and simple question is one thing, but bounty? Seriously... – Alix Axel May 15 '11 at 14:59
  • @Alix Isn't one of the goals of the site to give good results for search engines, to knock off Experts Exchange, W3 Schools, etc? And what is so bad with a bounty? I've always wanted to place one on someone else's question, and I thought this was a good opportunity. The question itself isn't that bad, obviously the OP could of made some effort on their behalf, but it is still a decent question. Do you have the same objection to this [recent bounty](http://stackoverflow.com/questions/5473011/how-can-i-convert-array-of-bytes-to-a-string-in-php/5473057#5473057) ? – alex May 15 '11 at 23:41
  • @alex: I've nothing against the edit (just the fact that you publicly advertised that you did it for the bump), but the point of a bounty is to attract attention on questions that still have no accepted solution. In this case, the question is crappy and subjective, and the solution had already presented itself 2 months ago in the form of a perfectly good and valid one-liner. The fact that you haven't even awarded the bounty yet just reflects your true (SEO) purpose. If you do this, at least try not to be so obvious - the last thing I want is copycats using SO tools for the wrong purposes. – Alix Axel May 16 '11 at 11:51
  • @Alix Why would not awarding the bounty have anything to do with SEO? I am just waiting to see if any other solutions present themselves. Also, why am I not free to do what I want with my rep? – alex May 16 '11 at 11:55
  • @Alex: First of, you are free to do whatever you want. Regarding your SEO question, because it gets a 7-day stay on a privileged page for both users and search engines. And do you really think anyone will bother answer a -5 question when there's already a perfectly good answer that solves the problem in 3 distinct ways and was accepted 2 months ago? Really?! You're just gaming the system (and teaching others how to game it) while wasting people time with it. Lots of other better questions with still no (accepted) answer(s) at: http://stackoverflow.com/questions/?sort=unanswered. – Alix Axel May 16 '11 at 14:37
  • 1
    @Alix Axel. I've taken offense a few weeks ago when someone else offered a bounty himself after already aquiring the accepted answer (disguised repwhoring with a fairly stupid opinion post no less). It's not quite the same here. While it's already answered workably, there are a few more options. Some funny answers are still possible - two of which \*I\* dare not mention on SO. -- Also not sure if anyone will bother, but I'm certain @alex would put the attention award (which is all the bounty system shall do) on something more interesting if it comes along. – mario May 16 '11 at 22:17
  • @Alix People answer all the time when there are existing valid solutions. Sometimes it adds noise, other times other valid answers come from a different perspective. Doesn't this site have game mechanics, so using it is *gaming*? – alex May 16 '11 at 23:52
  • @mario: Anyway, I've stated my point of view and I still stand by it (even the *bytes* array is too ambiguous for anyone to answer with complete confidence), nonetheless I don't want to start a flame war. But now I'm curious about your other options... :P – Alix Axel May 17 '11 at 01:18
  • 1
    The 'call_user_func_array()' should really be replaced by the Argument unpacking via '...' see: http://php.net/manual/en/migration56.new-features.php So the code would be pack('C*',...$bytes) – Eaton Emmerich Nov 14 '17 at 14:47
11

Ammending the answer by mario for using pack(): Since PHP 5.5, you can use Argument unpacking via ...

$str = pack('C*', ...$bytes);

The other functions are fine to use, but it is preferred to have readable code.

Eaton Emmerich
  • 452
  • 4
  • 17
2

Yet another way:

$str = vsprintf(str_repeat('%c', count($bytes)), $bytes);

Hurray!

Alix Axel
  • 151,645
  • 95
  • 393
  • 500
1

Mario has already provided the best fit, but here is a more exotic way to achieve this.

$str = call_user_func_array(
        'sprintf',
        array_merge((array) str_repeat('%c', count($bytes)), $bytes)
       );

CodePad.

Community
  • 1
  • 1
alex
  • 479,566
  • 201
  • 878
  • 984
0

Below is an example of converting Yodlee MFA ByteArray to CAPTCHA image. Hope it helps someone...

You simply have to convert the byte array to string, then encode to base64.

Here is a PHP example:

$byteArray = $obj_response->fieldInfo->image; //Here you get the image from the API getMFAResponse

$string = implode(array_map("chr", $byteArray)); //Convert it to string

$base64 = base64_encode($string); //Encode to base64

$img = "<img src= 'data:image/jpeg;base64, $base64' />"; //Create the image

print($img); //Display the image
Kumar Rakesh
  • 2,718
  • 2
  • 18
  • 39