Re: string to ubyte[]

2019-08-14 Thread H. S. Teoh via Digitalmars-d-learn
On Wed, Aug 14, 2019 at 03:11:44PM +, berni via Digitalmars-d-learn wrote: [...] > but unfortunately this does not work: > > >ubyte[] convert_string_pair(string first, string second) > >{ > >return 0x00 ~ first ~ 0x00 ~ second ~ 0x00; > >} > > The reason is, that this expr

string to ubyte[]

2019-08-14 Thread berni via Digitalmars-d-learn
I've got a function which takes two strings and should return them as a ubyte[] with additional zero bytes in between and around. This works: ubyte[] convert_string_pair(string first, string second) { auto b = new ubyte[](0); b ~= 0x00 ~ first ~ 0x00 ~ second ~ 0x00;

Re: string to ubyte[]

2019-08-14 Thread Adam D. Ruppe via Digitalmars-d-learn
On Wednesday, 14 August 2019 at 15:11:44 UTC, berni wrote: The reason is, that this expression creates a string and not a ubyte[]... it should be ok to just cast it in this case.

Re: Casting int[] to ubyte[]: element wise cast or slice cast

2019-02-16 Thread kdevel via Digitalmars-d-learn
On Friday, 15 February 2019 at 18:11:11 UTC, Dennis wrote: Your should report our observation: https://issues.dlang.org ``` void main () { enum A = [0x10203040, 0x50607080]; // shall enum behave like immutable? auto B = [0x10203040, 0x50607080]; assert (A == B); auto p = cast (ubyt

Re: Casting int[] to ubyte[]: element wise cast or slice cast

2019-02-15 Thread Dennis via Digitalmars-d-learn
te to int, I'm going from int to ubyte which should always be possible by multiplying the length by 4, in my case: int.sizeof * 2 == ubyte.sizeof * 8 == 8 This happens in case B of my example code, but not in case A. There the cast goes from 4*2 bytes to 1*2 instead of 1*8 and I don't get a runtime error.

Re: Casting int[] to ubyte[]: element wise cast or slice cast

2019-02-15 Thread H. S. Teoh via Digitalmars-d-learn
ment to a > ubyte: That's correct. If you want to *transcribe* a ubyte[] to an int[], what you want is probably something like this: import std.conv : to; ubyte[] data = ...; int[] result = data.map!(b => b.to!int).array; [...] > I looked at the spec and

Casting int[] to ubyte[]: element wise cast or slice cast

