2 PROJECT: FlowerSoft C++ library
3 FILE : string-convert.cc
10 #include "libc-extension.hh"
12 #include "string-convert.hh"
15 a safe length for stringconversion buffers
17 worst case would be %f printing HUGE (or 1/HUGE), which is approx
18 2e318, this number would have approx 318 zero's in its string.
20 Should enlarge buff dynamically.
25 static const int STRING_BUFFER_LEN=1024;
28 String_convert::bin2hex_str (String bin_str)
31 Byte const* byte_C = bin_str.byte_C();
32 for ( int i = 0; i < bin_str.length_i(); i++) {
33 str += (char)nibble2hex_byte (*byte_C >> 4);
34 str += (char)nibble2hex_byte (*byte_C++);
40 String_convert::bin2_i (String bin_str)
42 assert (bin_str.length_i() <= 4);
45 for ( int i = 0; i < bin_str.length_i(); i++) {
47 result_i += (Byte)bin_str[ i ];
52 // breendet imp from String
54 String_convert::dec2_i (String dec_str)
56 if ( !dec_str.length_i())
60 int conv = sscanf (dec_str.ch_C(), "%ld", &l);
67 String_convert::i64_str (I64 i64, char const* fmt)
69 char buffer[STRING_BUFFER_LEN];
70 snprintf (buffer, STRING_BUFFER_LEN,
71 (fmt ? fmt : "%Ld"), i64); // assume radix 10
72 return String (buffer);
75 // breendet imp from String
77 String_convert::dec2_f (String dec_str)
79 if ( !dec_str.length_i())
82 int conv = sscanf (dec_str.ch_C(), "%lf", &d);
88 String_convert::hex2bin_i (String hex_str, String& bin_str_r)
90 if ( hex_str.length_i() % 2)
91 hex_str = "0" + hex_str;
94 Byte const* byte_C= hex_str.byte_C();
96 while ( i < hex_str.length_i()) {
97 int high_i = hex2nibble_i (*byte_C++);
98 int low_i = hex2nibble_i (*byte_C++);
99 if ( high_i < 0 || low_i < 0)
100 return 1; // illegal char
101 bin_str_r += String ((char)( high_i << 4 | low_i), 1 );
108 String_convert::hex2bin_str (String hex_str)
111 // silly, asserts should alway be "on"!
112 // assert (!hex2bin_i (hex_str, str) );
113 int error_i = hex2bin_i (hex_str, str);
119 String_convert::hex2nibble_i (Byte byte)
121 if ( byte >= '0' && byte <= '9')
123 if ( byte >= 'A' && byte <= 'F')
124 return byte - 'A' + 10;
125 if ( byte >= 'a' && byte <= 'f')
126 return byte - 'a' + 10;
130 // stupido. Should use int_str()
132 String_convert::i2dec_str (int i, int length_i, char ch)
142 return String (fill_ch, length_i - dec_str.length_i()) + dec_str;
146 // stupido. Should use int_str()
148 String_convert::u2hex_str (unsigned u, int length_i, char fill_ch)
156 str = String ((char)( ( u % 16)["0123456789abcdef"] ) ) + str;
160 str += int_str (u, "%x"); // hmm. %lx vs. %x -> portability?
163 str = String (fill_ch, length_i - str.length_i()) + str;
164 while ( ( str.length_i() > length_i) && ( str[ 0 ] == 'f' ) )
165 str = str.mid_str (2, INT_MAX);
171 String_convert::i2hex_str (int i, int length_i, char fill_ch)
173 return u2hex_str ((unsigned)i, length_i, fill_ch);
177 String_convert::nibble2hex_byte (Byte byte)
179 if ( ( byte & 0x0f) <= 9 )
180 return ( byte & 0x0f) + '0';
182 return ( byte & 0x0f) - 10 + 'a';
185 Convert an integer to a string
188 #fmt# is a printf style format, default assumes "%d" as format.
191 String_convert::int_str (int i, char const* fmt)
193 char buffer[STRING_BUFFER_LEN];
194 snprintf (buffer, STRING_BUFFER_LEN,
195 (fmt ? fmt : "%d"), i); // assume radix 10
196 return String (buffer);
200 Convert a double to a string.
202 @param #fmt# is a printf style format, default assumes "%lf" as format
205 String_convert::double_str (double f, char const* fmt)
207 char buf[STRING_BUFFER_LEN];
209 snprintf (buf, STRING_BUFFER_LEN, fmt ? fmt : "%f", f);
214 Make a string from a single character.
217 #n# is a repetition count, default value is 1
220 String_convert::char_str (char c, int n)
223 char* ch_p = new char[ n ];
225 String s ((Byte*)ch_p, n);
231 String_convert::rational_str (Rational r)
233 char * n = Itoa (r.numerator()); // LEAK????
236 if (r.denominator() != 1) {
237 char * d = Itoa (r.denominator());
238 s += String ('/') + String (d);
247 String_convert::pointer_str (void const *l)
249 char buffer[STRING_BUFFER_LEN];
250 snprintf (buffer, STRING_BUFFER_LEN, "%p", l); // assume radix 10
251 return String (buffer);