DZone Snippets is a public source code repository. Easily build up your personal collection of code snippets, categorize them with tags / keywords, and share them with the world

Katarina has posted 8 posts at DZone. View Full User Profile

To Decode String From Hex

08.28.2010
| 3072 views |
  • submit to reddit
        // To decode string from hex

	public static String decodeStringFromHex(String hexText) {

		String decodedText = null;
		String chunk = null;

		if (hexText != null && hexText.length() > 0) {
			int numBytes = hexText.length() / 2;

			byte[] rawToByte = new byte[numBytes];
			int offset = 0;
			// int bCounter = 0;
			for (int i = 0; i < numBytes; i++) {
				chunk = hexText.substring(offset, offset + 2);
				offset += 2;
				rawToByte[i] = (byte) (Integer.parseInt(chunk, 16) & 0x000000FF);
			}
			decodedText = new String(rawToByte);
		}
		return decodedText;
	}