Author Topic: Base 64 encoder / decoder  (Read 7716 times)

0 Members and 1 Guest are viewing this topic.

Offline Dav

  • Forum Resident
  • Posts: 792
    • View Profile
Re: Base 64 encoder / decoder
« Reply #15 on: December 18, 2020, 10:25:35 am »
This thread has re-sparked my interest in binary to text encoders.  Always looking for something better than base64 to output code safe to post on forums.  Has anyone tried to implement base91 before? 

Edit: Nevermind-- looks like base91 is patented.

- Dav
« Last Edit: December 18, 2020, 10:28:50 am by Dav »

Offline SMcNeill

  • QB64 Developer
  • Forum Resident
  • Posts: 3972
    • View Profile
    • Steve’s QB64 Archive Forum
Re: Base 64 encoder / decoder
« Reply #16 on: December 18, 2020, 10:33:45 am »
@Dav

You posted an encoding routine which you use, a while back, which:

Quote
The encoded bytes range from ASC 48  "0"  to ASC 111 "o".  111 - 48  = 63 (64 characters). In that range, the comma and quotes are avoided.

I had a similar method going for my stuff, but then I ran into an issue, which might affect your encoder as well:

58 is ":", which in QB64 is a command separator.

My solution was simply to change my code so that it started at 59 instead, and thus that problem is forever avoided and I don't have to enclose the output in quotes if I want to use it for something like DATA statements.  ;)
https://github.com/SteveMcNeill/Steve64 — A github collection of all things Steve!

Offline Dav

  • Forum Resident
  • Posts: 792
    • View Profile
Re: Base 64 encoder / decoder
« Reply #17 on: December 18, 2020, 10:40:04 am »
Thanks for the tip, @SMcNeill.  Yes, I would need to change that if my output wasn't in quotes.  One problem I did have to fix in my last few encoders here was using the @ symbol.  That was interfering with posted code because of the forums way of using @ to note members, so I had to swap it out with an unused character.

Edit: Thread about the @ problem here.

- Dav
« Last Edit: December 18, 2020, 11:10:44 am by Dav »

Offline SpriggsySpriggs

  • Forum Resident
  • Posts: 1145
  • Larger than life
    • View Profile
    • GitHub
Re: Base 64 encoder / decoder
« Reply #18 on: December 18, 2020, 11:54:06 am »
One problem I did have to fix in my last few encoders here was using the @ symbol.  That was interfering with posted code because of the forums way of using @ to note members, so I had to swap it out with an unused character.

@Dav

I remember that! It feels like such a long time ago. Your code revolutionized includes for me.

By the way, how did you hide the URL and show just "here"? I haven't figured out how to do that. Never mind. Figured out how to do that
« Last Edit: December 18, 2020, 12:03:02 pm by SpriggsySpriggs »
Shuwatch!

Offline bplus

  • Global Moderator
  • Forum Resident
  • Posts: 8053
  • b = b + ...
    • View Profile
Re: Base 64 encoder / decoder
« Reply #19 on: December 18, 2020, 12:31:33 pm »
I found we were on this very same subject last year! Petr and AndyA were doing things.

I am wondering if all this data packing slows program loads?

Offline Petr

  • Forum Resident
  • Posts: 1720
  • The best code is the DNA of the hops.
    • View Profile
Re: Base 64 encoder / decoder
« Reply #20 on: December 18, 2020, 01:05:07 pm »
Quote
I am wondering if all this data packing slows program loads?

My version slows down, AndyA's version practically not at all. This is the difference between using the logical AND operator (AndyA version) and / or strings (my version).

Offline Dav

  • Forum Resident
  • Posts: 792
    • View Profile
Re: Base 64 encoder / decoder
« Reply #21 on: December 18, 2020, 01:19:13 pm »
I haven't tested mine for speed, but I imagine it's slow as molasses. Not familiar with AndyA's version (I didn't hang around here too much last year) but I'll search for it.   Maybe these encoding schemes would make a good test in a benchmark program!

- Dav
« Last Edit: December 18, 2020, 01:29:44 pm by Dav »

