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 "));
}
}
///:~