2016-09-28 15:40:06 crystal300 阅读数 3363
  • PHP7字符串模块

    King老师精心打造的PHP7字符串实战课程,由浅入深的讲解了PHP中字符串模块,并介绍了一些常用的字符串相关操作,让我们一起开启字符串之旅吧~ 山哥出品,必属精品!

    15539 人正在学习 去看看 何山

swift 3.0 语法有一定变化,相应字符串截取变化也不小,所以重写了一下取颜色的方法如下:

    func transferStringToColor(colorStr:String) -> UIColor {
        
        var color = UIColor.red
        var cStr : String = colorStr.trimmingCharacters(in: CharacterSet.whitespacesAndNewlines).uppercased()
        
        if cStr.hasPrefix("#") {
            let index = cStr.index(after: cStr.startIndex)
            cStr = cStr.substring(from: index)
        }
        if cStr.characters.count != 6 {
            return UIColor.black
        }
        //两种不同截取字符串的方法
        let rRange = cStr.startIndex ..< cStr.index(cStr.startIndex, offsetBy: 2)
        let rStr = cStr.substring(with: rRange)
        
        let gRange = cStr.index(cStr.startIndex, offsetBy: 2) ..< cStr.index(cStr.startIndex, offsetBy: 4)
        let gStr = cStr.substring(with: gRange)
        
        let bIndex = cStr.index(cStr.endIndex, offsetBy: -2)
        let bStr = cStr.substring(from: bIndex)
        
        color = UIColor.init(colorLiteralRed: Float(changeToInt(numStr: rStr)) / 255, green: Float(changeToInt(numStr: gStr)) / 255, blue: Float(changeToInt(numStr: bStr)) / 255, alpha: 1)
        return color
    }
}

    func changeToInt(numStr:String) -> Int {
    
    let str = numStr.uppercased()
    var sum = 0
    for i in str.utf8 {
        //0-9 从48开始
        sum = sum * 16 + Int(i) - 48
        if i >= 65 {
            //A~Z 从65开始,但初始值为10
            sum -= 7
        }
    }
    return sum
}


当然,最好还是用Scanner来实现十六进制字符串转数字


 func transferStringToColor(_ colorStr:String) -> UIColor {
        
        var color = UIColor.red
        var cStr : String = colorStr.trimmingCharacters(in: CharacterSet.whitespacesAndNewlines).uppercased()
        
        if cStr.hasPrefix("#") {
            let index = cStr.index(after: cStr.startIndex)
            cStr = cStr.substring(from: index)
        }
        if cStr.characters.count != 6 {
            return UIColor.black
        }
        
        let rRange = cStr.startIndex ..< cStr.index(cStr.startIndex, offsetBy: 2)
        let rStr = cStr.substring(with: rRange)
        
        let gRange = cStr.index(cStr.startIndex, offsetBy: 2) ..< cStr.index(cStr.startIndex, offsetBy: 4)
        let gStr = cStr.substring(with: gRange)
        
        let bIndex = cStr.index(cStr.endIndex, offsetBy: -2)
        let bStr = cStr.substring(from: bIndex)
        
        var r:CUnsignedInt = 0, g:CUnsignedInt = 0, b:CUnsignedInt = 0;
        Scanner(string: rStr).scanHexInt32(&r)
        Scanner(string: gStr).scanHexInt32(&g)
        Scanner(string: bStr).scanHexInt32(&b)
        
        color = UIColor(red: CGFloat(r) / 255.0, green: CGFloat(g) / 255.0, blue: CGFloat(b) / 255.0, alpha: CGFloat(1))
        
        return color
    }



2019-06-13 16:27:55 sun6223508 阅读数 476
  • PHP7字符串模块

    King老师精心打造的PHP7字符串实战课程,由浅入深的讲解了PHP中字符串模块,并介绍了一些常用的字符串相关操作,让我们一起开启字符串之旅吧~ 山哥出品,必属精品!

    15539 人正在学习 去看看 何山

swift  字符串String转Int   16进制字符串String转Int 

 

var  hexi:UInt = UInt(hexS, radix: 16)

 

 

 

