Java hexadecimal conversion helper classes (binary, decimal, hexadecimal, String, byte [] conversion)

Source: Internet
Author: User
Package COM. mas. FHK. util; import Java. io. bytearrayinputstream; import Java. io. bytearrayoutputstream; import Java. io. ioexception; import Java. io. objectinputstream; import Java. io. objectoutputstream; import Java. io. serializable; import Java. io. unsupportedencodingexception; import Java. security. messagedigest; import Java. security. nosuchalgorithmexception; import Java. text. simpledateformat; import Java. UT Il. calendar; import Java. util. date; import Java. util. gregoriancalendar; // The help class public class convertutil {public final static char [] btoa = "0123456789 abcdef ". tochararray (); Private convertutil () {} public static string getnowdate () {try {Java. util. calendar c = Java. util. calendar. getinstance (); Java. text. simpledateformat F = new Java. text. simpledateformat ("mm dd hh: mm"); Return F. format (C. gettime ());} Catch (exception e) {Throw E ;}} /*** convert the int value to a 4-digit hexadecimal format * @ Param S * @ return */public static string format4hex (int n) {try {string STR = integer. tohexstring (n); int Len = Str. length (); string hexstr = ""; for (INT I = Len; I <4; I ++) {if (I = Len) hexstr = "0 "; else hexstr = hexstr + "0";} return hexstr + Str. touppercase () ;}catch (exception e) {Throw e ;}/ *** convert binary to a hexadecimal string * @ Param binarystr * @ return */Public Static string bintohex (string binstr) {try {int Len = binstr. length ()/4; string res = ""; for (INT I = 0; I <Len; I ++) {char [] CH = new char [4]; binstr. getchars (I * 4, I * 4 + 4, ch, 0); Res = res + switchs (trans1 (CH);} return res;} catch (exception E) {Throw e ;}} public static int trans1 (char [] ch) {try {int sum = 0; For (INT I = 0; I <4; I ++) {int y = 8; If (CH [I] = '1') {for (Int J = 1; j <= I; j ++) {Y = y/2;} sum = sum + Y;} return sum;} catch (exception e) {Throw E;} public static string switchs (int I) {try {string S = ""; Switch (I) {case 10: S = "A"; break; Case 11: S = "B"; break; Case 12: S = "C"; break; Case 13: S = "D"; break; Case 14: S = "e"; break; Case 15: S = "F "; default: S = "" + I;} return s;} catch (exception e) {Throw E ;}} /*** convert the hexadecimal format to binary format * @ Param Str * @ return */ Public static string hextobin (string Str) {try {string resultstr = ""; string str2 = ""; for (INT I = 0; I <Str. length (); I ++) {Switch (Str. substring (I, I + 1 ). touppercase () {Case "0": str2 = "0000"; break; Case "1": str2 = "0001"; break; Case "2 ": str2 = "0010"; break; Case "3": str2 = "0011"; break; Case "4": str2 = "0100"; break; Case "5 ": str2 = "0101"; break; Case "6": str2 = "0110"; bre AK; Case "7": str2 = "0111"; break; Case "8": str2 = "1000"; break; Case "9": str2 = "1001 "; break; Case "A": str2 = "1010"; break; Case "B": str2 = "1011"; break; Case "C": str2 = "1100 "; break; Case "D": str2 = "1101"; break; Case "E": str2 = "1110"; break; Case "F": str2 = "1111 "; break;} resultstr + = str2;} return resultstr;} catch (exception e) {Throw E;}/*** convert ASCII code to hexadecimal format * @ Param Ch * @ Return */public static string asctohex (string ch) {try {char [] chararr = CH. tochararray (); string STR = ""; for (INT I = 0; I <chararr. length; I ++) {int A = (INT) chararr [I]; // ASCII STR + = integer. tostring (A, 16); // hexadecimal} return Str. touppercase () ;}catch (exception e) {Throw e ;}} /*** convert the hexadecimal string to ASCII * @ Param hexstr * @ return */public static string hextoasc (string HEX) {try {stringbuilder ascstr = ne W stringbuilder (); stringbuilder temp = new stringbuilder (); // convert each of the two to a group of ASCII characters for (INT I = 0; I 

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.