May 23, 2014 Is it a bug or I do not undestand something | ||||
|---|---|---|---|---|
| ||||
Hello everybody
I've started to use D language recently and
I have faced with such problem:
I have such method:
byte[] encodeLong(long value) {
byte buf[10] = 0;
long n = (value << 1) ^ (value >> 63);
int position = 0;
while (n & ~0x7F) {
buf[position++] = cast(byte)((n | 0x80) & 0xFF);
n >>>= 7;
}
buf[position++] = cast(byte)(n);
return buf.dup;
}
somewhere in the other module I'm doing this:
string s = "some string";
byte[] buf = encodeLong(s.length);
writeln(buf);
char[] av = cast(char[])(buf);
writeln(av);
And I get such results:
[48, 0, 0, 0, 0, 0, 0, 0, 0, 0]
0
So the questions why?
BR
Andrey
| ||||
May 23, 2014 Re: Is it a bug or I do not undestand something | ||||
|---|---|---|---|---|
| ||||
Posted in reply to Andrey Pleskach | Oops I get it ... It works correctly.
Sorry for such stupid question :)
On Friday, 23 May 2014 at 12:32:46 UTC, Andrey Pleskach wrote:
> Hello everybody
>
> I've started to use D language recently and
> I have faced with such problem:
>
> I have such method:
> byte[] encodeLong(long value) {
> byte buf[10] = 0;
> long n = (value << 1) ^ (value >> 63);
> int position = 0;
> while (n & ~0x7F) {
> buf[position++] = cast(byte)((n | 0x80) & 0xFF);
> n >>>= 7;
> }
> buf[position++] = cast(byte)(n);
> return buf.dup;
> }
>
> somewhere in the other module I'm doing this:
> string s = "some string";
> byte[] buf = encodeLong(s.length);
>
> writeln(buf);
> char[] av = cast(char[])(buf);
> writeln(av);
>
> And I get such results:
> [48, 0, 0, 0, 0, 0, 0, 0, 0, 0]
> 0
>
> So the questions why?
>
> BR
> Andrey
| |||
Copyright © 1999-2021 by the D Language Foundation
Permalink
Reply