Manually Convert hex to bin & bin to hex

Source: Internet
Author: User
Back up the old post and pull it ,:)
---------------------------------------------------------------------------
Hey, I simply wrote hex/bin both positive and negative conversions. Maybe I can copy my own program later. HIA

//: Hexbinexchangeclass. Java

//-----------------------------------------------//
// Hex to bin & bin to hex class //
// Freedebug (c) 2003-12-11 //
//-----------------------------------------------//
Import java. util .*;

Class hexbinexchange {

// Change a num from hex to bin
Public static string hex2bin (string shex ){
String stmp = "";
Stringbuffer sbresult = new stringbuffer ();
For (INT I = 0; I <shex. Length (); I ++ ){
Stmp = shex. substring (I, I + 1 );
For (Int J = 0; j <16; j ++ ){
If (stmp. comparetoignorecase (scode [J] [0]) = 0 ){
Sbresult. append (scode [J] [1]);
Break;
}
}
}
Return sbresult. tostring ();
}

// Chage a num from bin to hex
Public static string bin2hex (string sbin ){

// Count how many zero will fill in the front of sbin
Int ifillzero = sbin. Length () % 4;
If (ifillzero! = 0) ifillzero = 4-ifillzero;

// Fill zero in the front of sbin
Stringbuffer sbtmp = new stringbuffer ();
For (INT I = 0; I <ifillzero; I ++) sbtmp. append ("0 ");
Sbin = sbtmp. append (sbin). tostring ();

// Okey, just like hex2bin fuction
String stmp = "";
Stringbuffer sbresult = new stringbuffer ();
For (INT I = 0; I <sbin. Length (); I + = 4 ){
Stmp = sbin. substring (I, I + 4 );
For (Int J = 0; j <16; j ++ ){
If (stmp. comparetoignorecase (scode [J] [1]) = 0 ){
Sbresult. append (scode [J] [0]);
Break;
}
}
}
Return sbresult. tostring ();
}

// A code table for exchange
Private Static string [] [] scode = {
{"0", "0000 "},
{"1", "0001 "},
{"2", "0010 "},
{"3", "0011 "},
{"4", "0100 "},
{"5", "0101 "},
{"6", "0110 "},
{"7", "0111 "},
{"8", "1000 "},
{"9", "1001 "},
{"A", "1010 "},
{"B", "1011 "},
{"C", "1100 "},
{"D", "1101 "},
{"E", "1110 "},
{"F", "1111 "}};
}

Public class hexbinexchangeclass {

Public static void main (string [] ARGs ){
System. Out. println (hexbinexchange. hex2bin ("70abc12d "));
System. Out. println (hexbinexchange. bin2hex ("1110000101010111100000100101101 "));
}
}

///:~

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.