31

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

How can I do this?

4
  • 2
    Could we see some code ? What have you tried already ? Commented Mar 29, 2011 at 13:10
  • 3
    what do you mean by convert? turning asciivalues into their character counter parts? Commented Mar 29, 2011 at 13:11
  • 1
    What is your question? Is it related to your previous one? stackoverflow.com/questions/5470030/… Commented Mar 29, 2011 at 13:11
  • 1
    thought this was a duplicate of stackoverflow.com/questions/885597/string-to-byte-array-in-php BUT IT'S NOT, SORRY (can't remove the post) Commented Mar 29, 2011 at 13:14

5 Answers 5

88
+50

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).

Sign up to request clarification or add additional context in comments.

9 Comments

@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 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 ?
@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. 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.
The 'call_user_func_array()' should really be replaced by the Argument unpacking via '...' see: php.net/manual/en/migration56.new-features.php So the code would be pack('C*',...$bytes)
|
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.

2 Comments

would be nice if you had given a usable example related to the question.
this is the fastest executing solution
2

Yet another way:

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

Hurray!

Comments

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.

Comments

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.