BASE32

 投稿者:しばっち  投稿日:2020年10月 3日(土)19時41分54秒
  BASE32では5バイト40bitを5bitずつ8つに分け32進文字列に変換します。
文字数が5の倍数ではない時"="で埋めます。
5バイトを8文字に変換するので増大率は8/5倍になります。

https://en.wikipedia.org/wiki/Base32

OPTION CHARACTER BYTE
DO
   READ IF MISSING THEN EXIT DO:A$
   LET S$=ENCODEBASE32$(A$)
   PRINT "原文  :";A$
   PRINT "ENCODE:";S$
   PRINT "DECODE:";DECODEBASE32$(S$)
   PRINT
LOOP
DATA A
DATA AB
DATA ABC
DATA ABCD
DATA ABCDE
DATA ABCDEFGHIJKLMNOPQRSTUVWXYZ
DATA 0123456789
DATA 十進BASIC
END

EXTERNAL  FUNCTION ENCODEBASE32$(A$)
OPTION CHARACTER BYTE
LET S$="ABCDEFGHIJKLMNOPQRSTUVWXYZ234567"
LET L=MOD(LEN(A$),5)
FOR I=0 TO INT(LEN(A$)/5)-1
   LET D$=A$(5*I+1:5*I+5)
   LET N=ORD(D$(1:1))*256^4+ORD(D$(2:2))*256^3+ORD(D$(3:3))*256^2+ORD(D$(4:4))*256+ORD(D$(5:5))
   LET N1=MOD(INT(N/32^7),32)+1
   LET N2=MOD(INT(N/32^6),32)+1
   LET N3=MOD(INT(N/32^5),32)+1
   LET N4=MOD(INT(N/32^4),32)+1
   LET N5=MOD(INT(N/32^3),32)+1
   LET N6=MOD(INT(N/32^2),32)+1
   LET N7=MOD(INT(N/32),32)+1
   LET N8=MOD(N,32)+1
   LET ENC$=ENC$&S$(N1:N1)&S$(N2:N2)&S$(N3:N3)&S$(N4:N4)&S$(N5:N5)&S$(N6:N6)&S$(N7:N7)&S$(N8:N8)
NEXT I
LET D$=A$(5*I+1:LEN(A$))
SELECT CASE L
CASE 0
CASE 4
!'4byte 32bit 12345  67812  34567  81234  56781  23456  78000  8倍して5bitずつ*7つ
   LET N=ORD(D$(1:1))*256^3+ORD(D$(2:2))*256^2+ORD(D$(3:3))*256+ORD(D$(4:4))
   LET N=N*8
   LET N1=MOD(INT(N/32^6),32)+1
   LET N2=MOD(INT(N/32^5),32)+1
   LET N3=MOD(INT(N/32^4),32)+1
   LET N4=MOD(INT(N/32^3),32)+1
   LET N5=MOD(INT(N/32^2),32)+1
   LET N6=MOD(INT(N/32),32)+1
   LET N7=MOD(N,32)+1
   LET ENC$=ENC$&S$(N1:N1)&S$(N2:N2)&S$(N3:N3)&S$(N4:N4)&S$(N5:N5)&S$(N6:N6)&S$(N7:N7)&"="
CASE 3
!'3byte 24bit 12345  67812  34567  81234  56780  2倍して5bitずつ*5つ
   LET N=ORD(D$(1:1))*256^2+ORD(D$(2:2))*256+ORD(D$(3:3))
   LET N=N*2
   LET N1=MOD(INT(N/32^4),32)+1
   LET N2=MOD(INT(N/32^3),32)+1
   LET N3=MOD(INT(N/32^2),32)+1
   LET N4=MOD(INT(N/32),32)+1
   LET N5=MOD(N,32)+1
   LET ENC$=ENC$&S$(N1:N1)&S$(N2:N2)&S$(N3:N3)&S$(N4:N4)&S$(N5:N5)&"==="
CASE 2
!'2byte 16bit 12345  67812  34567  80000  16倍して5bitずつ*4つ
   LET N=ORD(D$(1:1))*256+ORD(D$(2:2))
   LET N=N*16
   LET N1=MOD(INT(N/32^3),32)+1
   LET N2=MOD(INT(N/32^2),32)+1
   LET N3=MOD(INT(N/32),32)+1
   LET N4=MOD(N,32)+1
   LET ENC$=ENC$&S$(N1:N1)&S$(N2:N2)&S$(N3:N3)&S$(N4:N4)&"===="
