java代码如下:private String genRandom(int i)
{
byte abyte0[] = new byte[i];
for(int j = 0; j < i; j++)
{
Integer integer = new Integer((int)((float)Math.random() * 57F) + 65);
abyte0[j] = integer.byteValue();
} return new String(abyte0);
}要求转成C#代码.
注:java的byte与C#的sbyte才是相对应的。
难点:return new String(abyte0); 就卡在这句上。我看了下java的new String(byte bytes[])的具体实现,但是不知道C#中如何实现这句。
别跟我ToString(),Convert之类的,早就试过了。
{
byte abyte0[] = new byte[i];
for(int j = 0; j < i; j++)
{
Integer integer = new Integer((int)((float)Math.random() * 57F) + 65);
abyte0[j] = integer.byteValue();
} return new String(abyte0);
}要求转成C#代码.
注:java的byte与C#的sbyte才是相对应的。
难点:return new String(abyte0); 就卡在这句上。我看了下java的new String(byte bytes[])的具体实现,但是不知道C#中如何实现这句。
别跟我ToString(),Convert之类的,早就试过了。
.NET中二进制数据一般用byte[]
unsafe
{
// Null terminated ASCII characters in an sbyte array
String szAsciiUpper = null;
sbyte[] sbArr1 = new sbyte[] { 0x41, 0x42, 0x43, 0x00 };
// Instruct the Garbage Collector not to move the memory
fixed(sbyte* pAsciiUpper = sbArr1)
{
szAsciiUpper = new String(pAsciiUpper);
}
String szAsciiLower = null;
sbyte[] sbArr2 = { 0x61, 0x62, 0x63, 0x00 };
// Instruct the Garbage Collector not to move the memory
fixed(sbyte* pAsciiLower = sbArr2)
{
szAsciiLower = new String(pAsciiLower, 0, sbArr2.Length);
}
// Prints "ABC abc"
Console.WriteLine(szAsciiUpper + " " + szAsciiLower); // Compare Strings - the result is true
Console.WriteLine("The Strings are equal when capitalized ? " +
(String.Compare(szAsciiUpper.ToUpper(), szAsciiLower.ToUpper())==0?"true":"false") ); // This is the effective equivalent of another Compare method, which ignores case
Console.WriteLine("The Strings are equal when capitalized ? " +
(String.Compare(szAsciiUpper, szAsciiLower, true)==0?"true":"false") );
}