Java hexadecimal characters and byte array conversions __java

Source: Internet
Author: User
Tags stringbuffer

The recent project needs to convert a byte array to a hexadecimal number, which has been studied for a long time and shared


converting between byte arrays and hexadecimal strings

/** * Convert byte[] to hex * string. Here we can convert byte to int and then use integer.tohexstring (int) to convert into a 16-string. * @param src * @return/public String bytestohexstring (byte[] src) {StringBuilder StringBuilder = new Stringbuil
		Der ("");
		if (src = null | | | src.length <= 0) {return null;
			for (int i = 0; i < src.length i++) {int v = src[i] & 0xFF;
			String HV = integer.tohexstring (v);
			if (Hv.length () < 2) {stringbuilder.append (0);
		} stringbuilder.append (HV);
	return stringbuilder.tostring ();  /** * Convert hex string to byte[] * * @param hexstring * @return/public byte[] Hexstringtobytes (string
		hexstring) {if (hexstring = = NULL | | | hexstring.equals ("")) {return null;	}//hexstring = Hexstring.touppercase ();
		If it is uppercase int length = Hexstring.length ()/2;
		char[] Hexchars = Hexstring.tochararray ();
		Byte[] D = new Byte[length];
			for (int i = 0; i < length; i++) {int pos = i * 2; D[i] = (byte) (ChartobyTe (Hexchars[pos]) << 4 |
		Chartobyte (Hexchars[pos + 1]));
	return D; Private byte Chartobyte (char c) {return (byte) ' 0123456789abcdef '. IndexOf (c);//Return (byte) "0123456789ABCDEF".
	IndexOf (c); }



the conversion between a string and its corresponding UTF-8 encoding

/** * Convert UTF-8 code string to string, capable of handling kanji * @param s * @return/public static string Convertutf8tost
		Ring (String s) {if (s = = NULL | | s.equals ("")) {return null;

			try {s = s.touppercase ();
			int total = S.length ()/2;

			int pos = 0;
			byte[] buffer = new Byte[total];

				for (int i = 0; i < total; i++) {int start = i * 2;
				Buffer[i] = (byte) integer.parseint (s.substring (Start, start + 2), 16);
			pos++;

		return new String (buffer, 0, POS, "UTF-8");
		catch (Unsupportedencodingexception e) {e.printstacktrace ();
	return s; /** * Convert String to UTF code string, capable of handling kanji * * @param S * Original String * @return/public static St
		Ring ConvertStringToUTF8 (String s) {if (s = = NULL | | s.equals ("")) {return null;
		StringBuffer sb = new StringBuffer ();
			try {char C;
				for (int i = 0; i < s.length (); i++) {c = S.charat (i); Byte[] B = character.tostring (c). GetBytes ("UTF-8 ");  if (c >= 0 && C <= 255) {//123...9-A-Z for (int j = 0; J < B.length; J +) {int k =
						B[J];
						if (k < 0) k = 256;	Sb.append ("0000" +integer.tohexstring (k). toUpperCase ());
					Consider the case of ASICII code characters//Sb.append (Integer.tohexstring (k). toUpperCase ());
						} else {for (int j = 0; J < B.length; J + +) {int k = b[j];
						if (k < 0) k = 256;
					Sb.append (Integer.tohexstring (k). toUpperCase ());

		catch (Exception e) {e.printstacktrace ()}}};
	return sb.tostring (); }


converts a byte array to long

	 * * Byte[]->long/public
	final static long Getlong (byte[] buf, boolean ASC) {
		if (buf = null) {
  
   throw New IllegalArgumentException ("byte array is null!");
		}
		if (Buf.length > 8) {
			throw new IllegalArgumentException ("byte array size > 8!");
		}
		Long r = 0;
		if (ASC) for
			(int i = buf.length-1 i >= 0; i--) {
				R <<= 8;
				R |= (Buf[i] & 0x000000ff);
			}
		else for
			(int i = 0; i < buf.length i++) {
				R <<= 8;
				R |= (Buf[i] & 0x000000ff);
			}
		return r;
	}
  




Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.