JS代码转换成C#
functionstr2binl(str){varbin=Array();varmask=(1<<chrsz)-1;for(vari=0;i<str.length*chr...
function str2binl(str)
{
var bin = Array();
var mask = (1 << chrsz) - 1;
for(var i = 0; i < str.length * chrsz; i += chrsz)
bin[i>>5] |= (str.charCodeAt(i / chrsz) & mask) << (i%32);
return bin;
}
/*
* Convert an array of little-endian words to a string
*/
function binl2str(bin)
{
var str = "";
var mask = (1 << chrsz) - 1;
for(var i = 0; i < bin.length * 32; i += chrsz)
str += String.fromCharCode((bin[i>>5] >>> (i % 32)) & mask);
return str;
}
请问上面的代码转换成C# 展开
{
var bin = Array();
var mask = (1 << chrsz) - 1;
for(var i = 0; i < str.length * chrsz; i += chrsz)
bin[i>>5] |= (str.charCodeAt(i / chrsz) & mask) << (i%32);
return bin;
}
/*
* Convert an array of little-endian words to a string
*/
function binl2str(bin)
{
var str = "";
var mask = (1 << chrsz) - 1;
for(var i = 0; i < bin.length * 32; i += chrsz)
str += String.fromCharCode((bin[i>>5] >>> (i % 32)) & mask);
return str;
}
请问上面的代码转换成C# 展开
1个回答
2013-03-29
展开全部
private int chrsz = 8;
private int[] str2bin(string str)
{
int count = (int)Math.Ceiling((decimal)(str.Length * chrsz >> 5));
int[] bin = new int[count];
char[] c = str.ToCharArray();
int mask = (1 << chrsz) - 1;
for (int i = 0; i < c.Length * chrsz; i += chrsz)
{
System.Text.ASCIIEncoding asciiEncoding = new System.Text.ASCIIEncoding();
int start = (int)Math.Floor((decimal)(i / chrsz));
int j = (int)asciiEncoding.GetBytes(c, start, 1)[0];
bin[i >> 5] |= (j & mask) << (i % 32);
}
return bin;
} private string bin2str(int[]bin)
{
string str = string.Empty;
int mask = (1 << chrsz) - 1;
for (int i = 0; i < bin.Length * 32; i += chrsz)
{
int j = (bin[i>>5] >> (i % 32)) & mask;
if (j >= 0 && j <= 255)
{
System.Text.ASCIIEncoding asciiEncoding = new System.Text.ASCIIEncoding();
byte[] byteArray = new byte[] { (byte)j };
str += asciiEncoding.GetString(byteArray);
}
}
return str;
}大概就这么个意思了。。。应该会有错误,不是很严谨,自己改去吧。。。还有,这类的代码,经常在网站木马上看到。。。
private int[] str2bin(string str)
{
int count = (int)Math.Ceiling((decimal)(str.Length * chrsz >> 5));
int[] bin = new int[count];
char[] c = str.ToCharArray();
int mask = (1 << chrsz) - 1;
for (int i = 0; i < c.Length * chrsz; i += chrsz)
{
System.Text.ASCIIEncoding asciiEncoding = new System.Text.ASCIIEncoding();
int start = (int)Math.Floor((decimal)(i / chrsz));
int j = (int)asciiEncoding.GetBytes(c, start, 1)[0];
bin[i >> 5] |= (j & mask) << (i % 32);
}
return bin;
} private string bin2str(int[]bin)
{
string str = string.Empty;
int mask = (1 << chrsz) - 1;
for (int i = 0; i < bin.Length * 32; i += chrsz)
{
int j = (bin[i>>5] >> (i % 32)) & mask;
if (j >= 0 && j <= 255)
{
System.Text.ASCIIEncoding asciiEncoding = new System.Text.ASCIIEncoding();
byte[] byteArray = new byte[] { (byte)j };
str += asciiEncoding.GetString(byteArray);
}
}
return str;
}大概就这么个意思了。。。应该会有错误,不是很严谨,自己改去吧。。。还有,这类的代码,经常在网站木马上看到。。。
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询