Return-Path: X-Original-To: apmail-directory-commits-archive@www.apache.org Delivered-To: apmail-directory-commits-archive@www.apache.org Received: from mail.apache.org (hermes.apache.org [140.211.11.3]) by minotaur.apache.org (Postfix) with SMTP id EC6D4107AC for ; Tue, 13 Jan 2015 00:01:43 +0000 (UTC) Received: (qmail 22895 invoked by uid 500); 13 Jan 2015 00:01:45 -0000 Delivered-To: apmail-directory-commits-archive@directory.apache.org Received: (qmail 22844 invoked by uid 500); 13 Jan 2015 00:01:45 -0000 Mailing-List: contact commits-help@directory.apache.org; run by ezmlm Precedence: bulk List-Help: List-Unsubscribe: List-Post: List-Id: Reply-To: dev@directory.apache.org Delivered-To: mailing list commits@directory.apache.org Received: (qmail 22826 invoked by uid 99); 13 Jan 2015 00:01:45 -0000 Received: from tyr.zones.apache.org (HELO tyr.zones.apache.org) (140.211.11.114) by apache.org (qpsmtpd/0.29) with ESMTP; Tue, 13 Jan 2015 00:01:45 +0000 Received: by tyr.zones.apache.org (Postfix, from userid 65534) id 3378AA02FC9; Tue, 13 Jan 2015 00:01:45 +0000 (UTC) Content-Type: text/plain; charset="us-ascii" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit From: drankye@apache.org To: commits@directory.apache.org Date: Tue, 13 Jan 2015 00:01:45 -0000 Message-Id: <43d76f4b53664fd7a1c9191e52282c79@git.apache.org> In-Reply-To: References: X-Mailer: ASF-Git Admin Mailer Subject: [2/2] directory-kerberos git commit: Consolidating common utils Consolidating common utils Project: http://git-wip-us.apache.org/repos/asf/directory-kerberos/repo Commit: http://git-wip-us.apache.org/repos/asf/directory-kerberos/commit/b7d41822 Tree: http://git-wip-us.apache.org/repos/asf/directory-kerberos/tree/b7d41822 Diff: http://git-wip-us.apache.org/repos/asf/directory-kerberos/diff/b7d41822 Branch: refs/heads/master Commit: b7d4182295a824e98b1ace36804502ddfb2e009a Parents: c58ed0e Author: Drankye Authored: Tue Jan 13 08:00:53 2015 +0800 Committer: Drankye Committed: Tue Jan 13 08:00:53 2015 +0800 ---------------------------------------------------------------------- contrib/haox-util/pom.xml | 18 +++++++++ .../src/main/java/org/haox/util/HexUtil.java | 39 ++++++++++++++++++++ 2 files changed, 57 insertions(+) ---------------------------------------------------------------------- http://git-wip-us.apache.org/repos/asf/directory-kerberos/blob/b7d41822/contrib/haox-util/pom.xml ---------------------------------------------------------------------- diff --git a/contrib/haox-util/pom.xml b/contrib/haox-util/pom.xml new file mode 100644 index 0000000..c3e59a9 --- /dev/null +++ b/contrib/haox-util/pom.xml @@ -0,0 +1,18 @@ + + + 4.0.0 + + + contrib + org.haox + 1.0-SNAPSHOT + + + haox-util + + Haox Util + Haox common util, without any 3rd party dependency + + + + http://git-wip-us.apache.org/repos/asf/directory-kerberos/blob/b7d41822/contrib/haox-util/src/main/java/org/haox/util/HexUtil.java ---------------------------------------------------------------------- diff --git a/contrib/haox-util/src/main/java/org/haox/util/HexUtil.java b/contrib/haox-util/src/main/java/org/haox/util/HexUtil.java new file mode 100644 index 0000000..f8e06f9 --- /dev/null +++ b/contrib/haox-util/src/main/java/org/haox/util/HexUtil.java @@ -0,0 +1,39 @@ +package org.haox.util; + +public class HexUtil { + + final static String HEX_CHARS_STR = "0123456789ABCDEF"; + final static char[] HEX_CHARS = HEX_CHARS_STR.toCharArray(); + + /** + * Convert bytes into format as: + * 02020080 + */ + public static String bytesToHex(byte[] bytes) { + int len = bytes.length * 2; + char[] hexChars = new char[len]; + for ( int j = 0; j < bytes.length; j++ ) { + int v = bytes[j] & 0xFF; + hexChars[j * 2] = HEX_CHARS[v >>> 4]; + hexChars[j * 2 + 1] = HEX_CHARS[v & 0x0F]; + } + + return new String(hexChars); + } + + /** + * Convert hex string like follows into byte array + * 02020080 + */ + public static byte[] hex2bytes(String hexString) { + hexString = hexString.toUpperCase(); + int len = hexString.length() / 2; + byte[] bytes = new byte[len]; + char[] hexChars = hexString.toCharArray(); + for (int i = 0, j = 0; i < len; ++i) { + bytes[i] = (byte) ((HEX_CHARS_STR.indexOf(hexChars[j++]) << 4) + HEX_CHARS_STR.indexOf(hexChars[j++])); + } + + return bytes; + } +}