-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
45e191d
commit 3329c3f
Showing
5 changed files
with
53 additions
and
26 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
Binary file modified
BIN
+1.05 KB
(260%)
out/production/classes/com/lowlevelsubmarine/applock/Hex.class
Binary file not shown.
Binary file modified
BIN
+0 Bytes
(100%)
out/production/classes/com/lowlevelsubmarine/applock/Test.class
Binary file not shown.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,15 +1,41 @@ | ||
package com.lowlevelsubmarine.applock; | ||
|
||
import com.sun.org.apache.xerces.internal.impl.dv.util.HexBin; | ||
|
||
public class Hex { | ||
|
||
public static String encode(byte[] decoded) { | ||
return HexBin.encode(decoded).toLowerCase(); | ||
private static final char[] HEX_CODE = "0123456789abcdef".toCharArray(); | ||
|
||
public static String encode(byte[] data) { | ||
StringBuilder r = new StringBuilder(data.length*2); | ||
for ( byte b : data) { | ||
r.append(HEX_CODE[(b >> 4) & 0xF]); | ||
r.append(HEX_CODE[(b & 0xF)]); | ||
} | ||
return r.toString(); | ||
} | ||
|
||
public static byte[] decode(String s) { | ||
final int len = s.length(); | ||
// "111" is not a valid hex encoding. | ||
if( len%2 != 0 ) { | ||
throw new IllegalArgumentException("hexBinary needs to be even-length: " + s); | ||
} | ||
byte[] out = new byte[len/2]; | ||
for( int i=0; i<len; i+=2 ) { | ||
int h = hexToBin(s.charAt(i )); | ||
int l = hexToBin(s.charAt(i+1)); | ||
if( h==-1 || l==-1 ) { | ||
throw new IllegalArgumentException("contains illegal character for hexBinary: " + s); | ||
} | ||
out[i/2] = (byte)(h*16+l); | ||
} | ||
return out; | ||
} | ||
|
||
public static byte[] decode(String encoded) { | ||
return HexBin.decode(encoded); | ||
private static int hexToBin(char ch) { | ||
if('0'<=ch && ch<='9') return ch-'0'; | ||
if('A'<=ch && ch<='F') return ch-'A'+10; | ||
if('a'<=ch && ch<='f') return ch-'a'+10; | ||
return -1; | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters