www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.learn - Byte Array Literal

reply "Anibal" <anibal.palma toc.cl> writes:
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
next sibling parent reply "bearophile" <bearophileHUGS lycos.com> writes:
Anibal:

 byte[] arr = [ 0x00, 0xA4, 0x04];

 This throws a int[] to byte[] cast error
You 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 compiles
Generally in D try to mimize as much as possible the usage of cast(). Bye, bearophile
Oct 09 2014
parent reply "bearophile" <bearophileHUGS lycos.com> writes:
 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
parent "Anibal" <anibal.palma toc.cl> writes:
On Thursday, 9 October 2014 at 15:41:48 UTC, bearophile wrote:
 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
Got it to work, thanks a lot!
Oct 09 2014
prev sibling parent reply ketmar via Digitalmars-d-learn <digitalmars-d-learn puremagic.com> writes:
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
parent reply "bearophile" <bearophileHUGS lycos.com> writes:
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
parent ketmar via Digitalmars-d-learn <digitalmars-d-learn puremagic.com> writes:
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