我想从十六进制编码的字符串中获取十进制值,例如:A-> 10,B-> 11等.我编码如下
NSString *tempNumber;
tempNumber=number.text;
NSScanner *scanner=[NSScanner scannerWithString:tempNumber];
unsigned int temp;
[scanner scanHexInt:temp];
NSLog(@"%@:%d",tempNumber,temp);
Run Code Online (Sandbox Code Playgroud)
但NSLog输出不是我所知道的..
2012-01-24 01:34:57.703 TestingUnit[34861:f803] 4:-1073750472
2012-01-24 01:35:01.133 TestingUnit[34861:f803] 6:-1073750408
2012-01-24 01:35:01.983 TestingUnit[34861:f803] 2:-1073750408
2012-01-24 01:35:02.414 TestingUnit[34861:f803] 1:-1073750408
Run Code Online (Sandbox Code Playgroud)
谁能告诉我怎么解决这个问题????
最近我在iOS上做了一个项目,我创建了一个类,即YellowTileView
我想做点什么,比如我点击按钮,会显示一个新的图块
-(IBAction)ShowImage:(id)sender
{
YellowTileView *yt=[[YellowTileView alloc] initWithFrame:CGRectMake(0, 0, 60, 80)];
[self.view addSubview:yt];
}
Run Code Online (Sandbox Code Playgroud)
这对我来说很好.但下一步是采用另一种方法随机化的整数.
我的问题是我可以自己重新定义/创建方法initWithFrame
吗?如果是的话,我怎么能这样做,因为我在课堂上有一些绘图代码会有任何问题YellowTileView
吗?
我编写了以下方法将Hex字符串转换为int:
-(long)intFromHexString:(NSString*) string
{
char tempChar;
int temp;
tempChar=[string characterAtIndex:[string length]-1];
temp=strtol(&tempChar, NULL, 16);
NSLog(@"***>%c = %i",tempChar,temp);
return temp;
}
Run Code Online (Sandbox Code Playgroud)
大部分时间它都能正常工作,但有时会遇到这样的大麻烦:
2012-02-10 01:09:28.516 GameView[7664:f803] ***>7 = 7
2012-02-10 01:09:28.517 GameView[7664:f803] ***>7 = 7
2012-02-10 01:09:28.518 GameView[7664:f803] ***>D = 13
2012-02-10 01:09:28.519 GameView[7664:f803] ***>5 = 5
2012-02-10 01:09:28.520 GameView[7664:f803] ***>5 = 5
2012-02-10 01:09:28.520 GameView[7664:f803] ***>D = 13
2012-02-10 01:09:28.521 GameView[7664:f803] ***>4 = 4
2012-02-10 01:09:28.522 GameView[7664:f803] ***>4 = 4
2012-02-10 01:09:28.522 GameView[7664:f803] ***>5 = 5
2012-02-10 01:09:28.523 GameView[7664:f803] ***>4 = …
Run Code Online (Sandbox Code Playgroud)