Base64 decode data: Difference between revisions
Content added Content deleted
Drkameleon (talk | contribs) |
No edit summary |
||
Line 1,031: | Line 1,031: | ||
-- Paul R. Ehrlich |
-- Paul R. Ehrlich |
||
</pre> |
</pre> |
||
=={{header|QB64}}== |
|||
<lang vb> |
|||
Option _Explicit |
|||
Dim Shared As String Base64(0 To 63) |
|||
Dim As _Unsigned Integer x |
|||
Restore BASE64 |
|||
For x = 0 To 63 |
|||
Read Base64(x) |
|||
Next |
|||
Dim As String udata, encoded, decoded |
|||
udata = "VG8gZXJyIGlzIGh1bWFuLCBidXQgdG8gcmVhbGx5IGZvdWwgdGhpbmdzIHVwIHlvdSBuZWVkIGEgY29tcHV0ZXIuCiAgICAtLVBhdWwgUi5FaHJsaWNo" |
|||
decoded = decode(udata) |
|||
Print udata |
|||
Print decoded |
|||
BASE64: |
|||
Data "A","B","C","D","E","F","G","H","I","J","K","L","M","N","O","P","Q","R","S","T","U","V","W","X","Y","Z" |
|||
Data "a","b","c","d","e","f","g","h","i","j","k","l","m","n","o","p","q","r","s","t","u","v","w","x","y","z" |
|||
Data "0","1","2","3","4","5","6","7","8","9","+","/" |
|||
Function findIndex& (value As _Unsigned _Byte) |
|||
If Asc("A") <= value And value <= Asc("Z") Then |
|||
findIndex = value - Asc("A") |
|||
Exit Function |
|||
End If |
|||
If Asc("a") <= value And value <= Asc("z") Then |
|||
findIndex = value - Asc("a") + 26 |
|||
Exit Function |
|||
End If |
|||
If Asc("0") <= value And value <= Asc("9") Then |
|||
findIndex = value - Asc("0") + 52 |
|||
Exit Function |
|||
End If |
|||
If value = Asc("+") Then |
|||
findIndex = 62 |
|||
Exit Function |
|||
End If |
|||
If value = Asc("/") Then |
|||
findIndex = 63 |
|||
Exit Function |
|||
End If |
|||
findIndex = -1 |
|||
End Function |
|||
Function encode$ (source As String) |
|||
Dim As _Unsigned _Integer64 length: length = Len(source) |
|||
Dim As _Unsigned _Integer64 it, strend |
|||
Dim As Long acc |
|||
Dim As String sink |
|||
strend = length |
|||
While it <> strend |
|||
Dim As _Unsigned _Byte b1, b2, b3, b4 |
|||
it = it + 1 |
|||
b1 = Asc(Mid$(source, it, 1)) |
|||
sink = sink + Base64(_SHR(b1, 2)) |
|||
acc = _SHL(b1 And &H3, 4) |
|||
If it <> strend Then |
|||
it = it + 1 |
|||
b2 = Asc(Mid$(source, it, 1)) |
|||
acc = acc Or _SHR(b2, 4) |
|||
sink = sink + Base64(acc) |
|||
acc = _SHL(b2 And &HF, 2) |
|||
If it <> strend Then |
|||
it = it + 1 |
|||
b3 = Asc(Mid$(source, it, 1)) |
|||
acc = acc Or _SHR(b3, 6) |
|||
sink = sink + Base64(acc) |
|||
sink = sink + Base64(b3 And &H3F) |
|||
Else |
|||
sink = sink + Base64(acc) |
|||
sink = sink + "=" |
|||
End If |
|||
Else |
|||
sink = sink + Base64(acc) |
|||
sink = sink + "=" |
|||
sink = sink + "=" |
|||
End If |
|||
Wend |
|||
encode = sink |
|||
End Function |
|||
Function decode$ (source As String) |
|||
Dim As _Unsigned _Integer64 length: length = Len(source) |
|||
Dim As _Unsigned _Integer64 it, strend |
|||
Dim As Long acc |
|||
Dim As String sink |
|||
strend = length |
|||
While it <> strend |
|||
Dim As _Unsigned _Byte b1, b2, b3, b4 |
|||
it = it + 1 |
|||
b1 = Asc(Mid$(source, it, 1)) |
|||
it = it + 1 |
|||
b2 = Asc(Mid$(source, it, 1)) |
|||
it = it + 1 |
|||
b3 = Asc(Mid$(source, it, 1)) |
|||
it = it + 1 |
|||
b4 = Asc(Mid$(source, it, 1)) |
|||
Dim As Long i1, i2 |
|||
i1 = findIndex(b1) |
|||
i2 = findIndex(b2) |
|||
acc = _SHL(i1, 2) |
|||
acc = acc Or _SHR(i2, 4) |
|||
sink = sink + Chr$(acc) |
|||
If b3 <> Asc("=") Then |
|||
Dim As Long i3 |
|||
i3 = findIndex(b3) |
|||
acc = _SHL(i2 And &HF, 4) |
|||
acc = acc Or _SHR(i3, 2) |
|||
sink = sink + Chr$(acc) |
|||
If b4 <> Asc("=") Then |
|||
Dim As Long i4 |
|||
i4 = findIndex(b4) |
|||
acc = _SHL(i3 And &H3, 6) |
|||
acc = acc Or i4 |
|||
sink = sink + Chr$(acc) |
|||
End If |
|||
End If |
|||
Wend |
|||
decode = sink |
|||
End Function |
|||
</lang> |
|||
{{out}} |
|||
<pre> |
|||
VG8gZXJyIGlzIGh1bWFuLCBidXQgdG8gcmVhbGx5IGZvdWwgdGhpbmdzIHVwIHlvdSBuZWVkIGEgY29tcHV0ZXIuCiAgICAtLVBhdWwgUi5FaHJsaWNo |
|||
To err is human, but to really foul things up you need a computer. |
|||
--Paul R.Ehrlich |
|||
</pre> |
|||
=={{header|Raku}}== |
=={{header|Raku}}== |
||
(formerly Perl 6) |
(formerly Perl 6) |