Created
October 23, 2012 14:23
-
-
Save Robertof/3939031 to your computer and use it in GitHub Desktop.
[sharing time] Utils class for gist 3938993
This file contains hidden or 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
| /** | |
| * Created by Robertof <[email protected] / http://about.me/roberto.f>. | |
| * File creation date: 10-ott-2012 19.14.37 | |
| * Licensed under GNU/GPL v3. | |
| */ | |
| package com.robertof; | |
| public class Utils { | |
| public static String bytesToHex(byte[] bytes) { | |
| final char[] hexArray = {'0','1','2','3','4','5','6','7','8','9','A','B','C','D','E','F'}; | |
| char[] hexChars = new char[bytes.length * 2]; | |
| int v; | |
| for ( int j = 0; j < bytes.length; j++ ) { | |
| v = bytes[j] & 0xFF; | |
| hexChars[j * 2] = hexArray[v >>> 4]; | |
| hexChars[j * 2 + 1] = hexArray[v & 0x0F]; | |
| } | |
| return new String(hexChars); | |
| } | |
| public static byte[] hexToBytes (String hex) | |
| { | |
| int len = hex.length(); | |
| byte[] data = new byte[len / 2]; | |
| for (int i = 0; i < len; i += 2) | |
| { | |
| data[i/2] = (byte) ((Character.digit(hex.charAt(i), 16) << 4) | |
| + Character.digit(hex.charAt(i+1), 16)); | |
| } | |
| return data; | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment