PHP - Convert to ASCII

  • alexislalas
  • Novice
  • Novice
  • No Avatar
  • Joined: Feb 27, 2007
  • Posts: 30
  • Status: Offline

Post May 15th, 2009, 6:42 am

hello,

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

thanks!
  • Anonymous
  • Bot
  • No Avatar
  • Joined: 25 Feb 2008
  • Posts: ?
  • Loc: Ozzuland
  • Status: Online

Post May 15th, 2009, 6:42 am

  • UPSGuy
  • Lurker ಠ_ಠ
  • Web Master
  • User avatar
  • Joined: Jul 25, 2005
  • Posts: 2735
  • Loc: Nashville, TN
  • Status: Offline

Post May 15th, 2009, 6:47 am

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.
I'd love to change the world, but they won't give me the source code.
  • alexislalas
  • Novice
  • Novice
  • No Avatar
  • Joined: Feb 27, 2007
  • Posts: 30
  • Status: Offline

Post May 15th, 2009, 7:09 am

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
  • Joined: Jul 25, 2005
  • Posts: 2735
  • Loc: Nashville, TN
  • Status: Offline

Post May 15th, 2009, 7:26 am

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.
I'd love to change the world, but they won't give me the source code.
  • alexislalas
  • Novice
  • Novice
  • No Avatar
  • Joined: Feb 27, 2007
  • Posts: 30
  • Status: Offline

Post May 15th, 2009, 9:01 am

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
  • Joined: Jul 25, 2005
  • Posts: 2735
  • Loc: Nashville, TN
  • Status: Offline

Post May 15th, 2009, 9:47 am

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.
I'd love to change the world, but they won't give me the source code.
  • alexislalas
  • Novice
  • Novice
  • No Avatar
  • Joined: Feb 27, 2007
  • Posts: 30
  • Status: Offline

Post May 15th, 2009, 9:51 am

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
  • Joined: Nov 18, 2007
  • Posts: 429
  • Status: Offline

Post May 20th, 2009, 10:37 pm

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
  • Bogey
  • Genius
  • User avatar
  • Joined: Jul 14, 2005
  • Posts: 8211
  • Loc: USA
  • Status: Offline

Post May 21st, 2009, 5:12 pm

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
"Bring forth therefore fruits meet for repentance:" Matthew 3:8

Post Information

  • Total Posts in this topic: 9 posts
  • Users browsing this forum: No registered users and 172 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
 
 

© 2011 Unmelted, LLC. Ozzu® is a registered trademark of Unmelted, LLC.