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.
24 static const int STRING_BUFFER_LEN=1024;
27 String_convert::bin2hex_str( String bin_str )
30 Byte const* byte_c_l = bin_str.byte_c_l();
31 for ( int i = 0; i < bin_str.length_i(); i++ ) {
32 str += (char)nibble2hex_byte( *byte_c_l >> 4 );
33 str += (char)nibble2hex_byte( *byte_c_l++ );
39 String_convert::bin2_i( String bin_str )
41 assert( bin_str.length_i() <= 4 );
44 for ( int i = 0; i < bin_str.length_i(); i++ ) {
46 result_i += (Byte)bin_str[ i ];
51 // breendet imp from String
53 String_convert::dec2_i( String dec_str )
55 if ( !dec_str.length_i() )
59 int conv = sscanf( dec_str.ch_c_l(), "%ld", &l );
66 String_convert::i64_str( I64 i64, char const* fmt)
68 char buffer[STRING_BUFFER_LEN];
69 snprintf(buffer, STRING_BUFFER_LEN,
70 (fmt ? fmt : "%Ld"), i64 ); // assume radix 10
71 return String(buffer);
74 // breendet imp from String
76 String_convert::dec2_f( String dec_str )
78 if ( !dec_str.length_i() )
81 int conv = sscanf( dec_str.ch_c_l(), "%lf", &d );
87 String_convert::hex2bin_i( String hex_str, String& bin_str_r )
89 if ( hex_str.length_i() % 2 )
90 hex_str = "0" + hex_str;
93 Byte const* byte_c_l= hex_str.byte_c_l();
95 while ( i < hex_str.length_i() ) {
96 int high_i = hex2nibble_i( *byte_c_l++ );
97 int low_i = hex2nibble_i( *byte_c_l++ );
98 if ( high_i < 0 || low_i < 0 )
99 return 1; // illegal char
100 bin_str_r += String( (char)( high_i << 4 | low_i ), 1 );
107 String_convert::hex2bin_str( String hex_str )
110 // silly, asserts should alway be "on"!
111 // assert( !hex2bin_i( hex_str, str ) );
112 int error_i = hex2bin_i( hex_str, str );
118 String_convert::hex2nibble_i( Byte byte )
120 if ( byte >= '0' && byte <= '9' )
122 if ( byte >= 'A' && byte <= 'F' )
123 return byte - 'A' + 10;
124 if ( byte >= 'a' && byte <= 'f')
125 return byte - 'a' + 10;
129 // stupido. Should use int_str()
131 String_convert::i2dec_str( int i, int length_i, char ch )
141 return String( fill_ch, length_i - dec_str.length_i() ) + dec_str;
145 // stupido. Should use int_str()
147 String_convert::u2hex_str( unsigned u, int length_i, char fill_ch )
155 str = String( (char)( ( u % 16 )["0123456789abcdef"] ) ) + str;
159 str += int_str( u, "%x" );
162 str = String( fill_ch, length_i - str.length_i() ) + str;
163 while ( ( str.length_i() > length_i ) && ( str[ 0 ] == 'f' ) )
164 str = str.mid_str( 2, INT_MAX );
170 String_convert::i2hex_str( int i, int length_i, char fill_ch )
172 return u2hex_str( (unsigned)i, length_i, fill_ch );
176 String_convert::nibble2hex_byte( Byte byte )
178 if ( ( byte & 0x0f ) <= 9 )
179 return ( byte & 0x0f ) + '0';
181 return ( byte & 0x0f ) - 10 + 'a';
184 Convert an integer to a string
187 #fmt# is a printf style format, default assumes "%d" as format.
190 String_convert::int_str(int i, char const* fmt)
192 char buffer[STRING_BUFFER_LEN];
193 snprintf(buffer, STRING_BUFFER_LEN,
194 (fmt ? fmt : "%d"), i ); // assume radix 10
195 return String(buffer);
199 Convert a double to a string.
201 @param #fmt# is a printf style format, default assumes "%lf" as format
204 String_convert::double_str(double f, char const* fmt)
206 char buf[STRING_BUFFER_LEN];
208 snprintf(buf, STRING_BUFFER_LEN, fmt ? fmt : "%f", f);
213 Make a string from a single character.
216 #n# is a repetition count, default value is 1
219 String_convert::char_str(char c, int n)
222 char* ch_p = new char[ n ];
223 memset( ch_p, c, n );
224 String s((Byte*)ch_p, n);
230 String_convert::rational_str(Rational r)
232 char * n = Itoa(r.numerator()); // LEAK????
235 if (r.denominator() != 1) {
236 char * d = Itoa(r.denominator());
237 s += String( '/' ) + String(d);
246 String_convert::pointer_str(const void *l)
249 return String_convert::i64_str(i64, "0x%0Lx");