【发布时间】:2014-03-26 10:42:39
【问题描述】:
我只是想完成一些事情,我想将字符串从 ansi 转换为 utf8,反之亦然。
示例:to_ansi(1234) 预期结果:NLKJ
示例:to_utf8(NLKJ) 预期结果:1234
functions I currently have are:
function to_ansi($str)
{
$newString = "";
$reversedString = strrev($str);
for($i=0; $i < strlen($reversedString); $i++ ) {
$newString .= iconv(mb_detect_encoding(), 'UTF-8', chr(ord($reversedString[$i]) * 1.5));
}
return $newString;
}
function to_utf8($str)
{
$newString = "";
$reversedString = strrev($str);
for($i=0; $i < strlen($reversedString); $i++ ) {
$newString .= iconv(mb_detect_encoding(), 'UTF-8', chr(ord($reversedString[$i]) / 1.5));
}
return $newString;
}
使用上面我得到的那些函数
示例:to_ansi(1234) 结果:NLKI
示例:to_utf8(NLKJ) 结果:1224
其实我只是把vbs解释成PHP,原来的函数是:
Function ToAnsi(ByVal strPassword As String) As String
Dim strLetter As String
Dim strRevPass As String
Dim strNewPass As String
strRevPass = strReverse(strPassword)
strNewPass = ""
For a = 1 To Len(strRevPass)
strLetter = Mid$(strRevPass, a, 1)
strNewPass = strNewPass & Chr((Asc(strLetter) * 1.5))
Next a
Text2.Text = strNewPass
End Function
Function ToUTF8(ByVal strPassword As String)
Dim strLetter As String
Dim strRevPass As String
Dim strNewPass As String
strRevPass = strReverse(strPassword)
strNewPass = ""
For a = 1 To Len(strRevPass)
strLetter = Mid$(strRevPass, a, 1)
strNewPass = strNewPass & Chr(Asc(strLetter) / 1.5)
Next a
txtText3.Text = strNewPass
End Function
【问题讨论】:
-
为什么你认为 ANSI 中的 1234 应该 UTF-8 中的 NLKJ?