2019-02-15 Thread Dennis via Digitalmars-d-learn
I assumed that casting an int[] to a ubyte[] would keep all bytes and quadruple the length of the original array. But when the array is a literal, it keeps the same length but truncates every int element to a ubyte: ``` import std.stdio; void main() { // enum: enum litA = [0x10203040,

Re: Can I convert the Range returned by asUpperCase to ubyte[]?

2018-05-01 Thread Jonathan M Davis via Digitalmars-d-learn
On Tuesday, May 01, 2018 20:13:41 Dr.No via Digitalmars-d-learn wrote: > I'm trying to do an optimization here: a hash function which > expect a ubye[] array as argument, would just work if I cast > string to ubyte[] but I need to convert it to upper case, so I'd > like to d

Re: Can I convert the Range returned by asUpperCase to ubyte[]?

2018-05-01 Thread ag0aep6g via Digitalmars-d-learn
On 05/01/2018 10:13 PM, Dr.No wrote: I'm trying to do an optimization here: a hash function which expect a ubye[] array as argument, would just work if I cast string to ubyte[] but I need to convert it to upper case, so I'd like to do that lazily, so that the byte is converted to

Can I convert the Range returned by asUpperCase to ubyte[]?

2018-05-01 Thread Dr.No via Digitalmars-d-learn
I'm trying to do an optimization here: a hash function which expect a ubye[] array as argument, would just work if I cast string to ubyte[] but I need to convert it to upper case, so I'd like to do that lazily, so that the byte is converted to its upper case version soon as it's

Re: convert string to ubyte[]

2017-11-11 Thread aki via Digitalmars-d-learn
On Saturday, 11 November 2017 at 15:48:59 UTC, Mike Parker wrote: auto s = "hello"; auto bytes = s.representation; https://dlang.org/phobos/std_string.html#.representation Thank you for the replay. Now I know. aki

Re: convert string to ubyte[]

2017-11-11 Thread bauss via Digitalmars-d-learn
On Saturday, 11 November 2017 at 15:48:59 UTC, Mike Parker wrote: On Saturday, 11 November 2017 at 15:38:18 UTC, aki wrote: [...] I don't know about the error you're seeing, but the generic way to get an array of the underlying data type of a string is via std.string.representation. import

Re: convert string to ubyte[]

2017-11-11 Thread user1234 via Digitalmars-d-learn
On Saturday, 11 November 2017 at 15:48:59 UTC, Mike Parker wrote: On Saturday, 11 November 2017 at 15:38:18 UTC, aki wrote: auto bytes = cast(immutable(ubyte)[])s; Of course, if you need a mutable array you should dup: auto bytes = cast(ubyte[])s.dup; Not only "should" but he "must" otherwise

Re: convert string to ubyte[]

2017-11-11 Thread Guillaume Piolat via Digitalmars-d-learn
On Saturday, 11 November 2017 at 15:38:18 UTC, aki wrote: Hello, This will be trivial question but I cannot figure out what's wrong. I want to convert string to an array of ubyte. import std.conv; void main() { auto s = "hello"; ubyte[] b = to!(ubyte[])(s); }

Re: convert string to ubyte[]

2017-11-11 Thread Mike Parker via Digitalmars-d-learn
On Saturday, 11 November 2017 at 15:38:18 UTC, aki wrote: Hello, This will be trivial question but I cannot figure out what's wrong. I want to convert string to an array of ubyte. import std.conv; void main() { auto s = "hello"; ubyte[] b = to!(ubyte[])(s); }

convert string to ubyte[]

2017-11-11 Thread aki via Digitalmars-d-learn
Hello, This will be trivial question but I cannot figure out what's wrong. I want to convert string to an array of ubyte. import std.conv; void main() { auto s = "hello"; ubyte[] b = to!(ubyte[])(s); } It compiles but cause run time error: std.conv.ConvExceptio

Re: casting SysTime to ubyte[]

2015-01-14 Thread Laeeth Isharc via Digitalmars-d-learn
I really wouldn't advise doing that. SysTime contains a long which represents the time in hnsecs since midnight, January 1st, 1 A.D., and that could be written to a file quite easily. But it also contains a reference to a TimeZone object, so what you're doing would just be writing its address

Re: casting SysTime to ubyte[]

2015-01-12 Thread Jonathan M Davis via Digitalmars-d-learn
On Monday, January 12, 2015 13:59:27 Laeeth Isharc via Digitalmars-d-learn wrote: > import std.datetime; > import std.stdio; > import std.conv; > > void main(string[] arg) > { > auto a=Clock.currTime(); > auto b=cast(ubyte[])a; > writefln("%s",b); > } > > how do i get the time as a binary re

Re: casting SysTime to ubyte[]

2015-01-12 Thread Daniel Kozák via Digitalmars-d-learn
V Mon, 12 Jan 2015 13:59:27 + Laeeth Isharc via Digitalmars-d-learn napsáno: > import std.datetime; > import std.stdio; > import std.conv; > > void main(string[] arg) > { > auto a=Clock.currTime(); > auto b=cast(ubyte[])a; > writefln("%s",b); > } > > how do i get the time

Re: casting SysTime to ubyte[]

2015-01-12 Thread Steven Schveighoffer via Digitalmars-d-learn
On 1/12/15 8:59 AM, Laeeth Isharc wrote: import std.datetime; import std.stdio; import std.conv; void main(string[] arg) { auto a=Clock.currTime(); auto b=cast(ubyte[])a; writefln("%s",b); } how do i get the time as a binary representation I can write to a file? You can always

casting SysTime to ubyte[]

2015-01-12 Thread Laeeth Isharc via Digitalmars-d-learn
import std.datetime; import std.stdio; import std.conv; void main(string[] arg) { auto a=Clock.currTime(); auto b=cast(ubyte[])a; writefln("%s",b); } how do i get the time as a binary representation I can write to a file? Thanks.

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

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-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-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 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: 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

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: cast from void[] to ubyte[] in ctfe

2012-07-16 Thread Don Clugston
On 13/07/12 12:52, Johannes Pfau wrote: Am Fri, 13 Jul 2012 11:53:07 +0200 schrieb Don Clugston : On 13/07/12 11:16, Johannes Pfau wrote: Casting from void[] to ubyte[] is currently not allowed in CTFE. Is there a special reason for this? I don't see how this cast can be dangerous?

Re: cast from void[] to ubyte[] in ctfe

2012-07-13 Thread Johannes Pfau
Am Fri, 13 Jul 2012 11:53:07 +0200 schrieb Don Clugston : > On 13/07/12 11:16, Johannes Pfau wrote: > > Casting from void[] to ubyte[] is currently not allowed in CTFE. Is > > there a special reason for this? I don't see how this cast can be > > dangerous? > >

Re: cast from void[] to ubyte[] in ctfe

2012-07-13 Thread Don Clugston
On 13/07/12 11:16, Johannes Pfau wrote: Casting from void[] to ubyte[] is currently not allowed in CTFE. Is there a special reason for this? I don't see how this cast can be dangerous? CTFE doesn't allow ANY form of reinterpret cast, apart from signed<->unsigned. In particul

cast from void[] to ubyte[] in ctfe

2012-07-13 Thread Johannes Pfau
Casting from void[] to ubyte[] is currently not allowed in CTFE. Is there a special reason for this? I don't see how this cast can be dangerous? I need this for a function which accepts any type and passes it's binary representation to a function only accepti

Re: to!(ubyte[])("")

2012-06-22 Thread Kenji Hara
On Friday, 22 June 2012 at 09:18:38 UTC, simendsjo wrote: Bug or by design? (using dmd head) import std.conv; void main() { to!(ubyte[])(""); } std/array.d(493): Attempting to fetch the front of an empty array of immutable(char) [snip] It is design. With the conversion from

to!(ubyte[])("")

2012-06-22 Thread simendsjo
Bug or by design? (using dmd head) import std.conv; void main() { to!(ubyte[])(""); } std/array.d(493): Attempting to fetch the front of an empty array of immutable(char) to(_d_assert_msg+0x45) [0x43700d] to(@property dchar std.array.front!(immutable(char