unit BinHexTools;
interface
function HexToBin(HexNr : string): string; //把十六进制字符串转换为二进制字符串
function HexCharToInt(HexToken : char):Integer; //转换一个十六进制字符为整数
function HexCharToBin(HexToken : char): string;//转换一个十六进制字符为二进制字符串
function pow(base, power: integer): integer;//指数函数
function BinStrToInt(BinStr : string) : integer;//把二进制字符串转换为整数
function DecodeSMS7Bit(PDU : string):string;// 解码一个7-bit SMS (GSM 03.38) 为ASCII码
function ReverseStr(SourceStr : string) : string; //反转一个字符串
implementation
uses sysutils, dialogs;
interface
function HexToBin(HexNr : string): string; //把十六进制字符串转换为二进制字符串
function HexCharToInt(HexToken : char):Integer; //转换一个十六进制字符为整数
function HexCharToBin(HexToken : char): string;//转换一个十六进制字符为二进制字符串
function pow(base, power: integer): integer;//指数函数
function BinStrToInt(BinStr : string) : integer;//把二进制字符串转换为整数
function DecodeSMS7Bit(PDU : string):string;// 解码一个7-bit SMS (GSM 03.38) 为ASCII码
function ReverseStr(SourceStr : string) : string; //反转一个字符串
implementation
uses sysutils, dialogs;
function HexCharToInt(HexToken : char):Integer;
begin
{if HexToken>#97 then HexToken:=Chr(Ord(HexToken)-32);
{ use lowercase aswell }
Result:=0;
if (HexToken>#47) and (HexToken<#58) then { 9 }
Result:=Ord(HexToken)-48
else if (HexToken>#64) and (HexToken<#71) then { F }
Result:=Ord(HexToken)-65 + 10;
end;
function HexCharToBin(HexToken : char): string;
var DivLeft : integer;
begin
begin
{if HexToken>#97 then HexToken:=Chr(Ord(HexToken)-32);
{ use lowercase aswell }
Result:=0;
if (HexToken>#47) and (HexToken<#58) then { 9 }
Result:=Ord(HexToken)-48
else if (HexToken>#64) and (HexToken<#71) then { F }
Result:=Ord(HexToken)-65 + 10;
end;
function HexCharToBin(HexToken : char): string;
var DivLeft : integer;
begin
DivLeft:=HexCharToInt(HexToken); { first HEX->BIN }
Result:='';
{ Use reverse dividing }
repeat { Trick; divide by 2 }
if odd(DivLeft) then { result = odd ? then bit = 1 }
Result:='1'+Result { result = even ? then bit = 0 }
else
Result:='0'+Result;
DivLeft:=DivLeft div 2; { keep dividing till 0 left and length = 4 }
until (DivLeft=0) and (length(Result)=4); { 1 token = nibble = 4 bits }
end;
function HexToBin(HexNr : string): string;
{ only stringsize is limit of binnr }
var Counter : integer;
Result:='';
{ Use reverse dividing }
repeat { Trick; divide by 2 }
if odd(DivLeft) then { result = odd ? then bit = 1 }
Result:='1'+Result { result = even ? then bit = 0 }
else
Result:='0'+Result;
DivLeft:=DivLeft div 2; { keep dividing till 0 left and length = 4 }
until (DivLeft=0) and (length(Result)=4); { 1 token = nibble = 4 bits }
end;
function HexToBin(HexNr : string): string;
{ only stringsize is limit of binnr }
var Counter : integer;
begin
Result:='';
for Counter:=1 to length(HexNr) do
Result:=Result+HexCharToBin(HexNr[Counter]);
end;
function pow(base, power: integer): integer;
var counter : integer;
begin
Result:=1;
for counter:=1 to power do
Result:=Result*base;
end;
Result:='';
for Counter:=1 to length(HexNr) do
Result:=Result+HexCharToBin(HexNr[Counter]);
end;
function pow(base, power: integer): integer;
var counter : integer;
begin
Result:=1;
for counter:=1 to power do
Result:=Result*base;
end;
function BinStrToInt(BinStr : string) : integer;
var counter : integer;
begin
if length(BinStr)>16 then
raise ERangeError.Create(#13+BinStr+#13+
'is not within the valid range of a 16 bit binary.'+#13);
Result:=0;
for counter:=1 to length(BinStr) do
if BinStr[Counter]='1' then
Result:=Result+pow(2,length(BinStr)-counter);
end;
function DecodeSMS7Bit(PDU : string):string;
var OctetStr : string;
OctetBin : string;
Charbin : string;
PrevOctet: string;
Counter : integer;
Counter2 : integer;
begin
PrevOctet:='';
Result:='';
for Counter:=1 to length(PDU) do
begin
if length(PrevOctet)>=7 then { if 7 Bit overflow on previous }
begin
var OctetStr : string;
OctetBin : string;
Charbin : string;
PrevOctet: string;
Counter : integer;
Counter2 : integer;
begin
PrevOctet:='';
Result:='';
for Counter:=1 to length(PDU) do
begin
if length(PrevOctet)>=7 then { if 7 Bit overflow on previous }
begin
if BinStrToInt(PrevOctet)<>0 then
Result:=Result+Chr(BinStrToInt(PrevOctet))
else Result:=Result+' ';
PrevOctet:='';
end;
if Odd(Counter) then { only take two nibbles at a time }
begin
OctetStr:=Copy(PDU,Counter,2);
OctetBin:=HexToBin(OctetStr);
Charbin:=''; 做梦梦见老鼠
for Counter2:=1 to length(PrevOctet) do
Charbin:=Charbin+PrevOctet[Counter2];
Result:=Result+Chr(BinStrToInt(PrevOctet))
else Result:=Result+' ';
PrevOctet:='';
end;
if Odd(Counter) then { only take two nibbles at a time }
begin
OctetStr:=Copy(PDU,Counter,2);
OctetBin:=HexToBin(OctetStr);
Charbin:=''; 做梦梦见老鼠
for Counter2:=1 to length(PrevOctet) do
Charbin:=Charbin+PrevOctet[Counter2];
for Counter2:=1 to 7-length(PrevOctet) do
Charbin:=OctetBin[8-Counter2+1]+Charbin;
if BinStrToInt(Charbin)<>0 then Result:=Result+Chr(BinStrToInt(CharBin))
else Result:=Result+' ';
PrevOctet:=Copy(OctetBin,1,length(PrevOctet)+1);
end;
end;
end;
function ReverseStr(SourceStr : string) : string;
var Counter : integer;
begin
Result:='';
for Counter:=1 to length(SourceStr) do
Result:=SourceStr[Counter]+Result;
end;
for Counter:=1 to length(SourceStr) do
Result:=SourceStr[Counter]+Result;
end;
16进制的就是STRING类型的!
要完成此效果需要两个步骤
第一步:把如下代码加入到 <head> 区域中
<SCRIPT LANGUAGE= "JavaScript ">
<!-- Begin
binary_numbers = new Array( "0000 ", "0001 ", "0010 ", "0011 ", "0100 ", "0101 ",
"0110 ", "0111 ", "1000 ", "1001 ", "1010 ", "1011 ", "1100 ", "1101 ", "1110 ", "1111 ");
第一步:把如下代码加入到 <head> 区域中
<SCRIPT LANGUAGE= "JavaScript ">
<!-- Begin
binary_numbers = new Array( "0000 ", "0001 ", "0010 ", "0011 ", "0100 ", "0101 ",
"0110 ", "0111 ", "1000 ", "1001 ", "1010 ", "1011 ", "1100 ", "1101 ", "1110 ", "1111 ");
function toBinary(High, Low) {
var hiHex = "ABCDEF ";
if (Low < 10 ) {
LowNib = Low;
}
else {
LowNib = 10 + hiHex.indexOf(Low);
}
if (High < 10 ) {
HighNib = High;
}
else {
HighNib = 10 + hiHex.indexOf(High);
}
eight_bits = binary_numbers[HighNib] + " " + binary_numbers[LowNib];
return eight_bits;
}
function Dec2Hex(Decimal) {
var hexChars = "0123456789ABCDEF ";
var a = Decimal % 16;
var b = (Decimal - a)/16;
hex = " " + hexChars.charAt(b) + hexChars.charAt(a);
L = hexChars.charAt(a);
H = hexChars.charAt(b);
return hex;
}
var symbols = " !\ "#$%& '()*+ '-./0123456789:; <=> ?@ ";
function toAscii () {
var loAZ = "abcdefghijklmnopqrstuvwxyz ";
return eight_bits;
}
function Dec2Hex(Decimal) {
var hexChars = "0123456789ABCDEF ";
var a = Decimal % 16;
var b = (Decimal - a)/16;
hex = " " + hexChars.charAt(b) + hexChars.charAt(a);
L = hexChars.charAt(a);
H = hexChars.charAt(b);
return hex;
}
var symbols = " !\ "#$%& '()*+ '-./0123456789:; <=> ?@ ";
function toAscii () {
var loAZ = "abcdefghijklmnopqrstuvwxyz ";
symbols+= UpperCase();
symbols+= "[\\]^_` ";
symbols+= loAZ;
symbols+= "{|}~ ";
var loc;
loc = symbols.indexOf(document.asciiform.Input.value);
if (loc > -1) {
Ascii_Decimal = 32 + loc;
return (32 + loc);
}
return(0); // If not in range 32-126 return ZERO
}
function getAscii(some_value) {
Dec.value = toAscii();
Hex.value = Dec2Hex(toAscii());
symbols+= "[\\]^_` ";
symbols+= loAZ;
symbols+= "{|}~ ";
var loc;
loc = symbols.indexOf(document.asciiform.Input.value);
if (loc > -1) {
Ascii_Decimal = 32 + loc;
return (32 + loc);
}
return(0); // If not in range 32-126 return ZERO
}
function getAscii(some_value) {
Dec.value = toAscii();
Hex.value = Dec2Hex(toAscii());
document.asciiform.binary.value = toBinary(H, L);
}
// End -->
</script>
第二步:把如下代码加入到 <body> 区域中
<center>
<form name= "asciiform " onsubmit= "return false; ">
<table border=1> <tr> <td>
<center>
输入一个字符:
<input size=1 name=Input maxlength=1 onChange= "getAscii(this.value) ">
}
// End -->
</script>
第二步:把如下代码加入到 <body> 区域中
<center>
<form name= "asciiform " onsubmit= "return false; ">
<table border=1> <tr> <td>
<center>
输入一个字符:
<input size=1 name=Input maxlength=1 onChange= "getAscii(this.value) ">
<p>
<input type=button value= "显示 " onClick= "Dec.value=toAscii() ">
</center>
<p>
<input size=10 name=toDec onFocus= "this.blur() ">
ASCII码十进制输出 <br>
<input size=10 name=toHex onFocus= "this.blur() ">
ASCII码十六进制输出 <br>
<input size=10 name=binary onFocus= "this.blur() ">
ASCII码二进制输出
</td>
</tr>
</table>
</form>
<input type=button value= "显示 " onClick= "Dec.value=toAscii() ">
</center>
<p>
<input size=10 name=toDec onFocus= "this.blur() ">
ASCII码十进制输出 <br>
<input size=10 name=toHex onFocus= "this.blur() ">
ASCII码十六进制输出 <br>
<input size=10 name=binary onFocus= "this.blur() ">
ASCII码二进制输出
</td>
</tr>
</table>
</form>
发布评论