YASQ - Proper way to convert byte[] <--> string

Steve Teale steve.teale at britseyeview.com
Thu Jul 12 09:02:34 PDT 2007


Frits van Bommel Wrote:

> Steve Teale wrote:
> > Frits van Bommel Wrote:
> > 
> >> ---
> >> A[n .. n+s.length] = cast(byte[]) s;
> >> ---
> > 
> > Can I use n+s.length?  In my experimentation i noticed that a UTF8 string containing a character using a two-byte representation definitely had an s.length of the number of characters, which was one less than the number of bytes.
> 
> You noticed wrong...
> char[]s in D aren't very special, they're just specific array types that 
> happen to be handled specially by some functions (such as writef*)[1]. 
> The .length is the number of elements, and each element is a fixed size. 
> A char is just a type representing a byte from UTF-8 text.
> ---
> import std.stdio;
> 
> void main() {
> 	auto s = "\u0100";
> 	writefln(s);
> 	writefln(s.length);
> 	writefln((cast(byte[])s).length);
> }
> ---
> Outputs a weird character (an A with a - on top) and two times the number 2.
> 
> 
> [1]: and by foreach statements as well; they can automagically extract 
> char/wchar/dchar elements from char[]/wchar[]dchar[], in any combination.

You are correct, I had misinterpreted my own test program.




More information about the Digitalmars-d mailing list