Offline NOVARSEG

  • Forum Resident
  • Posts: 509
    • View Profile
Re: Base 64 encoder / decoder
« Reply #22 on: December 18, 2020, 04:43:02 pm »
Here is base 256 to base 128 encoder / decoder


detected bug . . . .fixed

Code: QB64: [Select]
  1. 'BASE 128 encoder  / decoder
  2. 'input string is encoded then decoded
  3.  
  4. DIM b8, b7, b6, b5, b4, b3, b2, b1 AS INTEGER
  5. DIM string1 AS STRING 'input string
  6. DIM string2 AS STRING 'output string
  7. string1 = "ASCII stands for American Standard Code for Information Interchange..." + CHR$(166) + CHR$(167) + CHR$(168) + CHR$(169) + CHR$(170) + CHR$(171) + CHR$(172)
  8. 'string1 = "ASCII stands for American Standard Code for Information Interchange..."
  9.  
  10. 'string1 must be a multiple of 7 bytes
  11.  
  12. PRINT "string1"
  13. PRINT string1
  14. n = 45 'offset into ASCII table
  15.  
  16. 'Encode
  17. FOR x% = 1 TO LEN(string1) STEP 7
  18.     b7 = ASC(MID$(string1, x%, 1))
  19.     b6 = ASC(MID$(string1, x% + 1, 1))
  20.     b5 = ASC(MID$(string1, x% + 2, 1))
  21.     b4 = ASC(MID$(string1, x% + 3, 1))
  22.     b3 = ASC(MID$(string1, x% + 4, 1))
  23.     b2 = ASC(MID$(string1, x% + 5, 1))
  24.     b1 = ASC(MID$(string1, x% + 6, 1))
  25.  
  26.     IF b7 >= 128 THEN
  27.         b7 = b7 - 128
  28.         b8 = 64
  29.     END IF
  30.     s7 = CHR$(b7 + n)
  31.  
  32.     IF b6 >= 128 THEN
  33.         b6 = b6 - 128
  34.         b8 = b8 + 32
  35.     END IF
  36.     s6 = CHR$(b6 + n)
  37.  
  38.     IF b5 >= 128 THEN
  39.         b5 = b5 - 128
  40.         b8 = b8 + 16
  41.     END IF
  42.     s5 = CHR$(b5 + n)
  43.  
  44.     IF b4 >= 128 THEN
  45.         b4 = b4 - 128
  46.         b8 = b8 + 8
  47.     END IF
  48.     s4 = CHR$(b4 + n)
  49.  
  50.     IF b3 >= 128 THEN
  51.         b3 = b3 - 128
  52.         b8 = b8 + 4
  53.     END IF
  54.     s3 = CHR$(b3 + n)
  55.  
  56.     IF b2 >= 128 THEN
  57.         b2 = b2 - 128
  58.         b8 = b8 + 2
  59.     END IF
  60.     s2 = CHR$(b2 + n)
  61.  
  62.     IF b1 >= 128 THEN
  63.         b1 = b1 - 128
  64.         b8 = b8 + 1
  65.     END IF
  66.     s1 = CHR$(b1 + n)
  67.  
  68.     s8 = CHR$(b8 + n)
  69.  
  70.     string2 = string2 + (s8 + s7 + s6 + s5 + s4 + s3 + s2 + s1)
  71.  
  72. NEXT x%
  73.  
  74. string1 = ""
  75.  
  76. PRINT "string1 encoded"
  77. PRINT string2
  78.  
  79.  
  80. 'Decode
  81. FOR x% = 1 TO LEN(string2) STEP 8
  82.  
  83.     b8 = ASC(MID$(string2, x%, 1)) - n
  84.     b7 = ASC(MID$(string2, x% + 1, 1)) - n
  85.     b6 = ASC(MID$(string2, x% + 2, 1)) - n
  86.     b5 = ASC(MID$(string2, x% + 3, 1)) - n
  87.     b4 = ASC(MID$(string2, x% + 4, 1)) - n
  88.     b3 = ASC(MID$(string2, x% + 5, 1)) - n
  89.     b2 = ASC(MID$(string2, x% + 6, 1)) - n
  90.     b1 = ASC(MID$(string2, x% + 7, 1)) - n
  91.  
  92.     IF b8 >= 64 THEN
  93.         b8 = b8 - 64
  94.         b7 = b7 + 128
  95.     END IF
  96.     s7 = CHR$(b7)
  97.  
  98.     IF b8 >= 32 THEN
  99.         b8 = b8 - 32
  100.         b6 = b6 + 128
  101.     END IF
  102.     s6 = CHR$(b6)
  103.  
  104.     IF b8 >= 16 THEN
  105.         b8 = b8 - 16
  106.         b5 = b5 + 128
  107.     END IF
  108.     s5 = CHR$(b5)
  109.  
  110.     IF b8 >= 8 THEN
  111.         b8 = b8 - 8
  112.         b4 = b4 + 128
  113.     END IF
  114.     s4 = CHR$(b4)
  115.  
  116.     IF b8 >= 4 THEN
  117.         b8 = b8 - 4
  118.         b3 = b3 + 128
  119.     END IF
  120.     s3 = CHR$(b3)
  121.  
  122.     IF b8 >= 2 THEN
  123.         b8 = b8 - 2
  124.         b2 = b2 + 128
  125.     END IF
  126.     s2 = CHR$(b2)
  127.  
  128.     IF b8 >= 1 THEN
  129.         b8 = b8 - 1
  130.         b1 = b1 + 128
  131.     END IF
  132.     s1 = CHR$(b1)
  133.  
  134.     string1 = string1 + (s7 + s6 + s5 + s4 + s3 + s2 + s1)
  135.  
  136.  
  137. NEXT x%
  138.  
  139. PRINT "decoded"
  140. "
  141. PRINT string1
  142. PRINT
  143. PRINT
  144.  
  145. END
  146.  
