litesoft
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 |
package org.litesoft.codec; import org.litesoft.commonfoundation.base.*; import org.litesoft.server.file.*; import java.io.*; public class HexStringCodec { public static String encode( String pRegularString ) { if ( pRegularString == null ) { return null; } byte[] zBytes; try { zBytes = pRegularString.getBytes( FileUtils.UTF_8 ); } catch ( UnsupportedEncodingException e ) { throw new RuntimeException( e ); } StringBuilder sb = new StringBuilder( zBytes.length + zBytes.length ); for ( byte zByte : zBytes ) { sb.append( Hex.toChar( zByte >>> 4 ) ); sb.append( Hex.toChar( zByte ) ); } return sb.toString(); } public static String decode( String pEncodedString ) { if ( pEncodedString == null ) { return null; } if ( (pEncodedString.length() & 1) == 1 ) { throw new IllegalArgumentException( "Can't be HEX as it is 'Odd' Length: '" + pEncodedString + "'" ); } int zCharAt = 0; byte[] zBytes = new byte[pEncodedString.length() / 2]; for ( int i = 0; i < zBytes.length; i++ ) { int z1st = Hex.fromCharChecked( pEncodedString.charAt( zCharAt++ ) ); int z2nd = Hex.fromCharChecked( pEncodedString.charAt( zCharAt++ ) ); zBytes[i] = (byte) ((z1st << 4) + z2nd); } try { return new String( zBytes, FileUtils.UTF_8 ); } catch ( UnsupportedEncodingException e ) { throw new RuntimeException( e ); } } } |
Commits for litesoft/trunk/Java/core/Server/src/org/litesoft/codec/HexStringCodec.java
Revision | Author | Commited | Message |
---|---|---|---|
958 Diff | GeorgeS | Mon 14 Jul, 2014 15:29:35 +0000 | Embrace OSS code bases. |
948 Diff | GeorgeS | Sat 07 Jun, 2014 23:42:39 +0000 | Jusefuls Formatter Updated to New Code Format |
942 Diff | GeorgeS | Mon 02 Jun, 2014 23:41:46 +0000 | Extracting commonfoundation |
812 | GeorgeS | Sat 18 Aug, 2012 17:45:40 +0000 |