Subversion Repository Public Repository

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
50
51
52
53
54
55
56
57
58
59
60
61
package org.litesoft.codec;

import java.io.*;

import org.litesoft.core.util.*;
import org.litesoft.util.*;

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

Diff revisions: vs.
Revision Author Commited Message
812 GeorgeS picture GeorgeS Sat 18 Aug, 2012 17:45:40 +0000