Quote
>Hi..
>I want to save some texts in crypted form in a file. It doesnt even have
>to be really crypted, but just in a non-text form , so it cant be read by
>text editors.. Ive tried to save it with array of chars, records, array of
>integers with the chars in ascii numbers. but it wont work, the letters appear
>no matter how i save it.. I now wonder , if someone could help me.
>Im not familiar with encoding/crypting, maybe you could give me a quick
>lesson.A pascal program would be great to show what i should do..
One possibility is to simply manipulate each character before writing it
out. If the file is of type "text", then the most "natural" way to process it
will preserve the line structure, but you can easily scramble the characters.
One thing to take note of, however, is that you probably will want to only
"encrypt" the printable character set, ' '..'~'. One method of doing this
safely is to first move the characters down from the range 32 .. 126 to 0 ..
94, do some operation that is invertable (add 17, then do a mod 95), and shift
back up.
FUNCTION encrypt (letter : char) : char;
BEGIN { encrypt }
if (ord(' ') <= letter) and (letter <= ord('~'))
then encrypt := chr (ord(' ') + ((ord(letter) - ord(' ') + 17)
mod succ(ord('~') - ord(' ')))
else encrypt := letter
END;
ord(letter) - ord(' ') maps the printable characters between space and ~ into
the range 0 .. 94. I then add 17 (or some other encoding number), keep the
answer in the range 0 .. 94 (by using the mod function, again using ord('~') -
ord(' ') to store the range, in case it really isn't 95), then adding the
space back in to shift it back into the printable character range.
Quote
>My other question: How do i save both integers and chars and
booleans etc..>in ONE file. If i do it with records they will be written in a
prechosen >order, what if I dont want that, and I want to save space?
Depends. If you want to save them in "random" order, you are probably
best off saving them in a text file, whereby you save the ascii representation
(i.e. your file might contain the characters "1, 53.4, true, 23, skidoo". It
would then be up to you to read the file and decide what kind of information
is present (by looking for decimal points, reserved words such as "true",
etc.) to tell you if the data represent reals, integers, booleans, or
characters.
On the other hand, if you KNOW your data format, and it is fixed, you can
often get some space savings by writing files of (fixed format) records.
Depends on what your needs are.
Bob Schor
Pascal Enthusiast