public static string Decode(string str)
{
if (str == null || str == "") return "";
//取得随机数,第一位为随机数
int randomNum = int.Parse(str.Substring(0, 1));
string decoded = "";
for (int i = 1; i < str.Length; i += 4)
{
int charAsc = Convert.ToInt32(str.Substring(i, 4), 16);
decoded += (char)(charAsc - randomNum);
} return decoded;
}先谢谢大家!
{
if (str == null || str == "") return "";
//取得随机数,第一位为随机数
int randomNum = int.Parse(str.Substring(0, 1));
string decoded = "";
for (int i = 1; i < str.Length; i += 4)
{
int charAsc = Convert.ToInt32(str.Substring(i, 4), 16);
decoded += (char)(charAsc - randomNum);
} return decoded;
}先谢谢大家!
{
if ($str == null || $str == "") return "";
//取得随机数,第一位为随机数
$randomNum = intval(substr($str,0, 1));
$decoded = "";
for (int $i = 1; $i < strlen(str); $i += 4)
{
$charAsc =intval(substr($str,i, 4));
decoded .= ($charAsc - $randomNum);
} return $decoded;
}
int charAsc = Convert.ToInt32(str.Substring(i, 4), 16);
中的str.Substring(i, 4)是十六进制字符串,而非十进制串。
所以
$charAsc =intval(substr($str,i, 4));
应改为
$charAsc = hexdec(substr($str,i, 4));
的确是这样的我是这样写的function decode($str)
{
$randomnum=substr($str,0,1);
$decoded="";
$len=strlen($str)*1;
for($i=1;$i<$len;$i=$i+4)
{
$charAsc = hexdec(substr($str,$i,4));
$decoded .= chr($charAsc-$randomnum);
}
return $decoded;
}