在C#中
string p = "help";
byte myByte = (byte)p[1];
In C# byte is a unsigned 8 bit integer (0-255)。如何在Objective C中实现相同的目标?
实际要求是,我想将以下用c#编写的代码段转换为目标c。我无法弄清楚的部分是字节转换。
static Int64 Encrypt(string p)
{
Int64 c = 31;
for (int i = 0; i < p.Length; i++)
c = c * 47 + (byte)p[i] % 97;
return c;
}
答案 0 :(得分:1)
与C#平等:
注意:所有字符都是8位。
int64_t Encrypt(NSString *p) {
const char *cString = [p cStringUsingEncoding: NSASCIIStringEncoding];
unsigned long length = strlen(cString);
int64_t c = 31;
for (int i = 0; i < length; i++)
c = c * 47 + cString[i] % 97;
return c;
}
NSString *p = @"help";
NSLog(@"p: %@", p);
int64_t c = Encrypt(p);
NSLog(@"c: lld", c);
NSLog输出:
p:p:help
c:152006240