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
++)
34 str
+= (char)nibble2hex_byte (*byte_C
>> 4);
35 str
+= (char)nibble2hex_byte (*byte_C
++);
41 String_convert::bin2_i (String bin_str
)
43 return bin2_u (bin_str
);
47 String_convert::bin2_u (String bin_str
)
49 assert (bin_str
.length_i() <= (int)sizeof(unsigned));
51 unsigned result_u
= 0;
52 for (int i
= 0; i
< bin_str
.length_i(); i
++)
55 result_u
+= (Byte
)bin_str
[ i
];
60 // breendet imp from String
62 String_convert::dec2_i (String dec_str
)
64 if (!dec_str
.length_i())
68 int conv
= sscanf (dec_str
.ch_C(), "%ld", &l
);
75 String_convert::i64_str (I64 i64
, char const* fmt
)
77 char buffer
[STRING_BUFFER_LEN
];
78 snprintf (buffer
, STRING_BUFFER_LEN
,
79 (fmt
? fmt
: "%Ld"), i64
); // assume radix 10
80 return String (buffer
);
83 // breendet imp from String
85 String_convert::dec2_f (String dec_str
)
87 if (!dec_str
.length_i())
90 int conv
= sscanf (dec_str
.ch_C(), "%lf", &d
);
96 String_convert::hex2bin_i (String hex_str
, String
& bin_str_r
)
98 if (hex_str
.length_i() % 2)
99 hex_str
= "0" + hex_str
;
102 Byte
const* byte_C
= hex_str
.byte_C();
104 while (i
< hex_str
.length_i())
106 int high_i
= hex2nibble_i (*byte_C
++);
107 int low_i
= hex2nibble_i (*byte_C
++);
108 if (high_i
< 0 || low_i
< 0)
109 return 1; // illegal char
110 bin_str_r
+= String ((char)(high_i
<< 4 | low_i
), 1 );
117 String_convert::hex2bin_str (String hex_str
)
120 // silly, asserts should alway be "on"!
121 // assert (!hex2bin_i (hex_str, str) );
122 int error_i
= hex2bin_i (hex_str
, str
);
128 String_convert::hex2nibble_i (Byte byte
)
130 if (byte
>= '0' && byte
<= '9')
132 if (byte
>= 'A' && byte
<= 'F')
133 return byte
- 'A' + 10;
134 if (byte
>= 'a' && byte
<= 'f')
135 return byte
- 'a' + 10;
139 // stupido. Should use int_str()
141 String_convert::i2dec_str (int i
, int length_i
, char ch
)
151 return String (fill_ch
, length_i
- dec_str
.length_i()) + dec_str
;
155 // stupido. Should use int_str()
157 String_convert::u2hex_str (unsigned u
, int length_i
, char fill_ch
)
166 str
= String ((char)((u
% 16)["0123456789abcdef"] ) ) + str
;
170 str
+= int_str (u
, "%x"); // hmm. %lx vs. %x -> portability?
173 str
= String (fill_ch
, length_i
- str
.length_i()) + str
;
174 while ((str
.length_i() > length_i
) && (str
[ 0 ] == 'f' ) )
175 str
= str
.mid_str (2, INT_MAX
);
181 String_convert::i2hex_str (int i
, int length_i
, char fill_ch
)
183 return u2hex_str ((unsigned)i
, length_i
, fill_ch
);
187 String_convert::nibble2hex_byte (Byte byte
)
189 if ((byte
& 0x0f) <= 9 )
190 return (byte
& 0x0f) + '0';
192 return (byte
& 0x0f) - 10 + 'a';
195 Convert an integer to a string
198 #fmt# is a printf style format, default assumes "%d" as format.
201 String_convert::int_str (int i
, char const* fmt
)
203 char buffer
[STRING_BUFFER_LEN
];
204 snprintf (buffer
, STRING_BUFFER_LEN
,
205 (fmt
? fmt
: "%d"), i
); // assume radix 10
206 return String (buffer
);
210 Convert a double to a string.
212 @param #fmt# is a printf style format, default assumes "%lf" as format
215 String_convert::double_str (double f
, char const* fmt
)
217 char buf
[STRING_BUFFER_LEN
];
219 snprintf (buf
, STRING_BUFFER_LEN
, fmt
? fmt
: "%f", f
);
224 Make a string from a single character.
227 #n# is a repetition count, default value is 1
230 String_convert::char_str (char c
, int n
)
233 char* ch_p
= new char[ n
];
235 String
s ((Byte
*)ch_p
, n
);
241 String_convert::rational_str (Rational r
)
243 char * n
= Itoa (r
.numerator()); // LEAK????
246 if (r
.denominator() != 1)
248 char * d
= Itoa (r
.denominator());
249 s
+= String ('/') + String (d
);
258 String_convert::pointer_str (void const *l
)
260 char buffer
[STRING_BUFFER_LEN
];
261 snprintf (buffer
, STRING_BUFFER_LEN
, "%p", l
); // assume radix 10
262 return String (buffer
);