-3

I was wondering how one would get the Text representation of a series of bytes in PHP? (Basically, the PHP version of C#'s Encoding.getString(string s) method.)

I've been scouring google, and I can find how to get a textstring-to-bytes, but not the other way around.

Charles
  • 50,943
  • 13
  • 104
  • 142
Toby Person
  • 211
  • 1
  • 4
  • 3
    PHP strings are simple collections of sequential bytes, with no knowledge whatsoever of character encoding or charsets. Have you tried `echo`? You may need to provide more information on what your actual goals are. – Charles Jan 03 '13 at 22:09
  • 1
    [What Every Programmer Absolutely, Positively Needs To Know About Encodings And Character Sets To Work With Text](http://kunststube.net/encoding/) – deceze Jan 03 '13 at 22:14
  • My goals were for some sort of validation script. Basically, I'd have a desktop application which would be the login form for a program, and it'd get the bytes of the entered text (in int-like form), then pass it to the PHP script through the URL ($_REQUEST). The PHP script would then decode the int-form bytes back into their string value. This was primarily to hide the credentials for security purposes. If this is not possible, or is a notoriously bad "encryption" method, could you please suggest a better method? Thanks. – Toby Person Jan 03 '13 at 22:17
  • 2
    Don't roll your own encryption, hashing, obfscuation, etc. Your use case calls for SSL. – Charles Jan 03 '13 at 22:18

1 Answers1

2

In PHP such a method:

Encoding.GetString Method (Byte[])

is not necessary because in PHP, strings are like Byte[]. And that's it. So there is no such method, however you can easily write it yourself:

function Encoding_GetString($stringOfBytes) {
    return (string) $byteString;
}

If you want to convert an array of integers in the range of 0-255 into a string, you can use chr, array_map and implode:

$string = implode(array_map('chr', $arrayOfByteIntegers));
hakre
  • 193,403
  • 52
  • 435
  • 836