importFoundation extensionCharacter { functoInt()->Int { varintFromCharacter:Int=0 forscalarinString(self).unicodeScalars { intFromCharacter=Int(scalar.value) } returnintFromCharacter } } //MARK:十六进制-->十进制 funchex2dec(num:String)->Int { letstr=num.uppercaseString varsum=0 foriinstr.utf8{ sum=sum*16+Int(i)-48//0-9从48开始 ifi>=65{//A-Z从65开始,但有初始值10,所以应该是减去55 sum-=7 } } returnsum } //MARK:十进制-->十六进制 funcdec2hex(num:Int)->String{ returnString(format:"%0X",num) } //MARK:十进制->二进制 funcdec2bin(varnumber:Int)->String{ varstr="" whilenumber>0{ str="\(number%2)"+str number/=2 } returnstr } //MARK:二进制->十进制 funcbin2dec(num:String)->Int{ varsum=0 forccinnum.characters { sum=sum*2+cc.toInt() } returnsum }