« Last Edit: December 18, 2020, 07:23:01 pm by NOVARSEG »

Offline bplus

  • Global Moderator
  • Forum Resident
  • Posts: 8053
  • b = b + ...
    • View Profile
Re: Base 64 encoder / decoder
« Reply #23 on: December 18, 2020, 06:47:51 pm »
Quote
FOR x% = 1 TO LEN(string1) STEP 7
    b7 = ASC(MID$(string1, x%, 1))
    b6 = ASC(MID$(string1, x% + 1, 1))
    b5 = ASC(MID$(string1, x% + 2, 1))
    b4 = ASC(MID$(string1, x% + 3, 1))
    b3 = ASC(MID$(string1, x% + 4, 1))
    b2 = ASC(MID$(string1, x% + 5, 1))
    b1 = ASC(MID$(string1, x% + 6, 1))
Oh I think I see the error...

Anyway here is a tip:
Code: QB64: [Select]
  1. s$ = "string"
  2. FOR i = 1 TO LEN(s$)
  3.     PRINT ASC(s$, i); "  =  "; ASC(MID$(s$, i, 1)) '   <<<<<<<<<<<<< tip
« Last Edit: December 18, 2020, 07:25:22 pm by bplus »

Offline NOVARSEG

  • Forum Resident
  • Posts: 509
    • View Profile
Re: Base 64 encoder / decoder
« Reply #24 on: December 18, 2020, 07:35:59 pm »
Thanks bplus

code updated

