Skip to content
Advertisement

PHP encode and decode text to ASCII characters

I plan to make a class that can encode files into ASCII characters and decode into readable texts. Basically, the idea of my encoding is to encode all non-space characters into ASCII and replace the spaces into random non-numeric characters.

JavaScript

In this manner, if I have an entire file with multiple paragraphs and encode it, it wouldn’t be so obvious that I converted the characters to ASCII. So far this is my code:

JavaScript

I’m struggling to figure out the problem with my code. This is the result of it:

JavaScript

Please enlighten me!

Advertisement

Answer

Two problems: you were attempting to pass multiple numbers to chr() which is designed to return a single character, and you had no way to tell how long your numbers were going to be. Using your example above: is it 72, 101, 108, 108 or is it 72, 10, 10, 81, 08? So, fix your numbers at 3 digits using sprintf() and then you’ll know when to pass data to chr() and continue with your decoding.

JavaScript
User contributions licensed under: CC BY-SA
4 People found this is helpful
Advertisement