PHP - Convert to ASCII

  • alexislalas
  • Novice
  • Novice
  • alexislalas
  • Posts: 32

Post 3+ Months Ago

hello,

i have a number and want to convert it to ascii. how can i do it?

thanks!
  • Anonymous
  • Bot
  • No Avatar
  • Posts: ?
  • Loc: Ozzuland
  • Status: Online

Post 3+ Months Ago

  • UPSGuy
  • Lurker ಠ_ಠ
  • Web Master
  • User avatar
  • Posts: 2733
  • Loc: Nashville, TN

Post 3+ Months Ago

See if this function will do what you need.

Code: [ Select ]
 
<?php
 
echo ord("5");
 
?>
 
 
  1.  
  2. <?php
  3.  
  4. echo ord("5");
  5.  
  6. ?>
  7.  
  8.  


It occurs to me that you may want the reverse effect, so if that's the case, try chr() too.
  • alexislalas
  • Novice
  • Novice
  • alexislalas
  • Posts: 32

Post 3+ Months Ago

so, i have this value: 05152009090637000601406077

that is what i want to convert to ascii.

i do:
ord("05152009090637000601406077");
result = 48

chr("05152009090637000601406077");
result = ÿ

now, how can i get 48 & ÿ back to 05152009090637000601406077?

thanks!
  • UPSGuy
  • Lurker ಠ_ಠ
  • Web Master
  • User avatar
  • Posts: 2733
  • Loc: Nashville, TN

Post 3+ Months Ago

Urmm, lol, ord() will only convert the first character in the string, which is why you see 48. I would think chr() is the same. So what's the point? What should we be seeing as output? I think your description of the issue is a bit incomplete.
  • alexislalas
  • Novice
  • Novice
  • alexislalas
  • Posts: 32

Post 3+ Months Ago

lol...

i want the whole string (05152009090637000601406077) to be converted into ascii.

so, what i understand is that 48 is the value of 0? that means i would have to do it for every single digit of the string?
  • UPSGuy
  • Lurker ಠ_ಠ
  • Web Master
  • User avatar
  • Posts: 2733
  • Loc: Nashville, TN

Post 3+ Months Ago

05152009090637000601406077 does not have one single ascii representation, which is why I say that something feels off. Those numbers could be singles ascii chars or double ascii chars, so I was hoping you knew the proposed outcome.
  • alexislalas
  • Novice
  • Novice
  • alexislalas
  • Posts: 32

Post 3+ Months Ago

oooohhhh... i understand.

well... let me then tell you the whole story, lol.

we have this string: 000601406077
we then have another string that is the date & hour (MMDDYYYYHHIISS): 05152009090637

we concatenate them 05152009090637000601406077.

i want to convert that ascii and then the ascii to hexadecimal.

and about your answer, what do you mean by single ascii or double ascii characters?
  • devilwood
  • Silver Member
  • Silver Member
  • User avatar
  • Posts: 436

Post 3+ Months Ago

Sounds like you just need to do what UPSGuy is saying using the ord().

Something like:

Code: [ Select ]
$string = "05152009090637000601406077";
for ($i = 0; $i < strlen($string); $i++) {         
$to_ascii += ord($string[$i]);     
}
$newhex = bin2hex($to_ascii);
echo = $newhex;
  1. $string = "05152009090637000601406077";
  2. for ($i = 0; $i < strlen($string); $i++) {         
  3. $to_ascii += ord($string[$i]);     
  4. }
  5. $newhex = bin2hex($to_ascii);
  6. echo = $newhex;


You may want to check to make sure the loop is re-building the ascii values correctly to the variable $to_ascii and that bin2hex() is right. I think pack() will convert hex back to ascii. Lastly, you may want to package the loop and conversion to hex into a function to make it easier to use.
  • Bogey
  • Genius
  • Genius
  • Bogey
  • Posts: 8399
  • Loc: USA

Post 3+ Months Ago

I don't know if this is what you want...
Code: [ Select ]
<?php
function hex2bin($str) {
    $bin = "";
    $i = 0;
    do {
        $bin .= chr(hexdec($str{$i}.$str{($i + 1)}));
        $i += 2;
    } while ($i < strlen($str));
    return $bin;
}

$string = "05152009090637000601406077";
echo $string . '<br /><br />';
$newhex = bin2hex($string);

echo $newhex . '<br /><br />';

$to_this = hex2bin($newhex);
print_r($to_this);
?>
  1. <?php
  2. function hex2bin($str) {
  3.     $bin = "";
  4.     $i = 0;
  5.     do {
  6.         $bin .= chr(hexdec($str{$i}.$str{($i + 1)}));
  7.         $i += 2;
  8.     } while ($i < strlen($str));
  9.     return $bin;
  10. }
  11. $string = "05152009090637000601406077";
  12. echo $string . '<br /><br />';
  13. $newhex = bin2hex($string);
  14. echo $newhex . '<br /><br />';
  15. $to_this = hex2bin($newhex);
  16. print_r($to_this);
  17. ?>

hex2bin came from here

Post Information

  • Total Posts in this topic: 9 posts
  • Users browsing this forum: No registered users and 106 guests
  • You cannot post new topics in this forum
  • You cannot reply to topics in this forum
  • You cannot edit your posts in this forum
  • You cannot delete your posts in this forum
  • You cannot post attachments in this forum
 
cron
 

© 1998-2014. Ozzu® is a registered trademark of Unmelted, LLC.