diff options
author | Tobias Brunner <tobias@strongswan.org> | 2014-05-30 11:08:35 +0200 |
---|---|---|
committer | Tobias Brunner <tobias@strongswan.org> | 2014-07-22 10:41:48 +0200 |
commit | 140ce41a392353174657d1bdaada744fe99c30f5 (patch) | |
tree | ac9d8d0c6d0be580b301c281e64e00c3378239fd /src | |
parent | 9d994ba5eaa0f92ecaa6ff95d0b133307312fc8f (diff) | |
download | strongswan-140ce41a392353174657d1bdaada744fe99c30f5.tar.bz2 strongswan-140ce41a392353174657d1bdaada744fe99c30f5.tar.xz |
android: Add utility method to convert a byte array to a hex string
Diffstat (limited to 'src')
-rw-r--r-- | src/frontends/android/src/org/strongswan/android/utils/Utils.java | 40 |
1 files changed, 40 insertions, 0 deletions
diff --git a/src/frontends/android/src/org/strongswan/android/utils/Utils.java b/src/frontends/android/src/org/strongswan/android/utils/Utils.java new file mode 100644 index 000000000..b5c447f31 --- /dev/null +++ b/src/frontends/android/src/org/strongswan/android/utils/Utils.java @@ -0,0 +1,40 @@ +/* + * Copyright (C) 2014 Tobias Brunner + * Hochschule fuer Technik Rapperswil + * + * This program is free software; you can redistribute it and/or modify it + * under the terms of the GNU General Public License as published by the + * Free Software Foundation; either version 2 of the License, or (at your + * option) any later version. See <http://www.fsf.org/copyleft/gpl.txt>. + * + * This program is distributed in the hope that it will be useful, but + * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY + * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License + * for more details. + */ + +package org.strongswan.android.utils; + + +public class Utils +{ + static final char[] HEXDIGITS = "0123456789abcdef".toCharArray(); + + /** + * Converts the given byte array to a hexadecimal string encoding. + * + * @param bytes byte array to convert + * @return hex string + */ + public static String bytesToHex(byte[] bytes) + { + char[] hex = new char[bytes.length * 2]; + for (int i = 0; i < bytes.length; i++) + { + int value = bytes[i]; + hex[i*2] = HEXDIGITS[(value & 0xf0) >> 4]; + hex[i*2+1] = HEXDIGITS[ value & 0x0f]; + } + return new String(hex); + } +} |