-
iOS中NSString转换成HEX(十六进制)-NSData转换成int
- NSString *str = @"0xff055008";
- unsigned long red = strtoul([str UTF8String],0,16);
- unsigned long red = strtoul([@"0x6587" UTF8String],0,0);
- NSLog(@"转换完的数字为:%lx",red);
- + (NSString *)stringFromHexString:(NSString *)hexString {
-
- charchar *myBuffer = (charchar *)malloc((int)[hexString length] / 2 + 1);
- bzero(myBuffer, [hexString length] / 2 + 1);
- for (int i = 0; i < [hexString length] - 1; i += 2) {
- unsigned int anInt;
- NSString * hexCharStr = [hexString substringWithRange:NSMakeRange(i, 2)];
- NSScanner * scanner = [[[NSScanner alloc] initWithString:hexCharStr] autorelease];
- [scanner scanHexInt:&anInt];
- myBuffer[i / 2] = (char)anInt;
- }
- NSString *unicodeString = [NSString stringWithCString:myBuffer encoding:4];
- NSLog(@"------字符串=======%@",unicodeString);
- return unicodeString;
-
-
- }
-
-
- + (NSString *)hexStringFromString:(NSString *)string{
- NSData *myD = [string dataUsingEncoding:NSUTF8StringEncoding];
- Byte *bytes = (Byte *)[myD bytes];
- NSString *hexStr=@"";
- for(int i=0;i<[myD length];i++)
-
- {
- NSString *newHexStr = [NSString stringWithFormat:@"%x",bytes[i]&0xff];
-
- if([newHexStr length]==1)
-
- hexStr = [NSString stringWithFormat:@"%@0%@",hexStr,newHexStr];
-
- else
-
- hexStr = [NSString stringWithFormat:@"%@%@",hexStr,newHexStr];
- }
- return hexStr;
- }
- int i = 1;
- NSData *data = [NSData dataWithBytes: &i length: sizeof(i)];
- int i;
- [data getBytes: &i length: sizeof(i)];
-
相关阅读:
JS焦点图手动切换
Cookie
javaScript字符串方法(常用的)
javaScript函数
JavaScript条件语句和循环语句
JavaScript基本的数据类型
数据库的四个基本语句
CSS文本样式
HTML图片
C#集合
-
原文地址:https://www.cnblogs.com/Free-Thinker/p/9155380.html
Copyright © 2020-2023
润新知