let n = 14
var st = String(format:"%02X", n)
st += " is the hexadecimal representation of \(n)"
print(st)

 


 
2019-08-08 15:26:34 u010585502 阅读数 207
  • PHP7字符串模块

    King老师精心打造的PHP7字符串实战课程,由浅入深的讲解了PHP中字符串模块,并介绍了一些常用的字符串相关操作,让我们一起开启字符串之旅吧~ 山哥出品,必属精品!

    15539 人正在学习 去看看 何山

[UInt8]转16进制字符串

使用String的format方法,把UInt8转成hex,使用02表示2位,不足补0。

extension Array where Element == UInt8 {
    var hexString: String {
        return self.compactMap { String(format: "%02x", $0).uppercased() }
        .joined(separator: "")
    }
}
2014-10-20 21:32:15 Hello_Hwc 阅读数 9153
  • PHP7字符串模块

    King老师精心打造的PHP7字符串实战课程,由浅入深的讲解了PHP中字符串模块,并介绍了一些常用的字符串相关操作,让我们一起开启字符串之旅吧~ 山哥出品,必属精品!

    15539 人正在学习 去看看 何山

原创Blog,转载请注明出处

今天用swfit写项目的时候遇到了转换的问题,贴出来,给需要的人

一、Int转16进制或者8进制字符串

var hexStr = String().stringByAppendingFormat("%x",16) //hexStr = "10"
var octStr = String().stringByAppendingFormat("%o",16) //hexStr = "20"

二、单个字节转换成为整数
例如:"c"转换成99(十进制)
var test = "c"
var nsstrTest:NSString = test
var result = nsstrTest.UTF8String[0] //result = 99

三、十六进制字符串转整数
var hexString = "2b"
var result:UInt32 = 0
NSScanner.scannerWithString(hexString).scanHexInt(&result)//result = 43

四、过滤掉一个字符
比如过滤掉空白
var test = "123 456 7 8 9"
var result = test.stringByReplacingOccurrencesOfString(" ",withString:"",options:NSStringCompareOptions.LiteralSearch,range:nil)//testresult = "123456789"


五、过滤掉一个字符集
var doNotWant = NSCharacterSet(charactersInString:"-<>")
var test = "<123-456-789>"
var result = join("",test.componentsSeparatedByCharactersInSet(doNotWant))


2016-11-24 15:57:17 dkq972958298 阅读数 7375
  • PHP7字符串模块

    King老师精心打造的PHP7字符串实战课程,由浅入深的讲解了PHP中字符串模块,并介绍了一些常用的字符串相关操作,让我们一起开启字符串之旅吧~ 山哥出品,必属精品!

    15539 人正在学习 去看看 何山

其中将十六进制字符串转换成NSData的代码如下:

- (NSData *)convertHexStrToData:(NSString *)str {
    if (!str || [str length] == 0) {
        return nil;
    }
    
    NSMutableData *hexData = [[NSMutableData alloc] initWithCapacity:8];
    NSRange range;
    if ([str length] % 2 == 0) {
        range = NSMakeRange(0, 2);
    } else {
        range = NSMakeRange(0, 1);
    }
    for (NSInteger i = range.location; i < [str length]; i += 2) {
        unsigned int anInt;
        NSString *hexCharStr = [str substringWithRange:range];
        NSScanner *scanner = [[NSScanner alloc] initWithString:hexCharStr];
        
        [scanner scanHexInt:&anInt];
        NSData *entity = [[NSData alloc] initWithBytes:&anInt length:1];
        [hexData appendData:entity];
        
        range.location += range.length;
        range.length = 2;
    }
    
    LEDEBUG(@"hexdata: %@", hexData);
    return hexData;
}

将NSData转换成十六进制的字符串则可使用如下方式:


- (NSString *)convertDataToHexStr:(NSData *)data {
    if (!data || [data length] == 0) {
        return @"";
    }
    NSMutableString *string = [[NSMutableString alloc] initWithCapacity:[data length]];
    
    [data enumerateByteRangesUsingBlock:^(const void *bytes, NSRange byteRange, BOOL *stop) {
        unsigned char *dataBytes = (unsigned char*)bytes;
        for (NSInteger i = 0; i < byteRange.length; i++) {
            NSString *hexStr = [NSString stringWithFormat:@"%x", (dataBytes[i]) & 0xff];
            if ([hexStr length] == 2) {
                [string appendString:hexStr];
            } else {
                [string appendFormat:@"0%@", hexStr];
            }
        }
    }];
    
    return string;
}





没有更多推荐了,返回首页