www.digitalmars.com         C & C++   DMDScript  

digitalmars.D - Hexadecimal litterals not compatible with numeric types

reply "TechnoZeus" <TechnoZeus PeoplePC.com> writes:
Okay, I'm new here so please take it easy on me if this sounds like a stupid
question.

Why does the compilation of...

int testint = x"ff ff";
assert(testint==65535);

(inside of an otherwise working program) cause an error like...

F:\DMD\PRACTICE\GNPFU0C.D(262): cannot implicitly convert expression
("\u00ff\u00ff") of type char[2] to int

? I even tried casting it as...

int testint = cast(int)x"ff ff";
assert(testint==65535);

but then I got...

F:\DMD\PRACTICE\GNPFU0C.D(262): e2ir: cannot cast from char[2] to int

. Shouldn't a 4 digit hexadecimal litteral be compatible with the int variable
type?

I would think an implicit cast should be expected, since a hexadecimal litteral
represents a numeric value.

TZ
Apr 13 2005
parent reply Thomas Kuehne <thomas-dloop kuehne.thisisspam.cn> writes:
-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1

TechnoZeus schrieb am Wed, 13 Apr 2005 02:12:18 -0500:
 Okay, I'm new here so please take it easy on me if this sounds like a stupid
question.

 Why does the compilation of...

 int testint = x"ff ff";
 assert(testint==65535);

int testint = 0xffff; // or 0xFFFFF or 0xFF_FF or 0xff_ff assert(testint==65535); Thomas -----BEGIN PGP SIGNATURE----- iD8DBQFCXM9c3w+/yD4P9tIRAh8WAJwKfYuVkMKXkt6wJVFU+qkK4WrJUQCeNs/O CuXiiEHbaYNCML90ZMZLxuk= =d0MG -----END PGP SIGNATURE-----
Apr 13 2005
parent reply "TechnoZeus" <TechnoZeus PeoplePC.com> writes:
Right, but I'm taling about X strings, as described in
http://digitalmars.com/d/sdwest/paper.html as follows...

<quote>

X strings
Hex dumps often come in the form of:
  00 0A E3 DC 86 73 7E 7E

Putting them into a form acceptable to C:
  0x00, 0x0A, 0xE3, 0xDC, 0x86, 0x73, 0x7E, 0x7E,

or:
  "\x00\x0A\xE3\xDC\x86\x73\x7E\x7E"

This can get tedious and error prone when there's a lot of it. D has the x
string, where hex data can be simply wrapped with double quotes, leaving the
whitespace intact:
  x"00 0A E3 DC 86 73 7E 7E"

 </quote>

It seemed like a good idea, but I'm wondering why it doesn't work as I had
expected it to.

TechnoZeus

"Thomas Kuehne" <thomas-dloop kuehne.thisisspam.cn> wrote in message
news:tjvui2-60e.ln1 lnews.kuehne.cn...
 -----BEGIN PGP SIGNED MESSAGE-----
 Hash: SHA1

 TechnoZeus schrieb am Wed, 13 Apr 2005 02:12:18 -0500:
 Okay, I'm new here so please take it easy on me if this sounds like a stupid
question.

 Why does the compilation of...

 int testint = x"ff ff";
 assert(testint==65535);

int testint = 0xffff; // or 0xFFFFF or 0xFF_FF or 0xff_ff assert(testint==65535); Thomas -----BEGIN PGP SIGNATURE----- iD8DBQFCXM9c3w+/yD4P9tIRAh8WAJwKfYuVkMKXkt6wJVFU+qkK4WrJUQCeNs/O CuXiiEHbaYNCML90ZMZLxuk= =d0MG -----END PGP SIGNATURE-----

Apr 13 2005
parent reply =?ISO-8859-1?Q?Anders_F_Bj=F6rklund?= <afb algonet.se> writes:
TechnoZeus wrote:

 Right, but I'm taling about X strings, as described in
 http://digitalmars.com/d/sdwest/paper.html as follows...
 
 It seemed like a good idea, but I'm wondering why it doesn't work as I had
expected it to.

Seems like you got your strings and integers mixed up ? import std.stdio; void main() { writef(x"68 65 6c 6c 6f 0a"); } --anders
Apr 13 2005
parent "TechnoZeus" <TechnoZeus PeoplePC.com> writes:
Ah, okay.  Nope... I didn't get them mixed up.  I just misinterpreted the
intended usage.  Thanks for the clarification, and the example.  :)

TZ

"Anders F Björklund" <afb algonet.se> wrote in message
news:d3inem$18ld$1 digitaldaemon.com...
 TechnoZeus wrote:

 Right, but I'm taling about X strings, as described in
 http://digitalmars.com/d/sdwest/paper.html as follows...

 It seemed like a good idea, but I'm wondering why it doesn't work as I had
expected it to.

Seems like you got your strings and integers mixed up ? import std.stdio; void main() { writef(x"68 65 6c 6c 6f 0a"); } --anders

Apr 13 2005