ff.prac.util.encoding.Base16 Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of prac Show documentation
Show all versions of prac Show documentation
practical java utilities
/*
* Copyright (C) 2015-2018 XinZhenfeng
*
* This file is part of prac
*
* prac is free software: you can use, redistribute, and/or modify
* it under the terms of the GNU Affero General Public License, version 3
* or later ("AGPL"), as published by the Free Software Foundation.
*
* 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 Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see .
*/
package ff.prac.util.encoding;
public class Base16 {
public static final String ENCODED_CHARSET = "US-ASCII";
private static byte[] encodingTable = {
'0', '1', '2', '3', '4', '5', '6', '7', '8', '9',
'A', 'B', 'C', 'D', 'E', 'F'};
private static byte[] decodingTable = new byte[128];
static {
for(int i=0; i>> 4 ) & 0x0f;
int i2 = by & 0x0f ;
encoded.append( (char)(encodingTable[i1]) )
.append( (char)(encodingTable[i2]) );
}
return encoded.toString();
}
public static byte[] decode(String encoded) {
if(encoded==null || encoded.length()%2==1) {
return null;
}
byte[] decoded = new byte[encoded.length()/2];
for(int i=0; (i+1)