Java Utililty Methods ASCII

List of utility methods to do ASCII

Description

The list of methods to do ASCII are organized into topic(s).

Method

charascii(final int c)
ASCII.
return (char) ((c) <= 9 ? (c) + '0' : (c) + 'A' - 0xA);
byte[]ascii(String string)
Converts a string, which should contain only ascii-representable characters, to a byte[].
byte[] bytes = new byte[string.length()];
for (int i = 0; i < string.length(); i++) {
    bytes[i] = (byte) string.charAt(i);
return bytes;
byte[]ascii2Bcd(byte[] asc)
ascii Bcd
int len = asc.length / 2;
byte[] bcd = new byte[len];
for (int i = 0; i < len; i++) {
    bcd[i] = (byte) ((asc[2 * i] << 4) | asc[2 * i + 1]);
return bcd;
byte[]ascii2BCD(byte[] val, int len)
ascii BCD
byte[] valByte = new byte[(len + 1) / 2];
if (len % 2 == 0) {
    for (int i = 0; i < len; i++) {
        byte b = val[i];
        if (b > '9') {
            b = (byte) (b % 0x10 + 9);
        } else {
            b = (byte) (b % 0x10);
...
byte[]AsciiBTAddressToBytes(String btAddress)
Converts a nicely formatted Bluetooth address to string of bytes representing the address
if (btAddress.length() != 17)
    return null;
String[] tokens = btAddress.split(":");
if (tokens.length != 6)
    return null;
byte[] finalResult = new byte[tokens.length];
int i = 0;
for (String val : tokens) {
...
byte[]asciiBytes(String x)
ascii Bytes
byte[] res = new byte[x.length()];
for (int len = x.length(), i = 0; i < len; ++i)
    res[i] = (byte) x.charAt(i);
return res;
charasciiChar(int value)
ascii Char
if (value < 0x20 || value > 0x7e) {
    return '.';
} else {
    return (char) value;
voidasciiChars()
ascii Chars
for (int code = 0; code < 128; code++) {
    System.out.println("code=" + code + ",0x" + Integer.toHexString(code) + " [" + (char) code + "]");
byte[]asciiCharToBytes(char[] chars)
Converts characters from ascii encoded text to a byte[] and zero outs the original char[]
byte[] bytes = new byte[chars.length];
for (int i = 0; i < chars.length; i++) {
    bytes[i] = (byte) chars[i];
    chars[i] = '\0';
return bytes;
StringasciiDump(byte[] in)
ascii Dump
String out = new String("");
for (int i = 0; i < in.length; i++)
    out += ((in[i] & 0xFF) == 0) ? '_' : (char) (in[i] & 0xFF);
return out;