-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathcommon.func.php
151 lines (142 loc) · 3.85 KB
/
common.func.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
<?php
include('common.data.php');
function switch256to900($arr) //将6个字节转换成5个码字,参数为包含最多6个元素的数组;
{
$dec=0;
$return = array();
if(count($arr)<6) return $arr; //如果传递数组 不足6个元素,返回原来数组;
if(count($arr)>6) { echo "参数错误!"; exit(); } //如果传递数组超过6个元素,错误提示,终止程序;
foreach ($arr as $key=>$val)
{
$dec = bcadd($dec, bcmul( $val,Exponentiation(256,5-$key)));
}
for($i=0; $i < 5; $i++)
{
$return[$i]= bcmod($dec,900);
$dec= bcdiv($dec , 900);
}
return array_reverse($return);
}
function switch10to900($arr) //将44位十进制数字转换成15个码字,参数为包含最多44个元素的数组;中前导位1;
{
// $str="000213298174000"; //测试数据;
//$arr = str_split($str);
array_unshift($arr,"1");
$dec = implode($arr);
$length = intval((strlen($dec)-1)/3)+1; //码字长度;
for($i=0; $i < $length; $i++)
{
$return[$i]= bcmod($dec,900);
$dec= bcdiv($dec , 900);
//echo $return[$i]."dec:".$dec."<br />";
}
return array_reverse($return);
}
function Exponentiation($x,$s) //幂运算,x的s次幂;
{
$r=1.0;
if($x==1 || $x==0 || $s ==0) return 1;
for($i=0;$i<$s;$i++)
$r = bcmul($r,$x,50);
return $r;
}
function is_number($str)
{
if(strlen($str)>1)
{
$arr = str_split($str);
$strint = '';
foreach ($arr as $key => $value)
{
$strint .= intval($value).'';
}
if($str == $strint) return true;
else return false;
}
else
{
if(trim($str) === '') return false;
$strint = intval($str).'';
if($str == $strint) return true;
else return false;
}
}
function asciitotc($str, $mode)
{
$mix = array(48,49,50,51,52,53,54,55,56,57,38,35,43,37,61,94); //混合型字符ASCII列表
$Punc = array(59,60,62,64,91,92,93,95,96,126,33,10,34,124,40,41,63,123,125,39); //标点型字符ASCII列表;
$rep = array(13,09,44,58,45,46,36,47,42); //混合型与标点型 重复字符列表;
$mixcode = array(0,1,2,3,4,5,6,7,8,9,10,15,20,21,23,24);
$punccode = array(0,1,2,3,4,5,6,7,8,9,10,15,20,21,23,24,25,26,27,28);
$repcode = array(11,12,13,14,16,17,18,19,22);
$ascii = ord($str);
if($mode == 'Alpha')
{
if($ascii == 32) $ret = 26;
else $ret = ord($str) - 65 ;
}
else if($mode == 'Lower')
{
if($ascii == 32) $ret = 26;
else $ret = ord($str) - 97 ;
}
else if($mode == 'Mix')
{
if($ascii == 32) $ret = 26;
else if(in_array($ascii,$mix))
{
$key = array_keys($mix, $ascii);
$ret = $mixcode[$key['0']];
}
else if(in_array($ascii,$rep))
{
$key = array_keys($rep, $ascii);
$ret = $repcode[$key['0']];
}
}
else if($mode == "Punc")
{
if(in_array($ascii,$Punc))
{
$key = array_keys($Punc, $ascii);
$ret = $punccode[$key[0]];
}
else if(in_array($ascii,$rep))
{
$key = array_keys($rep, $ascii);
$ret = $repcode[$key];
}
}
return $ret;
}
function bushu($m)
{
return 929 - $m;
}
function ErrorCorrection($codearray,$correctleval) //数据,纠错级别;
{
$array = array();
$k = Exponentiation(2, $correctleval+1); //纠错码个数;
$c = array_pad($array, $k, 0); //纠错码数组
$t1 = $t2 = $t3 = 0; //临时变量
$a = geta($correctleval); //根据纠错级别,得出的g(x)展开后各项的系数;
foreach($codearray as $key => $data)
{
$t1 = ( $data + $c[$k - 1] ) % 929 ;
// echo " t1:$t1\n";
foreach($c as $keyc => $correct)
{
$t2 = ($t1 * $a[$k - $keyc - 1]) % 929;
$t3 = 929 - $t2;
if($k - $keyc - 1 == 0)
$m = $t3;
else $m = $c[$k - $keyc - 2] + $t3;
$c[$k - $keyc - 1] = $m % 929;
//echo "t2:".$t2." t3:".$t3." c[".($k - $keyc - 1)."]:".$c[$k - $keyc - 1]."\n";
}
}
$c = array_map("bushu", array_reverse($c));
//print_r($c);echo "<hr />";
return $c;
}
?>