Viewed   108 times

Say I have a string in php, that prints out to a text file like this:


How do I get the byte codes of this to my text file rather than the funky ascii characters?



Use the ord function


$var = "nÖ§9q1Fª£ˆæÓ§Œ_»—Ló]j";

for($i = 0; $i < strlen($var); $i++)
   echo ord($var[$i])."<br/>";
Sunday, August 7, 2022

Here is what you can do :

echo changeLetter("tä[email protected]", 'ä', 'ae'), PHP_EOL;
echo changeLetter("tü[email protected]", 'ü', 'ue'), PHP_EOL;
echo changeLetter("tö[email protected]", 'ö', 'oe'), PHP_EOL;


[email protected]
[email protected]
[email protected]

Function Used

function changeLetter($str, $needle, $val, $pos = 0) {
    $next = function ($str, &$pos) {
        if (! isset($str[$pos]))
            return false;
        $char = ord($str[$pos]);
        if ($char < 128) {
            return $str[$pos ++];
        } else {
            if ($char < 224) {
                $bytes = 2;
            } elseif ($char < 240) {
                $bytes = 3;
            } elseif ($char < 248) {
                $bytes = 4;
            } elseif ($char = 252) {
                $bytes = 5;
            } else {
                $bytes = 6;
            $str = substr($str, $pos, $bytes);
            $pos += $bytes;
            return $str;
    $mstr = "";
    while(($chr = $next($str, $pos)) !== false) {
        $mstr .= $chr == $needle ? $val : $chr;
    return $mstr;
Wednesday, August 24, 2022

The code:

public class Test {
    public static void main(String args[]) {
        String string = args[0];
        System.out.println("last character: " +
                           string.substring(string.length() - 1)); 

The output of java Test abcdef:

last character: f
Friday, October 21, 2022

That's what happens when someone sends you an email in outlook, so watch out, because you're totally missing the opening pair of those single quotes, and closing pair for the double quotes. But really, you need to be using html entity codes.

Why? Because there's a hell of a lot more out there in the wilderness than those two codes you posted above, in-fact, you've only shown half the set of quotes (ie, you missed the closing single quote, and opening double quote). There's hundreds, you need to be encoding them in a better way than string replace.

There's a couple ways to manage the translation from encoding to html entity.

There's more also, but they basically do the same thing.

Wednesday, August 31, 2022

Add your string as a watch, then edit the watch expression and append ".ToCharArray()" to view it as an array of chars. When you expand your watch you will see char code next to each individual char. Checking "Hexadecimal display" will show you hex codes for each character.

Wednesday, December 14, 2022
Only authorized users can answer the search term. Please sign in first, or register a free account.
Not the answer you're looking for? Browse other questions tagged :