-
StackOverflow 文件
-
D Language 教程
-
字串
-
將字串轉換為 ubyte,反之亦然
字串為不可變的 ubyte[]
string s = "unogatto";
immutable(ubyte[]) ustr = cast(immutable(ubyte)[])s;
assert(typeof(ustr).stringof == "immutable(ubyte[])");
assert(ustr.length == 8);
assert(ustr[0] == 0x75); //u
assert(ustr[1] == 0x6e); //n
assert(ustr[2] == 0x6f); //o
assert(ustr[3] == 0x67); //g
assert(ustr[7] == 0x6f); //o
字串到 ubyte[]
string s = "unogatto";
ubyte[] mustr = cast(ubyte[])s;
assert(typeof(mustr).stringof == "ubyte[]");
assert(mustr.length == 8);
assert(mustr[0] == 0x75);
assert(mustr[1] == 0x6e);
assert(mustr[2] == 0x6f);
assert(mustr[3] == 0x67);
assert(mustr[7] == 0x6f);
ubyte[]
到字串
ubyte[] stream = [ 0x75, 0x6e, 0x6f, 0x67];
string us = cast(string)stream;
assert(us == "unog");
參考