Code: QB64: [Select]
  1. 'BASE 128 encoder  / decoder
  2. 'input string is encoded then decoded
  3.  
  4. DIM b8, b7, b6, b5, b4, b3, b2, b1 AS INTEGER
  5. DIM string1 AS STRING 'input string
  6. DIM string2 AS STRING 'output string
  7. string1 = "ASCII stands for American Standard Code for Information Interchange..." + CHR$(166) + CHR$(167) + CHR$(168) + CHR$(169) + CHR$(170) + CHR$(171) + CHR$(172)
  8. 'string1 = "ASCII stands for American Standard Code for Information Interchange..."
  9.  
  10. 'string1 must be a multiple of 7 bytes
  11.  
  12. PRINT "string1"
  13. PRINT string1
  14. n = 45 'offset into ASCII table
  15.  
  16. 'Encode
  17. FOR x% = 1 TO LEN(string1) STEP 7
  18.  
  19.     b7 = ASC(string1, x%)
  20.     b6 = ASC(string1, x% + 1)
  21.     b5 = ASC(string1, x% + 2)
  22.     b4 = ASC(string1, x% + 3)
  23.     b3 = ASC(string1, x% + 4)
  24.     b2 = ASC(string1, x% + 5)
  25.     b1 = ASC(string1, x% + 6)
  26.  
  27.     IF b7 >= 128 THEN
  28.         b7 = b7 - 128
  29.         b8 = 64
  30.     END IF
  31.     s7 = CHR$(b7 + n)
  32.  
  33.     IF b6 >= 128 THEN
  34.         b6 = b6 - 128
  35.         b8 = b8 + 32
  36.     END IF
  37.     s6 = CHR$(b6 + n)
  38.  
  39.     IF b5 >= 128 THEN
  40.         b5 = b5 - 128
  41.         b8 = b8 + 16
  42.     END IF
  43.     s5 = CHR$(b5 + n)
  44.  
  45.     IF b4 >= 128 THEN
  46.         b4 = b4 - 128
  47.         b8 = b8 + 8
  48.     END IF
  49.     s4 = CHR$(b4 + n)
  50.  
  51.     IF b3 >= 128 THEN
  52.         b3 = b3 - 128
  53.         b8 = b8 + 4
  54.     END IF
  55.     s3 = CHR$(b3 + n)
  56.  
  57.     IF b2 >= 128 THEN
  58.         b2 = b2 - 128
  59.         b8 = b8 + 2
  60.     END IF
  61.     s2 = CHR$(b2 + n)
  62.  
  63.     IF b1 >= 128 THEN
  64.         b1 = b1 - 128
  65.         b8 = b8 + 1
  66.     END IF
  67.     s1 = CHR$(b1 + n)
  68.  
  69.     s8 = CHR$(b8 + n)
  70.  
  71.     string2 = string2 + (s8 + s7 + s6 + s5 + s4 + s3 + s2 + s1)
  72.  
  73. NEXT x%
  74.  
  75. string1 = ""
  76.  
  77. PRINT "string1 encoded"
  78. PRINT string2
  79.  
  80.  
  81. 'Decode
  82. FOR x% = 1 TO LEN(string2) STEP 8
  83.  
  84.     b8 = ASC(string2, x%) - n
  85.     b7 = ASC(string2, x% + 1) - n
  86.     b6 = ASC(string2, x% + 2) - n
  87.     b5 = ASC(string2, x% + 3) - n
  88.     b4 = ASC(string2, x% + 4) - n
  89.     b3 = ASC(string2, x% + 5) - n
  90.     b2 = ASC(string2, x% + 6) - n
  91.     b1 = ASC(string2, x% + 7) - n
  92.  
  93.     IF b8 >= 64 THEN
  94.         b8 = b8 - 64
  95.         b7 = b7 + 128
  96.     END IF
  97.     s7 = CHR$(b7)
  98.  
  99.     IF b8 >= 32 THEN
  100.         b8 = b8 - 32
  101.         b6 = b6 + 128
  102.     END IF
  103.     s6 = CHR$(b6)
  104.  
  105.     IF b8 >= 16 THEN
  106.         b8 = b8 - 16
  107.         b5 = b5 + 128
  108.     END IF
  109.     s5 = CHR$(b5)
  110.  
  111.     IF b8 >= 8 THEN
  112.         b8 = b8 - 8
  113.         b4 = b4 + 128
  114.     END IF
  115.     s4 = CHR$(b4)
  116.  
  117.     IF b8 >= 4 THEN
  118.         b8 = b8 - 4
  119.         b3 = b3 + 128
  120.     END IF
  121.     s3 = CHR$(b3)
  122.  
  123.     IF b8 >= 2 THEN
  124.         b8 = b8 - 2
  125.         b2 = b2 + 128
  126.     END IF
  127.     s2 = CHR$(b2)
  128.  
  129.     IF b8 >= 1 THEN
  130.         b8 = b8 - 1
  131.         b1 = b1 + 128
  132.     END IF
  133.     s1 = CHR$(b1)
  134.  
  135.     string1 = string1 + (s7 + s6 + s5 + s4 + s3 + s2 + s1)
  136.  
  137.  
  138. NEXT x%
  139.  
  140. PRINT "decoded"
  141. PRINT string1
  142.  
  143.  

