How to place char* of stringZ to ubyte[]?

2012-10-29 Thread denizzzka
immutable ubyte[] valueBin = cast(immutable(ubyte[])) toStringz(s); // s is string type Error: e2ir: cannot cast toStringz(s) of type immutable(char)* to type immutable(ubyte[])

Re: How to place char* of stringZ to ubyte[]?

2012-10-29 Thread bearophile
denizzzka: immutable ubyte[] valueBin = cast(immutable(ubyte[])) toStringz(s); // s is string type Error: e2ir: cannot cast toStringz(s) of type immutable(char)* to type immutable(ubyte[]) One way to do it: import std.stdio; void main() { string s = "hello"; auto valueBin = cast(i

Re: How to place char* of stringZ to ubyte[]?

2012-10-29 Thread denizzzka
On Monday, 29 October 2012 at 17:51:56 UTC, bearophile wrote: denizzzka: immutable ubyte[] valueBin = cast(immutable(ubyte[])) toStringz(s); // s is string type Error: e2ir: cannot cast toStringz(s) of type immutable(char)* to type immutable(ubyte[]) One way to do it: import std.stdio; v

Re: How to place char* of stringZ to ubyte[]?

2012-10-29 Thread bearophile
denizzzka: I am trying to send to remote host utf8 text with zero byte at end (required by protocol) What if your UTF8 string coming from D already contains several zeros? toStringz(s) returns a pointer, so you can't cast a pointer (that doesn't know the length the buffer it points to) to

Re: How to place char* of stringZ to ubyte[]?

2012-10-29 Thread denizzzka
On Monday, 29 October 2012 at 18:50:58 UTC, bearophile wrote: denizzzka: I am trying to send to remote host utf8 text with zero byte at end (required by protocol) What if your UTF8 string coming from D already contains several zeros? Incredible situation because it is text-based protocol

Re: How to place char* of stringZ to ubyte[]?

2012-10-30 Thread Nick Sabalausky
On Mon, 29 Oct 2012 19:50:57 +0100 "bearophile" wrote: > denizzzka: > > > I am trying to send to remote host utf8 text with zero byte at > > end (required by protocol) > > What if your UTF8 string coming from D already contains several > zeros? > If you need to send a string with an embedde

Re: How to place char* of stringZ to ubyte[]?

2012-10-30 Thread bearophile
denizzzka: I am concerned about the extra allocations of temp arrays. here is it, or not? compiler optimizes it? There is one allocation, and usually the D compilers can't optimize it away. In my case it does not matter but for the future I would like to know how it can be implemented wit