Thread overview | |||||||||
---|---|---|---|---|---|---|---|---|---|
|
October 09, 2014 Byte Array Literal | ||||
---|---|---|---|---|
| ||||
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. |
October 09, 2014 Re: Byte Array Literal | ||||
---|---|---|---|---|
| ||||
Posted in reply to Anibal | 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 |
October 09, 2014 Re: Byte Array Literal | ||||
---|---|---|---|---|
| ||||
Posted in reply to bearophile | > 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
|
October 09, 2014 Re: Byte Array Literal | ||||
---|---|---|---|---|
| ||||
Posted in reply to bearophile | 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!
|
October 09, 2014 Re: Byte Array Literal | ||||
---|---|---|---|---|
| ||||
Posted in reply to Anibal Attachments: | 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 = 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. |
October 09, 2014 Re: Byte Array Literal | ||||
---|---|---|---|---|
| ||||
Posted in reply to ketmar | 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 |
October 10, 2014 Re: Byte Array Literal | ||||
---|---|---|---|---|
| ||||
Posted in reply to bearophile Attachments: | 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 a ubyte[]. This doesn't make the cut. it's handy, people wants it, it will break almost nothing... no way. |
Copyright © 1999-2021 by the D Language Foundation