Offline SMcNeill

  • QB64 Developer
  • Forum Resident
  • Posts: 3972
    • View Profile
    • Steve’s QB64 Archive Forum
Re: Base 64 encoder / decoder
« Reply #25 on: December 18, 2020, 08:13:31 pm »
One note:

DIM b8, b7, b6, b5, b4, b3, b2, b1 AS INTEGER

You do realize only b1 is an INTEGER?  The other b's are SINGLE.
https://github.com/SteveMcNeill/Steve64 — A github collection of all things Steve!

Offline NOVARSEG

  • Forum Resident
  • Posts: 509
    • View Profile
Re: Base 64 encoder / decoder
« Reply #26 on: December 18, 2020, 09:57:47 pm »
Thanks SMcNeill
It's amazing it still worked!

Updated

Code: QB64: [Select]
  1. 'BASE 128 encoder  / decoder
  2. 'input string is encoded then decoded
  3.  
  4.  
  5. DIM string1 AS STRING 'input string
  6. DIM string2 AS STRING 'output string
  7. string1 = "ASCII stands for American Standard Code for Information Interchange..." + CHR$(166) + CHR$(1) + CHR$(168) + CHR$(169) + CHR$(170) + CHR$(171) + CHR$(172)
  8. 'string1 = "ASCII stands for American Standard Code for Information Interchange..."
  9.  
  10. 'string1 must be a multiple of 7 bytes
  11.  
  12. PRINT "string1"
  13. PRINT string1
  14. n = 65 'offset into ASCII table  starts at asc 65 "A"  to  asc 192
  15. 'no quotes, comma, :, @
  16.  
  17. 'Encode
  18. FOR x% = 1 TO LEN(string1) STEP 7
  19.  
  20.     b7 = ASC(string1, x%)
  21.     b6 = ASC(string1, x% + 1)
  22.     b5 = ASC(string1, x% + 2)
  23.     b4 = ASC(string1, x% + 3)
  24.     b3 = ASC(string1, x% + 4)
  25.     b2 = ASC(string1, x% + 5)
  26.     b1 = ASC(string1, x% + 6)
  27.  
  28.     IF b7 >= 128 THEN
  29.         b7 = b7 - 128
  30.         b8 = 64
  31.     END IF
  32.     s7 = CHR$(b7 + n)
  33.  
  34.     IF b6 >= 128 THEN
  35.         b6 = b6 - 128
  36.         b8 = b8 + 32
  37.     END IF
  38.     s6 = CHR$(b6 + n)
  39.  
  40.     IF b5 >= 128 THEN
  41.         b5 = b5 - 128
  42.         b8 = b8 + 16
  43.     END IF
  44.     s5 = CHR$(b5 + n)
  45.  
  46.     IF b4 >= 128 THEN
  47.         b4 = b4 - 128
  48.         b8 = b8 + 8
  49.     END IF
  50.     s4 = CHR$(b4 + n)
  51.  
  52.     IF b3 >= 128 THEN
  53.         b3 = b3 - 128
  54.         b8 = b8 + 4
  55.     END IF
  56.     s3 = CHR$(b3 + n)
  57.  
  58.     IF b2 >= 128 THEN
  59.         b2 = b2 - 128
  60.         b8 = b8 + 2
  61.     END IF
  62.     s2 = CHR$(b2 + n)
  63.  
  64.     IF b1 >= 128 THEN
  65.         b1 = b1 - 128
  66.         b8 = b8 + 1
  67.     END IF
  68.     s1 = CHR$(b1 + n)
  69.  
  70.     s8 = CHR$(b8 + n)
  71.  
  72.     string2 = string2 + (s8 + s7 + s6 + s5 + s4 + s3 + s2 + s1)
  73.  
  74. NEXT x%
  75.  
  76. string1 = ""
  77.  
  78. PRINT "string1 encoded"
  79. PRINT string2
  80.  
  81.  
  82. 'Decode
  83. FOR x% = 1 TO LEN(string2) STEP 8
  84.  
  85.     b8 = ASC(string2, x%) - n
  86.     b7 = ASC(string2, x% + 1) - n
  87.     b6 = ASC(string2, x% + 2) - n
  88.     b5 = ASC(string2, x% + 3) - n
  89.     b4 = ASC(string2, x% + 4) - n
  90.     b3 = ASC(string2, x% + 5) - n
  91.     b2 = ASC(string2, x% + 6) - n
  92.     b1 = ASC(string2, x% + 7) - n
  93.  
  94.     IF b8 >= 64 THEN
  95.         b8 = b8 - 64
  96.         b7 = b7 + 128
  97.     END IF
  98.     s7 = CHR$(b7)
  99.  
  100.     IF b8 >= 32 THEN
  101.         b8 = b8 - 32
  102.         b6 = b6 + 128
  103.     END IF
  104.     s6 = CHR$(b6)
  105.  
  106.     IF b8 >= 16 THEN
  107.         b8 = b8 - 16
  108.         b5 = b5 + 128
  109.     END IF
  110.     s5 = CHR$(b5)
  111.  
  112.     IF b8 >= 8 THEN
  113.         b8 = b8 - 8
  114.         b4 = b4 + 128
  115.     END IF
  116.     s4 = CHR$(b4)
  117.  
  118.     IF b8 >= 4 THEN
  119.         b8 = b8 - 4
  120.         b3 = b3 + 128
  121.     END IF
  122.     s3 = CHR$(b3)
  123.  
  124.     IF b8 >= 2 THEN
  125.         b8 = b8 - 2
  126.         b2 = b2 + 128
  127.     END IF
  128.     s2 = CHR$(b2)
  129.  
  130.     IF b8 >= 1 THEN
  131.         b8 = b8 - 1
  132.         b1 = b1 + 128
  133.     END IF
  134.     s1 = CHR$(b1)
  135.  
  136.     string1 = string1 + (s7 + s6 + s5 + s4 + s3 + s2 + s1)
  137.  
  138.  
  139. NEXT x%
  140.  
  141. PRINT "decoded"
  142. PRINT string1
  143.  
  144.  

