www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.learn - Where can get the Number Convert module?Thanks.

reply FrankLike <1150015857 qq.com> writes:
Hi,everyone,
     I need some help on 'Number Convert module' in D,such as 
byte[] type to  BinaryDigit.
Where can get the module?

Thanks.
Jan 13 2018
parent reply Jonathan M Davis <newsgroup.d jmdavisprog.com> writes:
On Sunday, January 14, 2018 01:45:37 FrankLike via Digitalmars-d-learn 
wrote:
 Hi,everyone,
      I need some help on 'Number Convert module' in D,such as
 byte[] type to  BinaryDigit.
 Where can get the module?

 Thanks.
Well, I'm not quite sure what you mean, but if you mean that you want to take a ubyte[] and do something like take arr[0 .. 4] and convert that to int, then you should check out https://dlang.org/phobos/std_bitmanip.html In particular, https://dlang.org/phobos/std_bitmanip.html#bigEndianToNative https://dlang.org/phobos/std_bitmanip.html#peek https://dlang.org/phobos/std_bitmanip.html#read - Jonathan M Davis
Jan 13 2018
parent reply FrankLike <1150015857 qq.com> writes:
On Sunday, 14 January 2018 at 02:03:39 UTC, Jonathan M Davis 
wrote:

 Well, I'm not quite sure what you mean, but if you mean that
Such as byte[] byteData =[0,0,0,8]; to convert, at last,get the string bit :"100".or get the BitArray. Thanks.
Jan 13 2018
next sibling parent FrankLike <1150015857 qq.com> writes:
On Sunday, 14 January 2018 at 02:41:39 UTC, FrankLike wrote:
 On Sunday, 14 January 2018 at 02:03:39 UTC, Jonathan M Davis 
 wrote:

 Well, I'm not quite sure what you mean, but if you mean that
Sorry,Such as byte[] byteData =[8,0,0,0]; to convert, at last,get the string bit :"100".or get the BitArray. Thanks.
Jan 13 2018
prev sibling parent reply Jonathan M Davis <newsgroup.d jmdavisprog.com> writes:
On Sunday, January 14, 2018 02:41:39 FrankLike via Digitalmars-d-learn 
wrote:
 On Sunday, 14 January 2018 at 02:03:39 UTC, Jonathan M Davis

 wrote:
 Well, I'm not quite sure what you mean, but if you mean that
Such as byte[] byteData =[0,0,0,8]; to convert, at last,get the string bit :"100".or get the BitArray.
I'd suggest looking at https://dlang.org/phobos/std_bitmanip.html#bitsSet and https://dlang.org/phobos/std_bitmanip.html#BitArray I'm not sure that either of them does quite what you want, but you may be able to use them to get what you want. Alternatively, you can convert to and from string using a radix with std.conv.to - e.g. assert(to!string(42, 2) == "101010"); assert(to!int("101010", 2) == 42); You have to already have convered the array of ubytes to an integral value to do that, but you can get the base-2 representation of a number that way. And one of these could be used to convert the array of ubytes to an integral: https://dlang.org/phobos/std_bitmanip.html#bigEndianToNative https://dlang.org/phobos/std_bitmanip.html#littleEndianToNative https://dlang.org/phobos/std_bitmanip.html#peek https://dlang.org/phobos/std_bitmanip.html#read The only ways in Phobos that I'm aware of to get a number in binary format as a string would be to use std.conv.to or std.conv.parse with a radix or to use toString on BitArray. So, if that's your ultimate goal, you'll need to figure out how to use one of those to get there. - Jonathan M Davis
Jan 13 2018
parent reply FrankLike <1150015857 qq.com> writes:
On Sunday, 14 January 2018 at 03:09:40 UTC, Jonathan M Davis 
wrote:
 On Sunday, January 14, 2018 02:41:39 FrankLike via 
 Digitalmars-d-learn wrote:
 [...]
I'd suggest looking at [...]
I get the result "1000" from byte[] byteData =[0,0,0,8]; Thank you very much.
Jan 13 2018
next sibling parent FrankLike <1150015857 qq.com> writes:
On Sunday, 14 January 2018 at 03:28:28 UTC, FrankLike wrote:
 On Sunday, 14 January 2018 at 03:09:40 UTC, Jonathan M Davis 
 wrote:
 On Sunday, January 14, 2018 02:41:39 FrankLike via 
 Digitalmars-d-learn wrote:
 [...]
I'd suggest looking at [...]
I get the result "1000" from ubyte[] byteData =[0,0,0,8]; Thank you very much.
Jan 13 2018
prev sibling parent reply Jonathan M Davis <newsgroup.d jmdavisprog.com> writes:
On Sunday, January 14, 2018 03:28:28 FrankLike via Digitalmars-d-learn 
wrote:
 On Sunday, 14 January 2018 at 03:09:40 UTC, Jonathan M Davis

 wrote:
 On Sunday, January 14, 2018 02:41:39 FrankLike via

 Digitalmars-d-learn wrote:
 [...]
I'd suggest looking at [...]
I get the result "1000" from byte[] byteData =[0,0,0,8]; Thank you very much.
Good to hear. On a side note, I would point out that you almost certainly want to be using ubyte and not byte. byte is signed, whereas ubyte is unsigned. So, if you want to represent bytes of memory rather than an integral value between 1 and 127, then you want ubyte. - Jonathan M Davis
Jan 13 2018
parent FrankLike <1150015857 qq.com> writes:
On Sunday, 14 January 2018 at 03:49:05 UTC, Jonathan M Davis 
wrote:

 I get the result "1000" from  byte[] byteData =[0,0,0,8];

 Thank you very much.
Good to hear. On a side note, I would point out that you almost certainly want to be using ubyte and not byte. byte is signed, whereas ubyte is unsigned. So, if you want to represent bytes of memory rather than an integral value between 1 and 127, then you want ubyte. - Jonathan M Davis
Thank you.
Jan 13 2018