Here you can find the source of ascii2bin(byte[] ascii, int offset, int len, byte[] binary)
public static boolean ascii2bin(byte[] ascii, int offset, int len, byte[] binary)
//package com.java2s; /*// w ww .ja va 2s.c o m * Copyright (c) 2010-2012 TMate Software Ltd * * This program is free software; you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by * the Free Software Foundation; version 2 of the License. * * This program is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU General Public License for more details. * * For information on how to redistribute this software under * the terms of a license other than GNU General Public License * contact TMate Software at support@hg4j.com */ public class Main { public static boolean ascii2bin(byte[] ascii, int offset, int len, byte[] binary) { assert len % 2 == 0; assert binary.length >= len >>> 1; boolean zeroBytes = true; for (int i = 0, j = offset; i < len >>> 1; i++) { int b = ascii[j++] & 0xCF; // -0x30 to get decimal digit out from their char, and to uppercase if a letter int hiNibble = b > 64 ? b - 55 : b; b = ascii[j++] & 0xCF; int lowNibble = b > 64 ? b - 55 : b; if (hiNibble >= 16 || lowNibble >= 16) { throw new IllegalArgumentException( String.format("Characters '%c%c' (%1$d and %2$d) at index %d are not valid hex digits", ascii[j - 2], ascii[j - 1], j - 2)); } b = (((hiNibble << 4) | lowNibble) & 0xFF); binary[i] = (byte) b; zeroBytes = zeroBytes && b == 0; } return zeroBytes; } }