digitalmars.D.learn - Byte Array Literal
- Anibal (17/17) Oct 09 2014 Hi everyone,
- bearophile (9/14) Oct 09 2014 You want ubytes (unsigned bytes) because 0x04 is 164 that is
- bearophile (4/6) Oct 09 2014 I'd like bytes to be named sbyte and ubyte in D, but Walter has
- Anibal (2/8) Oct 09 2014 Got it to work, thanks a lot!
- ketmar via Digitalmars-d-learn (9/9) Oct 09 2014 On Thu, 09 Oct 2014 15:26:52 +0000
- bearophile (9/15) Oct 09 2014 The problem is that cast. No one wants a string, most people want
- ketmar via Digitalmars-d-learn (5/7) Oct 10 2014 On Thu, 09 Oct 2014 20:08:11 +0000
Hi everyone, I'm just starting with D and i need a way to declare a byte array something like: byte[] arr = [ 0x00, 0xA4, 0x04]; This throws a int[] to byte[] cast error Tried also these ones byte[] arr = "\x00\xA4\x04"; byte[] arr = [ '\x00', '\xA4', '\x04']; byte[] arr = [ u'\x00', u'\xA4', u'\x04']; byte[] arr = [ b'\x00', b'\xA4', b'\x04']; I also tried byte[] arr = [cast(byte) 0x00, cast(byte)0xA4, cast(byte) 0x04]; and this at least compiles I read the online book and nowhere there is a byte literal mentioned. Is there another way besides casting to byte? Thanks in Advance.
Oct 09 2014
Anibal:byte[] arr = [ 0x00, 0xA4, 0x04]; This throws a int[] to byte[] cast errorYou want ubytes (unsigned bytes) because 0x04 is 164 that is bigger than byte.max. So use: ubyte[] arr = [ 0x00, 0xA4, 0x04];I also tried byte[] arr = [cast(byte) 0x00, cast(byte)0xA4, cast(byte) 0x04]; and this at least compilesGenerally in D try to mimize as much as possible the usage of cast(). Bye, bearophile
Oct 09 2014
You want ubytes (unsigned bytes) because 0x04 is 164 that is bigger than byte.max.I'd like bytes to be named sbyte and ubyte in D, but Walter has refused this. Bye, bearophile
Oct 09 2014
On Thursday, 9 October 2014 at 15:41:48 UTC, bearophile wrote:Got it to work, thanks a lot!You want ubytes (unsigned bytes) because 0x04 is 164 that is bigger than byte.max.I'd like bytes to be named sbyte and ubyte in D, but Walter has refused this. Bye, bearophile
Oct 09 2014
On Thu, 09 Oct 2014 15:26:52 +0000 Anibal via Digitalmars-d-learn <digitalmars-d-learn puremagic.com> wrote: additionally to all bearophile said, there is another interesting thing in D: special string literals for hex data. immutable ubyte[] n =3D cast(typeof(n))x"deadf00d"; or even: immutable ubyte[] n =3D cast(typeof(n))x"de ad f 0 0 d"; spaces doesn't matter, only digits do.
Oct 09 2014
ketmar:additionally to all bearophile said, there is another interesting thing in D: special string literals for hex data. immutable ubyte[] n = cast(typeof(n))x"deadf00d"; or even: immutable ubyte[] n = cast(typeof(n))x"de ad f 0 0 d"; spaces doesn't matter, only digits do.The problem is that cast. No one wants a string, most people want a ubyte[]. See: https://issues.dlang.org/show_bug.cgi?id=3850 https://issues.dlang.org/show_bug.cgi?id=5909 https://issues.dlang.org/show_bug.cgi?id=10454 Bye, bearophile
Oct 09 2014
On Thu, 09 Oct 2014 20:08:11 +0000 bearophile via Digitalmars-d-learn <digitalmars-d-learn puremagic.com> wrote:The problem is that cast. No one wants a string, most people want=20 a ubyte[].This doesn't make the cut. it's handy, people wants it, it will break almost nothing... no way.
Oct 10 2014