www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.learn - string to byte array

reply rickdiaz <ricky_d gmail.com> writes:
how do i convert a string of chars to byte array

will this work: 

ubyte[] b;
char[] s="test";
b = cast(ubyte[]) s;

thx in advance
Nov 04 2008
next sibling parent BCS <ao pathlink.com> writes:
Reply to rickdiaz,

 how do i convert a string of chars to byte array
 
 will this work:
 
 ubyte[] b;
 char[] s="test";
 b = cast(ubyte[]) s;
 thx in advance
 

yes
Nov 04 2008
prev sibling parent reply BCS <ao pathlink.com> writes:
Reply to rickdiaz,

 how do i convert a string of chars to byte array
 
 will this work:
 
 ubyte[] b;
 char[] s="test";
 b = cast(ubyte[]) s;
 thx in advance
 

note: char[] is utf-8 so that might not do exactly what you expect if you are not careful about what you expect.
Nov 04 2008
parent reply james <james gmail.com> writes:
BCS Wrote:

 Reply to rickdiaz,
 
 how do i convert a string of chars to byte array
 
 will this work:
 
 ubyte[] b;
 char[] s="test";
 b = cast(ubyte[]) s;
 thx in advance
 

note: char[] is utf-8 so that might not do exactly what you expect if you are not careful about what you expect.

i wonder how you turn uint into ubyte array?, because above method wont work
Nov 04 2008
parent reply "Steven Schveighoffer" <schveiguy yahoo.com> writes:
"james" wrote
 BCS Wrote:

 Reply to rickdiaz,

 how do i convert a string of chars to byte array

 will this work:

 ubyte[] b;
 char[] s="test";
 b = cast(ubyte[]) s;
 thx in advance

note: char[] is utf-8 so that might not do exactly what you expect if you are not careful about what you expect.

i wonder how you turn uint into ubyte array?, because above method wont work

uint i; auto ubarray = (cast(ubyte *)&i)[0..i.sizeof]; But be careful, you have to account for endianness. -Steve
Nov 04 2008
parent james <james gmail.com> writes:
Steven Schveighoffer Wrote:

 "james" wrote
 BCS Wrote:

 Reply to rickdiaz,

 how do i convert a string of chars to byte array

 will this work:

 ubyte[] b;
 char[] s="test";
 b = cast(ubyte[]) s;
 thx in advance

note: char[] is utf-8 so that might not do exactly what you expect if you are not careful about what you expect.

i wonder how you turn uint into ubyte array?, because above method wont work

uint i; auto ubarray = (cast(ubyte *)&i)[0..i.sizeof]; But be careful, you have to account for endianness. -Steve

Nov 04 2008