最近项目中对未登录用户昵称命名规则使用到了这一需求,之前没遇到到过,在网上找了下发现基本都是Java和C#或者C的,不过有十进制转十六进制的,所以拿来参考了下,直接写了个分类以供有需要的人参考或直接使用就行,代码如下:
.h
+ (NSString *)ToHex:(long long)convert;
.m
+ (NSString *)ToHex:(long long)convert
{
NSString * ruleValue;
NSString * string = @"";
long long temp;
for (int i = 0; i<9; i++)
{
temp = convert%32;
convert = convert/32;
switch (temp)
{
case 10:
ruleValue =@"A";break;
case 11:
ruleValue =@"B";break;
case 12:
ruleValue =@"C";break;
case 13:
ruleValue =@"D";break;
case 14:
ruleValue =@"E";break;
case 15:
ruleValue =@"F";break;
case 16:
ruleValue =@"G";break;
case 17:
ruleValue =@"H";break;
case 18:
ruleValue =@"I";break;
case 19:
ruleValue =@"J";break;
case 20:
ruleValue =@"K";break;
case 21:
ruleValue =@"L";break;
case 22:
ruleValue =@"M";break;
case 23:
ruleValue =@"N";break;
case 24:
ruleValue =@"O";break;
case 25:
ruleValue =@"P";break;
case 26:
ruleValue =@"Q";break;
case 27:
ruleValue =@"R";break;
case 28:
ruleValue =@"S";break;
case 29:
ruleValue =@"T";break;
case 30:
ruleValue =@"U";break;
case 31:
ruleValue =@"V";break;
default:
ruleValue = [NSString stringWithFormat:@"%lld",temp];
}
string = [ruleValue stringByAppendingString:string];
if (convert == 0)
{
break;
}
}
return string;
}