Handmade HEX to BIN & BIN TO HEX

zhaozj2021-02-16  71

Back up the old post,:) ------------------------------------------------------------------------------------------------------------------------------------------------------------------ -------------------------------- 嘿Can copy your own procedure, Hia 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

STMP = Shex.Substring (i, i 1);

For (int J = 0; j <16; j ) {

IF (stmp.comparetoirenorecase (scode [j]) == 0) {

SBRESULT.APPEND (Scode [J]);

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 - IFILLLZERO;

// Fill Zero in The Front of Sbin

StringBuffer sbtmp = new stringbuffer ();

For (int i = 0; i

Sbin = sbtmp.Append (sbin) .tostring ();

// OKey, Just Like Hex2bin FUCTION

String stmp = "";

StringBuffer sbresult = new stringbuffer ();

For (int i = 0; i

STMP = sbin.substring (i, i 4);

For (int J = 0; j <16; j ) {

IF (stmp.comparetoirenorecase (scode [j]) == 0) {

SBRESULT.APPEND (Scode [J]);

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 ("11100001010101101");

}

}

/ //: ~

转载请注明原文地址:https://www.9cbs.com/read-17237.html

New Post(0)