239 * hexadecimal representation are removed unless all the digits
240 * are zero, in which case a single zero is used. Next, the
241 * exponent is represented by {@code "p"} followed
242 * by a decimal string of the unbiased exponent as if produced by
243 * a call to {@link Integer#toString(int) Integer.toString} on the
244 * exponent value.
245 *
246 * <li>If <i>m</i> is a {@code float} value with a subnormal
247 * representation, the significand is represented by the
248 * characters {@code "0x0."} followed by a
249 * hexadecimal representation of the rest of the significand as a
250 * fraction. Trailing zeros in the hexadecimal representation are
251 * removed. Next, the exponent is represented by
252 * {@code "p-126"}. Note that there must be at
253 * least one nonzero digit in a subnormal significand.
254 *
255 * </ul>
256 *
257 * </ul>
258 *
259 * <table border>
260 * <caption>Examples</caption>
261 * <tr><th>Floating-point Value</th><th>Hexadecimal String</th>
262 * <tr><td>{@code 1.0}</td> <td>{@code 0x1.0p0}</td>
263 * <tr><td>{@code -1.0}</td> <td>{@code -0x1.0p0}</td>
264 * <tr><td>{@code 2.0}</td> <td>{@code 0x1.0p1}</td>
265 * <tr><td>{@code 3.0}</td> <td>{@code 0x1.8p1}</td>
266 * <tr><td>{@code 0.5}</td> <td>{@code 0x1.0p-1}</td>
267 * <tr><td>{@code 0.25}</td> <td>{@code 0x1.0p-2}</td>
268 * <tr><td>{@code Float.MAX_VALUE}</td>
269 * <td>{@code 0x1.fffffep127}</td>
270 * <tr><td>{@code Minimum Normal Value}</td>
271 * <td>{@code 0x1.0p-126}</td>
272 * <tr><td>{@code Maximum Subnormal Value}</td>
273 * <td>{@code 0x0.fffffep-126}</td>
274 * <tr><td>{@code Float.MIN_VALUE}</td>
275 * <td>{@code 0x0.000002p-126}</td>
276 * </table>
277 * @param f the {@code float} to be converted.
278 * @return a hex string representation of the argument.
279 * @since 1.5
280 * @author Joseph D. Darcy
281 */
282 public static String toHexString(float f) {
283 if (Math.abs(f) < Float.MIN_NORMAL
284 && f != 0.0f ) {// float subnormal
285 // Adjust exponent to create subnormal double, then
286 // replace subnormal double exponent with subnormal float
287 // exponent
288 String s = Double.toHexString(Math.scalb((double)f,
289 /* -1022+126 */
290 Double.MIN_EXPONENT-
291 Float.MIN_EXPONENT));
292 return s.replaceFirst("p-1022$", "p-126");
293 }
294 else // double string will be the same as float string
295 return Double.toHexString(f);
|
239 * hexadecimal representation are removed unless all the digits
240 * are zero, in which case a single zero is used. Next, the
241 * exponent is represented by {@code "p"} followed
242 * by a decimal string of the unbiased exponent as if produced by
243 * a call to {@link Integer#toString(int) Integer.toString} on the
244 * exponent value.
245 *
246 * <li>If <i>m</i> is a {@code float} value with a subnormal
247 * representation, the significand is represented by the
248 * characters {@code "0x0."} followed by a
249 * hexadecimal representation of the rest of the significand as a
250 * fraction. Trailing zeros in the hexadecimal representation are
251 * removed. Next, the exponent is represented by
252 * {@code "p-126"}. Note that there must be at
253 * least one nonzero digit in a subnormal significand.
254 *
255 * </ul>
256 *
257 * </ul>
258 *
259 * <table class="plain">
260 * <caption>Examples</caption>
261 * <thead>
262 * <tr><th>Floating-point Value</th><th>Hexadecimal String</th>
263 * </thead>
264 * <tbody>
265 * <tr><td>{@code 1.0}</td> <td>{@code 0x1.0p0}</td>
266 * <tr><td>{@code -1.0}</td> <td>{@code -0x1.0p0}</td>
267 * <tr><td>{@code 2.0}</td> <td>{@code 0x1.0p1}</td>
268 * <tr><td>{@code 3.0}</td> <td>{@code 0x1.8p1}</td>
269 * <tr><td>{@code 0.5}</td> <td>{@code 0x1.0p-1}</td>
270 * <tr><td>{@code 0.25}</td> <td>{@code 0x1.0p-2}</td>
271 * <tr><td>{@code Float.MAX_VALUE}</td>
272 * <td>{@code 0x1.fffffep127}</td>
273 * <tr><td>{@code Minimum Normal Value}</td>
274 * <td>{@code 0x1.0p-126}</td>
275 * <tr><td>{@code Maximum Subnormal Value}</td>
276 * <td>{@code 0x0.fffffep-126}</td>
277 * <tr><td>{@code Float.MIN_VALUE}</td>
278 * <td>{@code 0x0.000002p-126}</td>
279 * </tbody>
280 * </table>
281 * @param f the {@code float} to be converted.
282 * @return a hex string representation of the argument.
283 * @since 1.5
284 * @author Joseph D. Darcy
285 */
286 public static String toHexString(float f) {
287 if (Math.abs(f) < Float.MIN_NORMAL
288 && f != 0.0f ) {// float subnormal
289 // Adjust exponent to create subnormal double, then
290 // replace subnormal double exponent with subnormal float
291 // exponent
292 String s = Double.toHexString(Math.scalb((double)f,
293 /* -1022+126 */
294 Double.MIN_EXPONENT-
295 Float.MIN_EXPONENT));
296 return s.replaceFirst("p-1022$", "p-126");
297 }
298 else // double string will be the same as float string
299 return Double.toHexString(f);
|