美文网首页swift4.0
Swift/OC - 与外设连接时的字符处理(普通字符串-十六进

Swift/OC - 与外设连接时的字符处理(普通字符串-十六进

作者: 279cb620c509 | 来源:发表于2018-07-10 16:38 被阅读46次

    Swift - 与外设连接时的字符处理(普通字符串-十六进制互转)

    tips:与外部设备通信难免会遇到字符间互转的问题,普通字符串-十六进制互转是很多app与设备连接时会采用的方案,在网上看到oc版的,于是有了下面的的swift版,亲测可用,不足之处希望能留言告知,谢谢

    /// 十六进制转换为普通字符串的。

    func stringfromHexStr() -> String {
    
            var bytes = [UInt8]()
    
            var dataBStr : String = ""
    
            for (index, _) in self.enumerated(){
    
                let fromIndex = index*2
    
                let toIndex = index*2 + 1
    
                if toIndex > (self.count - 1) {
    
                    break
    
                }
    
                let hexCharStr = self.subString(fromIndex, length: 2)
    
                var r:CUnsignedInt = 0
    
                Scanner(string: hexCharStr).scanHexInt32(&r)
    
                bytes.append(UInt8(r))
    
            }
    
            dataBStr = String.init(data: Data(bytes: bytes), encoding: String.Encoding.ascii)!
    
            return dataBStr
    
        }
    
    ///普通字符串转换为十六进制的。
    
    
    
    func stringToHexString() -> String {
    
            let strData = self.data(using: String.Encoding.utf8)
    
            let bytes = strData?.withUnsafeBytes {
    
                [UInt8](UnsafeBufferPointer(start: $0, count: (strData?.count)!))
    
            }
    
            var sumString : String = ""
    
            for byte in bytes! {
    
                let newString = String(format: "%x",byte&0xff)
    
                if newString.count == 1 {
    
                    sumString = String(format: "%@0%@",sumString,newString)
    
                }else{
    
                    sumString = String(format: "%@%@",sumString,newString)
    
                }
    
            }
    
            
    
            return sumString
    
        }
    

    补充oc的对照版

    ///普通字符串转换为十六进制的。
    
    \+ (NSString *)hexStringFromString:(NSString *)string{
    
        NSData *myD = [string dataUsingEncoding:NSUTF8StringEncoding];
    
        Byte *bytes = (Byte *)[myD bytes];
    
        //下面是Byte 转换为16进制。
    
        NSString *hexStr=@"";
    
        for(int i=0;i<[myD length];i++)
    
        {
    
            NSString *newHexStr = [NSString stringWithFormat:@"%x",bytes[i]&0xff];///16进制数
    
            if([newHexStr length]==1)
    
                hexStr = [NSString stringWithFormat:@"%@0%@",hexStr,newHexStr];
    
            else
    
                hexStr = [NSString stringWithFormat:@"%@%@",hexStr,newHexStr];
    
        }
    
        return [NSString stringWithFormat:@"%@0d0a",hexStr];
    
    }
    
    /// 十六进制转换为普通字符串的。
    
    \+ (NSString *)stringFromHexString:(NSString *)hexString {
    
        
    
        char *myBuffer = (char *)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];
    
            [scanner scanHexInt:&anInt];
    
            myBuffer[i / 2] = (char)anInt;
    
        }
    
        NSString *unicodeString = [NSString stringWithCString:myBuffer encoding:4];
    
    //    NSLog(@"------字符串=======%@",unicodeString);
    
        return unicodeString;
    
    }
    

    相关文章

      网友评论

        本文标题:Swift/OC - 与外设连接时的字符处理(普通字符串-十六进

        本文链接:https://www.haomeiwen.com/subject/iecqpftx.html