Thread overview | |||||||||
---|---|---|---|---|---|---|---|---|---|
|
June 01, 2015 convert base | ||||
---|---|---|---|---|
| ||||
How could I convert a number form binary to an arbitrary base like 19 or 23? |
June 01, 2015 Re: convert base | ||||
---|---|---|---|---|
| ||||
Posted in reply to Hugo | On 6/1/15 3:43 PM, Hugo wrote:
> How could I convert a number form binary to an arbitrary base like 19 or
> 23?
import std.conv;
to!(string)(100, 19); // "55"
-Steve
|
June 01, 2015 Re: convert base | ||||
---|---|---|---|---|
| ||||
Posted in reply to Steven Schveighoffer | On Monday, 1 June 2015 at 19:53:50 UTC, Steven Schveighoffer wrote:
> On 6/1/15 3:43 PM, Hugo wrote:
>> How could I convert a number form binary to an arbitrary base like 19 or
>> 23?
>
> import std.conv;
>
> to!(string)(100, 19); // "55"
Thanks! Is there a way to specify a source base different than 10?
And by the way, this method does not seem to work for bases higher than 36, how could one achieve for example a conversion to a base-60?
|
June 02, 2015 Re: convert base | ||||
---|---|---|---|---|
| ||||
Posted in reply to Hugo | On 6/1/15 7:16 PM, Hugo wrote: > On Monday, 1 June 2015 at 19:53:50 UTC, Steven Schveighoffer wrote: >> On 6/1/15 3:43 PM, Hugo wrote: >>> How could I convert a number form binary to an arbitrary base like 19 or >>> 23? >> >> import std.conv; >> >> to!(string)(100, 19); // "55" > > Thanks! Is there a way to specify a source base different than 10? A "source base"? the source base is always binary :) If you want to go between base string representation, there is parse for going from string to binary. > And by the way, this method does not seem to work for bases higher than > 36, how could one achieve for example a conversion to a base-60? 35 in base-36 is Z. What is 36 in base-37? At some point you run out of alphabet. -Steve |
June 02, 2015 Re: convert base | ||||
---|---|---|---|---|
| ||||
Posted in reply to Steven Schveighoffer | On Tuesday, 2 June 2015 at 00:00:43 UTC, Steven Schveighoffer wrote: > On 6/1/15 7:16 PM, Hugo wrote: >> Thanks! Is there a way to specify a source base different than 10? > > A "source base"? the source base is always binary :) > > If you want to go between base string representation, there is parse for going from string to binary. What I meant was for example being able to pass from console as an argument the number to convert lets say in hexadecimal, octal or binary representation. I will check parse though, thanks. >> And by the way, this method does not seem to work for bases higher than >> 36, how could one achieve for example a conversion to a base-60? > > 35 in base-36 is Z. What is 36 in base-37? At some point you run out of alphabet. Well.. that depends on what you accept as valid characters, doesn't it? Base-64 is a good example. |
June 02, 2015 Re: convert base | ||||
---|---|---|---|---|
| ||||
Posted in reply to Hugo | On 6/1/15 8:36 PM, Hugo wrote: > On Tuesday, 2 June 2015 at 00:00:43 UTC, Steven Schveighoffer wrote: >> On 6/1/15 7:16 PM, Hugo wrote: >>> Thanks! Is there a way to specify a source base different than 10? >> >> A "source base"? the source base is always binary :) >> >> If you want to go between base string representation, there is parse >> for going from string to binary. > > What I meant was for example being able to pass from console as an > argument the number to convert lets say in hexadecimal, octal or binary > representation. > > I will check parse though, thanks. Yes, use std.conv.parse: auto binRepresentation = parse!long("1a2b", 16); // read hex > >>> And by the way, this method does not seem to work for bases higher than >>> 36, how could one achieve for example a conversion to a base-60? >> >> 35 in base-36 is Z. What is 36 in base-37? At some point you run out >> of alphabet. > > Well.. that depends on what you accept as valid characters, doesn't it? > Base-64 is a good example. This means 'A' and 'a' have 2 different values. I don't think a general function such as to with radix is good for this. You can probably do better with a custom function, I'm not sure if there's any base-64 libraries out there. -Steve |
June 02, 2015 Re: convert base | ||||
---|---|---|---|---|
| ||||
Posted in reply to Steven Schveighoffer Attachments: | On Mon, 01 Jun 2015 21:43:56 -0400, Steven Schveighoffer wrote:
> I don't think a general function such as to with radix is good for this. You can probably do better with a custom function, I'm not sure if there's any base-64 libraries out there.
std.base64? ;-)
|
Copyright © 1999-2021 by the D Language Foundation