Description
Appends a two-character hexadecimal representation of the supplied byte to the supplied string appender.
License
Open Source License
Parameter
Parameter | Description |
---|
b | the byte to format |
ap | the character sequence to append to |
Exception
Parameter | Description |
---|
IllegalStateException | DOCUMENT ME! |
Return
the same appendable supplied as an input parameter
Declaration
public static Appendable appendToString(final byte b, final Appendable ap)
Method Source Code
//package com.java2s;
/*// w ww . j a v a 2s .c o m
* Copyright (c) 2008 Kevin Brockhoff
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
* THE SOFTWARE.
*/
import java.io.IOException;
public class Main {
private static final String HEX_CHARS = "0123456789abcdefABCDEF";
/**
* Appends a two-character hexadecimal representation of the supplied byte to the supplied string appender.
*
* @param b the byte to format
* @param ap the character sequence to append to
*
* @return the same appendable supplied as an input parameter
*
* @throws IllegalStateException DOCUMENT ME!
*/
public static Appendable appendToString(final byte b, final Appendable ap) {
final int hex = b & 0xFF;
try {
ap.append(HEX_CHARS.charAt(hex >> 4));
ap.append(HEX_CHARS.charAt(hex & 0x0f));
} catch (IOException e) {
throw new IllegalStateException(e);
}
return ap;
}
/**
* Appends a two-character hexadecimal representation of each byte in the specified range of the supplied byte array
* to the supplied string appender.
*
* @param raw the byte array to format
* @param start the array index value to begin at
* @param len the number of bytes to format
* @param ap the character sequence to append to
*
* @return the same appendable supplied as an input parameter
*
* @throws IllegalArgumentException DOCUMENT ME!
*/
public static Appendable appendToString(final byte[] raw, final int start, final int len, final Appendable ap) {
if (start + len > raw.length) {
throw new IllegalArgumentException("exceeds byte array length");
}
for (int i = start; i < start + len; i++) {
appendToString(raw[i], ap);
}
return ap;
}
/**
* Appends a two-character hexadecimal representation of each byte in the supplied byte array to the supplied string
* appender.
*
* @param raw the byte array to format
* @param ap the character sequence to append to
*
* @return the same appendable supplied as an input parameter
*/
public static Appendable appendToString(final byte[] raw, final Appendable ap) {
return appendToString(raw, 0, raw.length, ap);
}
/**
* Appends two bytes array into one.
*
* @param a the first array
* @param b the following array
*
* @return the combined array
*/
public static byte[] append(final byte[] a, final byte[] b) {
final byte[] result = new byte[a.length + b.length];
System.arraycopy(a, 0, result, 0, a.length);
System.arraycopy(b, 0, result, a.length, b.length);
return result;
}
}
Related
- appendToFile(String filepath, String newLine)
- appendToFile(String sb, String directory, String fileName)
- appendToFile(String str, String filename, boolean appendNewLine)
- appendToFile(String string, File file)
- appendToFile(String text, String fileName)
- appendToTextFile(final File file, final String text)
- appendToTextFile(String contents, String fullPathFilename)