CASE 1
!'1byte 8bit 12345  67800  4倍して5bitずつ*2つ
   LET N=ORD(D$)
   LET N=N*4
   LET N1=MOD(INT(N/32),32)+1
   LET N2=MOD(N,32)+1
   LET ENC$=ENC$&S$(N1:N1)&S$(N2:N2)&"======"
END SELECT
LET ENCODEBASE32$=ENC$
END FUNCTION

EXTERNAL  FUNCTION DECODEBASE32$(M$)
OPTION CHARACTER BYTE
LET A$="ABCDEFGHIJKLMNOPQRSTUVWXYZ234567"
FOR I=0 TO LEN(M$)/8-1
   LET L$=M$(8*I+1:8*I+8)
   IF RIGHT$(L$,6)="======" THEN
      LET N1=POS(A$,L$(1:1))-1
      LET N2=POS(A$,L$(2:2))-1
      LET N=N1*32+N2
      LET N=N/4
      LET DEC$=DEC$&CHR$(MOD(N,256))
   ELSEIF RIGHT$(L$,4)="====" THEN
      LET N1=POS(A$,L$(1:1))-1
      LET N2=POS(A$,L$(2:2))-1
      LET N3=POS(A$,L$(3:3))-1
      LET N4=POS(A$,L$(4:4))-1
      LET N=N1*32^3+N2*32^2+N3*32+N4
      LET N=N/16
      LET DEC$=DEC$&CHR$(MOD(INT(N/256),256))&CHR$(MOD(N,256))
   ELSEIF RIGHT$(L$,3)="===" THEN
      LET N1=POS(A$,L$(1:1))-1
      LET N2=POS(A$,L$(2:2))-1
      LET N3=POS(A$,L$(3:3))-1
      LET N4=POS(A$,L$(4:4))-1
      LET N5=POS(A$,L$(5:5))-1
      LET N=N1*32^4+N2*32^3+N3*32^2+N4*32+N5
      LET N=N/2
      LET DEC$=DEC$&CHR$(MOD(INT(N/256^2),256))&CHR$(MOD(INT(N/256),256))&CHR$(MOD(N,256))
   ELSEIF RIGHT$(L$,1)="=" THEN
      LET N1=POS(A$,L$(1:1))-1
      LET N2=POS(A$,L$(2:2))-1
      LET N3=POS(A$,L$(3:3))-1
      LET N4=POS(A$,L$(4:4))-1
      LET N5=POS(A$,L$(5:5))-1
      LET N6=POS(A$,L$(6:6))-1
      LET N7=POS(A$,L$(7:7))-1
      LET N=N1*32^6+N2*32^5+N3*32^4+N4*32^3+N5*32^2+N6*32+N7
      LET N=N/8
      LET DEC$=DEC$&CHR$(MOD(INT(N/256^3),256))&CHR$(MOD(INT(N/256^2),256))&CHR$(MOD(INT(N/256),256))&CHR$(MOD(N,256))
   ELSE
      LET N1=POS(A$,L$(1:1))-1
      LET N2=POS(A$,L$(2:2))-1
      LET N3=POS(A$,L$(3:3))-1
      LET N4=POS(A$,L$(4:4))-1
      LET N5=POS(A$,L$(5:5))-1
      LET N6=POS(A$,L$(6:6))-1
      LET N7=POS(A$,L$(7:7))-1
      LET N8=POS(A$,L$(8:8))-1
      LET N=N1*32^7+N2*32^6+N3*32^5+N4*32^4+N5*32^3+N6*32^2+N7*32+N8
      LET DEC$=DEC$&CHR$(MOD(INT(N/256^4),256))&CHR$(MOD(INT(N/256^3),256))&CHR$(MOD(INT(N/256^2),256))&CHR$(MOD(INT(N/256),256))&CHR$(MOD(N,256))
   END IF
NEXT I
LET DECODEBASE32$=DEC$
END FUNCTION
 

戻る