《2022年怎么把一个字符串转换成进制数据 2.pdf》由会员分享,可在线阅读,更多相关《2022年怎么把一个字符串转换成进制数据 2.pdf(6页珍藏版)》请在taowenge.com淘文阁网|工程机械CAD图纸|机械工程制图|CAD装配图下载|SolidWorks_CaTia_CAD_UG_PROE_设计图分享下载上搜索。
1、怎么把一个字符串转换成16 进制数据 ,然后再把 16 进制数转成字符串 ? unit BinHexTools; interface function HexToBin(HexNr : string): string; / 把十六进制字符串转换为二进制字符串function HexCharToInt(HexToken : char):Integer; /转换一个十六进制字符为整数function HexCharToBin(HexToken : char): string;/ 转换一个十六进制字符为二进制字符串function pow(base, power: integer): integer
2、;/ 指数函数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; beg
3、in if HexToken#97 then HexToken:=Chr(Ord(HexToken)-32); use lowercase aswell Result:=0; if (HexToken#47) and (HexToken#64) and (HexTokenBIN 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 Res
4、ult:=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 名师资料总结 - - -精品资料欢迎下载 - - - - - - - - - - - - - - - - - - 名师精心整理 - - -
5、- - - - 第 1 页,共 6 页 - - - - - - - - - var Counter : integer; begin Result:=; for Counter:=1 to length(HexNr) do Result:=Result+HexCharToBin(HexNrCounter); end; function pow(base, power: integer): integer; var counter : integer; begin Result:=1; for counter:=1 to power do Result:=Result*base; end; fu
6、nction BinStrToInt(BinStr : string ) : integer; var counter : integer; begin if length(BinStr)16 then raise ERangeError.C reate(#13+BinStr+#13+ is not within the valid range of a 16 bit binary.+#13); Result:=0; for counter:=1 to leng th(BinStr) do if BinStrC ounter=1then Result:=Result+pow(2,leng th
7、(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 名师资料总结 - - -精品资料欢迎下载 - - - - - - - - - - - - -
8、 - - - - - 名师精心整理 - - - - - - - 第 2 页,共 6 页 - - - - - - - - - if length(PrevOctet)=7 then if 7 Bit overflow on previous begin if BinStrToInt(PrevOctet)0 then Result:=Result+C hr(BinStrToInt(PrevOctet) else Result:=R esult+; PrevOctet:=;end; if Odd(Counter) then only take two nibbles at a time begin
9、OctetStr:=Copy(PDU,Counter,2); OctetBin:=HexToBin(OctetStr); Charbin:=; for Counter2:=1 to leng th(PrevOctet) do Charbin:=Charbin+PrevOctetCounter2; for Counter2:=1 to 7-length(PrevOctet) do Charbin:=OctetBin8-Counter2+1+Charbin; if BinStrToInt(Charbin)0 then Result:=Result+Chr(BinStrToInt(C harBin)
10、 else Result:=R esult+; 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:=SourceStrC ounter+Result; end; 16 进制的就是 STRING 类型的!要完成此效果需要两个步骤第一步:把如下代码加入到
11、区域中 !- Begin 名师资料总结 - - -精品资料欢迎下载 - - - - - - - - - - - - - - - - - - 名师精心整理 - - - - - - - 第 3 页,共 6 页 - - - - - - - - - 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
12、 = 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_nu mbersHighNib + + binary_numbersLowNib; return eight_bits; function Dec2Hex(Decimal) var hexChars = 0123456789ABCDEF ; var a = De
13、cimal % 16; var b = (Decimal - a)/16; hex = + hexChars.charAt(b) + hexChars.charAt(a); L = hexC hars.charAt(a); H = hexChars.charAt(b); return hex; var symb ols = ! #$%& ()*+ -./0123456789:; ? ; function toAscii () var loAZ = abcdefghijklmnopqrstuvwxyz ; symbols+= loAZ.toUpperCase(); symbols+= _ ; s
14、ymbols+= loAZ; symbols+= | ; var loc; loc = symb ols.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 名师资料总结 - - -精品资料欢迎下载 - - - - - - - - - - - - - - - - - - 名师精心整理 - - - - - - - 第 4 页,共 6 页 - - - - - -
15、 - - - function getAscii(some_value) document.asciiform.toDec.value = toAscii(); document.asciiform.toHex.value = Dec2Hex(toAscii(); document.asciiform.binary.value = toBinary(H, L); / End - 第二步:把如下代码加入到 区域中 输入一个字符 : ASCII 码十进制输出 ASCII 码十六进制输出 ASCII 码二进制输出 function hexchange() var hex,dec; dec = par
16、seFloat(FKeyPad.ReadOut.value); hex = dec.toString(16); window.alert( 该十进制数转成十六进制数为:+ hex); function octchange() var oct,dec; dec = parseFloat(FKeyPad.ReadOut.value); 名师资料总结 - - -精品资料欢迎下载 - - - - - - - - - - - - - - - - - - 名师精心整理 - - - - - - - 第 5 页,共 6 页 - - - - - - - - - oct = dec.toS tring(8); window.alert( 该十进制数转成八进制数为:+ oct); function binchange() var bin,dec; dec = parseFloat(FKeyPad.ReadOut.value); bin = dec.toString(2); window.alert( 该十进制数转成二进制数为:+ bin); 名师资料总结 - - -精品资料欢迎下载 - - - - - - - - - - - - - - - - - - 名师精心整理 - - - - - - - 第 6 页,共 6 页 - - - - - - - - -