www.digitalmars.com         C & C++   DMDScript  

digitalmars.D - base/radix parameter in conv.d

reply ano <ano_member pathlink.com> writes:
hey,

in conv.d (phobos/std) there are some routines for string conversion (toInt(),
toLong(),..). am i missing something or are there no functions with a 'radix'
parameter for e.g. 16 == hex, 8 == octal, ... 

right now i'm using "extern (C) strtoul()" wrapper to read hexadecimal color
codes from some file but i assumed such things to be present in the D standard
library.

what do you think?
Oct 05 2004
next sibling parent reply Sjoerd van Leent <svanleent wanadoo.nl> writes:
ano wrote:
 hey,
 
 in conv.d (phobos/std) there are some routines for string conversion (toInt(),
 toLong(),..). am i missing something or are there no functions with a 'radix'
 parameter for e.g. 16 == hex, 8 == octal, ... 
 
 right now i'm using "extern (C) strtoul()" wrapper to read hexadecimal color
 codes from some file but i assumed such things to be present in the D standard
 library.
 
 what do you think?
 
 
Can't you use std.format.doFormat(...), this should be able to do just that. Regards, Sjoerd
Oct 05 2004
parent reply Burton Radons <burton-radons smocky.com> writes:
Sjoerd van Leent wrote:

 ano wrote:
 
 in conv.d (phobos/std) there are some routines for string conversion 
 (toInt(),
 toLong(),..). am i missing something or are there no functions with a 
 'radix'
 parameter for e.g. 16 == hex, 8 == octal, ...
 right now i'm using "extern (C) strtoul()" wrapper to read hexadecimal 
 color
 codes from some file but i assumed such things to be present in the D 
 standard
 library.

 what do you think?
Can't you use std.format.doFormat(...), this should be able to do just that.
He wants to go in the other direction.
Oct 05 2004
parent reply Sjoerd van Leent <svanleent wanadoo.nl> writes:
Burton Radons wrote:
 Sjoerd van Leent wrote:
 
 ano wrote:

 in conv.d (phobos/std) there are some routines for string conversion 
 (toInt(),
 toLong(),..). am i missing something or are there no functions with a 
 'radix'
 parameter for e.g. 16 == hex, 8 == octal, ...
 right now i'm using "extern (C) strtoul()" wrapper to read 
 hexadecimal color
 codes from some file but i assumed such things to be present in the D 
 standard
 library.

 what do you think?
Can't you use std.format.doFormat(...), this should be able to do just that.
He wants to go in the other direction.
My fault, I wasn't paying attention. I don't use the standard C functions very much (I normally use the C++ stream library for that purpose). Regards, Sjoerd
Oct 05 2004
parent ano <ano_member pathlink.com> writes:
In article <cjv46l$tt9$1 digitaldaemon.com>, Sjoerd van Leent says...
 Sjoerd van Leent wrote:
 
My fault, I wasn't paying attention. I don't use the standard C functions very much (I normally use the C++ stream library for that purpose). Regards, Sjoerd
thanks all, i'll have more thorough look into that soon. there're a gazillion ways to convert strings in C/C++/D ...
Oct 06 2004
prev sibling parent reply Burton Radons <burton-radons smocky.com> writes:
ano wrote:

 in conv.d (phobos/std) there are some routines for string conversion (toInt(),
 toLong(),..). am i missing something or are there no functions with a 'radix'
 parameter for e.g. 16 == hex, 8 == octal, ... 
std.conv is very limited, to the point where I haven't found any utility in it. I've attached a quick and dirty module I've hacked together for string-to-number conversion; I just wrote it this moment so yeah. Here's the docs from the top of the file: void string_to ( {char,wchar,dchar} [] string, out {int,uint,long,ulong,float,double,real} value, type_flags flags = default_flags, out size_t end_offset = null); These functions convert a string into an integer or floating-point extras. You can also configure it to only handle a subset of the functionality through the flags parameter. These are the parameters: string is the string to convert; it can be a UTF-8, UTF-16, or UTF-32 string. value is the value to output to; it can be of type int, uint, long, ulong, float, double, or real. flags, if provided, sets the subset of numbers that can be converted. This will be detailed later. To act like various languages, you can use c_flags_int, c_flags_float, d_flags_int, d_flags_float, csharp_flags_int, or csharp_flags_float. end_offset, if provided, is stored with the array index that processing terminated on. This can throw a set of errors: class_error_overflow is thrown if an overflow is detected when reading numbers. class_error_no_number_found is thrown if processing terminates without any digits read. Here are the string formats which can be parsed: " 45" (unless if flags.whitespace is false) "-4", "+8" (unless if flags.sign is false) "0x8F", "0X5" (unless if flags.hex is false) "8Fh", "AH" (unless if flags.hex_suffix is false) "0770" (unless if flags.oct is false) "0B1011", "0b1111" (unless if flags.bin is false) "1_004" (unless if flags.underlines is false) "45e4" (float only - unless if flags.exp is false) "16^-8" (float only - unless if flags.exp_hexbin is false) flags.exp_hexbin is crazy stuff for D. Maybe it's normal to work that way, I don't know. flags.radix can be used to set the default radix if none others are provided.
Oct 05 2004
parent Burton Radons <burton-radons smocky.com> writes:
Oops, renamed wstring_to and dstring_to to string_to; I had them that 
way because I was passing literals to string_to which DMD can't 
disambiguate.
Oct 05 2004