Offline Petr

  • Forum Resident
  • Posts: 1720
  • The best code is the DNA of the hops.
    • View Profile
Re: Base 64 encoder / decoder
« Reply #27 on: December 19, 2020, 03:12:49 am »

Offline Dav

  • Forum Resident
  • Posts: 792
    • View Profile

Offline NOVARSEG

  • Forum Resident
  • Posts: 509
    • View Profile
Re: Base 64 encoder / decoder
« Reply #29 on: December 19, 2020, 10:11:41 pm »
Base 256 to base 128 decoder using _SHR , _SHL,  AND

There are no conditional statements or loops other than one FOR NEXT which is need to process either 7 or 8 bytes at a time

How it works.  7 bytes are encoded at a time.  The MSB of the 7 input bytes form an 8th byte.

Decoding left shifts the 8th byte to obtain the MSB which is  added to the original byte.
Code should run pretty fast.

Code: QB64: [Select]
  1. 'BASE 128 encoder  / decoder   using _SHR , _SHL,  AND
  2. 'input string is encoded then decoded
  3.  
  4.  
  5. DIM string1 AS STRING
  6. DIM string2 AS STRING
  7.  
  8. string1 = "ASCII stands for American Standard Code for Information Interchange..." + CHR$(127) + CHR$(128) + CHR$(129) + CHR$(130) + CHR$(1) + CHR$(2) + CHR$(1)
  9.  
  10.  
  11. 'string1 must be a multiple of 7 bytes
  12.  
  13. PRINT "string1"
  14. PRINT string1
  15.  
  16. n = 65 'offset into ASCII table  starts at asc 65 "A"  to  asc 192
  17. 'no quotes, comma, :, @
  18.  
  19. 'Encode
  20. FOR x% = 1 TO LEN(string1) STEP 7
  21.  
  22.     b7 = ASC(string1, x%)
  23.     b6 = ASC(string1, x% + 1)
  24.     b5 = ASC(string1, x% + 2)
  25.     b4 = ASC(string1, x% + 3)
  26.     b3 = ASC(string1, x% + 4)
  27.     b2 = ASC(string1, x% + 5)
  28.     b1 = ASC(string1, x% + 6)
  29.  
  30.  
  31.     b8 = (128 AND b7) + b8
  32.     b8 = _SHR(b8, 1)
  33.     b7 = (b7 AND 127) + n
  34.  
  35.     b8 = (128 AND b6) + b8
  36.     b8 = _SHR(b8, 1)
  37.     b6 = (b6 AND 127) + n
  38.  
  39.     b8 = (128 AND b5) + b8
  40.     b8 = _SHR(b8, 1)
  41.     b5 = (b5 AND 127) + n
  42.  
  43.     b8 = (128 AND b4) + b8
  44.     b8 = _SHR(b8, 1)
  45.     b4 = (b4 AND 127) + n
  46.  
  47.     b8 = (128 AND b3) + b8
  48.     b8 = _SHR(b8, 1)
  49.     b3 = (b3 AND 127) + n
  50.  
  51.     b8 = (128 AND b2) + b8
  52.     b8 = _SHR(b8, 1)
  53.     b2 = (b2 AND 127) + n
  54.  
  55.     b8 = (128 AND b1) + b8
  56.     b8 = _SHR(b8, 1)
  57.     b1 = (b1 AND 127) + n
  58.  
  59.     b8 = b8 + n
  60.  
  61.  
  62.     string2 = string2 + (CHR$(b8) + CHR$(b7) + CHR$(b6) + CHR$(b5) + CHR$(b4) + CHR$(b3) + CHR$(b2) + CHR$(b1))
  63.  
  64.  
  65. NEXT x%
  66.  
  67. string1 = ""
  68.  
  69. PRINT "string1 encoded"
  70. PRINT string2
  71.  
  72.  
  73. 'Decode
  74. FOR x% = 1 TO LEN(string2) STEP 8
  75.  
  76.     b8 = ASC(string2, x%) - n
  77.     b7 = ASC(string2, x% + 1) - n
  78.     b6 = ASC(string2, x% + 2) - n
  79.     b5 = ASC(string2, x% + 3) - n
  80.     b4 = ASC(string2, x% + 4) - n
  81.     b3 = ASC(string2, x% + 5) - n
  82.     b2 = ASC(string2, x% + 6) - n
  83.     b1 = ASC(string2, x% + 7) - n
  84.  
  85.  
  86.     b8 = _SHL(b8, 1)
  87.     b1 = (128 AND b8) + b1
  88.  
  89.     b8 = _SHL(b8, 1)
  90.     b2 = (128 AND b8) + b2
  91.  
  92.     b8 = _SHL(b8, 1)
  93.     b3 = (128 AND b8) + b3
  94.  
  95.     b8 = _SHL(b8, 1)
  96.     b4 = (128 AND b8) + b4
  97.  
  98.     b8 = _SHL(b8, 1)
  99.     b5 = (128 AND b8) + b5
  100.  
  101.     b8 = _SHL(b8, 1)
  102.     b6 = (128 AND b8) + b6
  103.  
  104.     b8 = _SHL(b8, 1)
  105.     b7 = (128 AND b8) + b7
  106.  
  107.  
  108.     string1 = string1 + (CHR$(b7) + CHR$(b6) + CHR$(b5) + CHR$(b4) + CHR$(b3) + CHR$(b2) + CHR$(b1))
  109.  
  110. NEXT x%
  111.  
  112. PRINT "decoded"
  113. PRINT string1
  114.  
  115.