// Base64 turns bytes into text and back again.
import std.io.println;
import std.text.from_bytes;
import std.encoding.base64;
def main(): int {
let message: vector u8 = bytes_from_text("SultanC");
let encoded: string = base64_encode(&message);
println(clone(&encoded));
match base64_decode(&encoded) with
| result.ok(decoded) -> { println(from_bytes(&decoded)); }
| result.err(_) -> { println("invalid base64"); return 1; };
let broken: string = "not*base64";
match base64_decode(&broken) with
| result.ok(_) -> { println("unexpected"); }
| result.err(_) -> { println("rejected invalid input"); };
return 0;
}// قاعدة64 تحول البايتات إلى نص ثم تعيدها.
استورد std.io.println;
استورد std.text.from_bytes;
استورد std.encoding.base64;
عرف الرئيسية(): عدد {
دع الرسالة: متجه ط8 = بايتات_من_نص("SultanC");
دع المرمز: نص = رمز_قاعدة64(&الرسالة);
اطبع_سطر(استنسخ(&المرمز));
طابق فك_قاعدة64(&المرمز) مع
| نتيجة.نجح(المفكوك) -> { اطبع_سطر(من_بايتات(&المفكوك)); }
| نتيجة.خطأ(_) -> { اطبع_سطر("قاعدة64 غير صالحة"); أرجع 1; };
دع المعطوب: نص = "not*base64";
طابق فك_قاعدة64(&المعطوب) مع
| نتيجة.نجح(_) -> { اطبع_سطر("غير متوقع"); }
| نتيجة.خطأ(_) -> { اطبع_سطر("رُفض المدخل غير الصالح"); };
أرجع 0;
}
Run it
With the interpreter, without compiling:
./build/sultanc run examples/en/encoding/base64.snOr compile it to an executable:
./build/sultanc examples/en/encoding/base64.sn -o build/example && ./build/example