249 * <blockquote> 250 * {@code 0123456789abcdef} 251 * </blockquote> 252 * 253 * These are the characters {@code '\u005Cu0030'} through 254 * {@code '\u005Cu0039'} and {@code '\u005Cu0061'} through 255 * {@code '\u005Cu0066'}. If uppercase letters are desired, 256 * the {@link java.lang.String#toUpperCase()} method may be called 257 * on the result: 258 * 259 * <blockquote> 260 * {@code Long.toHexString(n).toUpperCase()} 261 * </blockquote> 262 * 263 * @param i a {@code long} to be converted to a string. 264 * @return the string representation of the unsigned {@code long} 265 * value represented by the argument in hexadecimal 266 * (base 16). 267 * @see #parseUnsignedLong(String, int) 268 * @see #toUnsignedString(long, int) 269 * @since 1.0.2 270 */ 271 public static String toHexString(long i) { 272 return toUnsignedString0(i, 4); 273 } 274 275 /** 276 * Returns a string representation of the {@code long} 277 * argument as an unsigned integer in base 8. 278 * 279 * <p>The unsigned {@code long} value is the argument plus 280 * 2<sup>64</sup> if the argument is negative; otherwise, it is 281 * equal to the argument. This value is converted to a string of 282 * ASCII digits in octal (base 8) with no extra leading 283 * {@code 0}s. 284 * 285 * <p>The value of the argument can be recovered from the returned 286 * string {@code s} by calling {@link 287 * Long#parseUnsignedLong(String, int) Long.parseUnsignedLong(s, 288 * 8)}. 289 * 290 * <p>If the unsigned magnitude is zero, it is represented by a 291 * single zero character {@code '0'} ({@code '\u005Cu0030'}); 292 * otherwise, the first character of the representation of the 293 * unsigned magnitude will not be the zero character. The 294 * following characters are used as octal digits: 295 * 296 * <blockquote> 339 * @see #parseUnsignedLong(String, int) 340 * @see #toUnsignedString(long, int) 341 * @since 1.0.2 342 */ 343 public static String toBinaryString(long i) { 344 return toUnsignedString0(i, 1); 345 } 346 347 /** 348 * Format a long (treated as unsigned) into a String. 349 * @param val the value to format 350 * @param shift the log2 of the base to format in (4 for hex, 3 for octal, 1 for binary) 351 */ 352 static String toUnsignedString0(long val, int shift) { 353 // assert shift > 0 && shift <=5 : "Illegal shift value"; 354 int mag = Long.SIZE - Long.numberOfLeadingZeros(val); 355 int chars = Math.max(((mag + (shift - 1)) / shift), 1); 356 char[] buf = new char[chars]; 357 358 formatUnsignedLong(val, shift, buf, 0, chars); 359 return new String(buf, true); 360 } 361 362 /** 363 * Format a long (treated as unsigned) into a character buffer. 364 * @param val the unsigned long to format 365 * @param shift the log2 of the base to format in (4 for hex, 3 for octal, 1 for binary) 366 * @param buf the character buffer to write to 367 * @param offset the offset in the destination buffer to start at 368 * @param len the number of characters to write 369 * @return the lowest character location used 370 */ 371 static int formatUnsignedLong(long val, int shift, char[] buf, int offset, int len) { 372 int charPos = len; 373 int radix = 1 << shift; 374 int mask = radix - 1; 375 do { 376 buf[offset + --charPos] = Integer.digits[((int) val) & mask]; 377 val >>>= shift; 378 } while (val != 0 && charPos > 0); | 249 * <blockquote> 250 * {@code 0123456789abcdef} 251 * </blockquote> 252 * 253 * These are the characters {@code '\u005Cu0030'} through 254 * {@code '\u005Cu0039'} and {@code '\u005Cu0061'} through 255 * {@code '\u005Cu0066'}. If uppercase letters are desired, 256 * the {@link java.lang.String#toUpperCase()} method may be called 257 * on the result: 258 * 259 * <blockquote> 260 * {@code Long.toHexString(n).toUpperCase()} 261 * </blockquote> 262 * 263 * @param i a {@code long} to be converted to a string. 264 * @return the string representation of the unsigned {@code long} 265 * value represented by the argument in hexadecimal 266 * (base 16). 267 * @see #parseUnsignedLong(String, int) 268 * @see #toUnsignedString(long, int) 269 * @see #toHexString(long, int) 270 * @since 1.0.2 271 */ 272 public static String toHexString(long i) { 273 return toUnsignedString0(i, 4); 274 } 275 276 /** 277 * Returns a string representation of the {@code long} 278 * argument as an unsigned integer in base 16, padded with 279 * leading zeroes if necessary. 280 * 281 * <p>The unsigned {@code long} value is the argument plus 282 * 2<sup>64</sup> if the argument is negative; otherwise, it is 283 * equal to the argument. This value is converted to a string of 284 * ASCII digits in hexadecimal (base 16) with possibly 285 * appended leading zeroes. A minimum amount of zeroes is appended 286 * to ensure the length of the resulting string is at least 287 * {@code minWidth}. 288 * 289 * <p>The value of the argument can be recovered from the returned 290 * string {@code s} by calling {@link 291 * Long#parseUnsignedLong(String, int) Long.parseUnsignedLong(s, 292 * 16)}. 293 * 294 * <p>If the unsigned magnitude is zero, it is represented by a 295 * single zero character {@code '0'} ({@code '\u005Cu0030'}). 296 * The following characters are used as hexadecimal digits: 297 * 298 * <blockquote> 299 * {@code 0123456789abcdef} 300 * </blockquote> 301 * 302 * These are the characters {@code '\u005Cu0030'} through 303 * {@code '\u005Cu0039'} and {@code '\u005Cu0061'} through 304 * {@code '\u005Cu0066'}. If uppercase letters are desired, 305 * the {@link java.lang.String#toUpperCase()} method may be called 306 * on the result: 307 * 308 * <blockquote> 309 * {@code Long.toHexString(n, 12).toUpperCase()} 310 * </blockquote> 311 * 312 * @param i a {@code long} to be converted to a string. 313 * @param minWidth a minimum required length of the resulting string. 314 * @return the string representation of the unsigned {@code long} 315 * value represented by the argument in hexadecimal 316 * (base 16), padded with leading zeroes if necessary. 317 * @see #parseUnsignedLong(String, int) 318 * @see #toUnsignedString(long, int) 319 * @see #toHexString(long) 320 * @since 1.9 321 */ 322 public static String toHexString(long i, int minWidth) { 323 return toUnsignedString0(i, 4, minWidth); 324 } 325 326 /** 327 * Returns a string representation of the {@code long} 328 * argument as an unsigned integer in base 8. 329 * 330 * <p>The unsigned {@code long} value is the argument plus 331 * 2<sup>64</sup> if the argument is negative; otherwise, it is 332 * equal to the argument. This value is converted to a string of 333 * ASCII digits in octal (base 8) with no extra leading 334 * {@code 0}s. 335 * 336 * <p>The value of the argument can be recovered from the returned 337 * string {@code s} by calling {@link 338 * Long#parseUnsignedLong(String, int) Long.parseUnsignedLong(s, 339 * 8)}. 340 * 341 * <p>If the unsigned magnitude is zero, it is represented by a 342 * single zero character {@code '0'} ({@code '\u005Cu0030'}); 343 * otherwise, the first character of the representation of the 344 * unsigned magnitude will not be the zero character. The 345 * following characters are used as octal digits: 346 * 347 * <blockquote> 390 * @see #parseUnsignedLong(String, int) 391 * @see #toUnsignedString(long, int) 392 * @since 1.0.2 393 */ 394 public static String toBinaryString(long i) { 395 return toUnsignedString0(i, 1); 396 } 397 398 /** 399 * Format a long (treated as unsigned) into a String. 400 * @param val the value to format 401 * @param shift the log2 of the base to format in (4 for hex, 3 for octal, 1 for binary) 402 */ 403 static String toUnsignedString0(long val, int shift) { 404 // assert shift > 0 && shift <=5 : "Illegal shift value"; 405 int mag = Long.SIZE - Long.numberOfLeadingZeros(val); 406 int chars = Math.max(((mag + (shift - 1)) / shift), 1); 407 char[] buf = new char[chars]; 408 409 formatUnsignedLong(val, shift, buf, 0, chars); 410 return new String(buf, true); 411 } 412 413 /** 414 * Format a long (treated as unsigned) into a String. 415 * @param val the value to format 416 * @param shift the log2 of the base to format in (4 for hex, 3 for octal, 1 for binary) 417 * @param minWidth the minimum width of the produced String 418 */ 419 static String toUnsignedString0(long val, int shift, int minWidth) { 420 int mag = Long.SIZE - Long.numberOfLeadingZeros(val); 421 int magLen = (mag + (shift - 1)) / shift; 422 int zeroes = Math.max(Math.max(minWidth, 1) - magLen, 0); 423 char[] buf = new char[magLen + zeroes]; 424 425 for (int i = 0; i < zeroes; ++i) 426 buf[i] = '0'; 427 if (magLen > 0) 428 formatUnsignedLong(val, shift, buf, zeroes, magLen); 429 return new String(buf, true); 430 } 431 432 /** 433 * Format a long (treated as unsigned) into a character buffer. 434 * @param val the unsigned long to format 435 * @param shift the log2 of the base to format in (4 for hex, 3 for octal, 1 for binary) 436 * @param buf the character buffer to write to 437 * @param offset the offset in the destination buffer to start at 438 * @param len the number of characters to write 439 * @return the lowest character location used 440 */ 441 static int formatUnsignedLong(long val, int shift, char[] buf, int offset, int len) { 442 int charPos = len; 443 int radix = 1 << shift; 444 int mask = radix - 1; 445 do { 446 buf[offset + --charPos] = Integer.digits[((int) val) & mask]; 447 val >>>= shift; 448 } while (val != 0 && charPos > 0); |