#import <Foundation/Foundation.h>

 

@interface ByteUnit : NSObject

//返回指定字节的10进制数

+(unsigned long)subDataLength:(NSData*)_didReadData startCount:(int)_startCount lenghtCount:(int)_lengthCount;

//将字节转为字符串形式

+(NSString *)byteToString:(NSData *)data startCount:(int)_startCount lenghtCount:(int)_lengthCount;

//字符串转字节数据

+(NSData*)stringToByte:(NSString*)string;

@end

 

//

//  ByteUnit.m

//  SmartHomeWIFI

//

//  Created by 深圳市 秀软科技有限公司 on 14-2-18.

//  Copyright (c) 2014 huhuaxiang. All rights reserved.

//

 

#import "ByteUnit.h"

 

@implementation ByteUnit

//获取数据长度。固定标准 4控制+4长度

+(unsigned long)subDataLength:(NSData*)_didReadData startCount:(int)_startCount lenghtCount:(int)_lengthCount

{

    unsigned long loc_length = 0;

    if (_didReadData.length >= _startCount+_lengthCount) {

        Byte *testByte = (Byte *)[_didReadData bytes];

        NSString *loc_string = @"";

        for (int i = 0 ; i <_lengthCount; i++) {

            unsigned long loc_length1 = testByte[_startCount+i];

            loc_string =[NSString stringWithFormat:@"%@%.2lx",loc_string,loc_length1];

            //[[NSString stringWithFormat:@"%.2lx",loc_length1] stringByAppendingString:loc_string];//

            //            NSLog(@"hejiang:%@",loc_string);

        }

        //        unsigned long loc_length1 = testByte[4];

        //        unsigned long loc_length2 = testByte[5];

        //        unsigned long loc_length3 = testByte[6];

        //        unsigned long loc_length4 = testByte[7];

        //        NSString *loc_string = [NSString stringWithFormat:@"%.2lx%.2lx%.2lx%.2lx",loc_length4,loc_length3,loc_length2,loc_length1];

        

        unsigned long red = strtoul([loc_string UTF8String],0,16);

        

        loc_length = red;

    }

    return loc_length;

//    if(_lengthCount==2)

//        return htons(loc_length);

//    else

//        return htonl(loc_length);

}

+(NSString *)byteToString:(NSData *)data startCount:(int)_startCount lenghtCount:(int)_lengthCount

{

    /*

    NSData *dataT=[data subdataWithRange:NSMakeRange(_startCount, _lengthCount)];

    NSString *str=[[NSString alloc]initWithData:dataT encoding:NSUTF8StringEncoding];

    return str;

     */

    

    NSString *loc_string = @"";

    if (data.length >= _startCount+_lengthCount) {

        Byte *testByte = (Byte *)[data bytes];

        for (int i = 0 ; i <_lengthCount; i++) {

            unsigned long loc_length1 = testByte[_startCount+i];

            loc_string = [NSString stringWithFormat:@"%@%.lx",loc_string,loc_length1];

        }

    }

    return loc_string;

}

+(NSData*)stringToByte:(NSString*)string

{

    NSString *hexString=[[string uppercaseString] stringByReplacingOccurrencesOfString:@" "withString:@""];

    if ([hexString length]%2!=0) {

        return nil;

    }

    Byte tempbyt[1]={0};

    NSMutableData* bytes=[NSMutableDatadata];

    for(int i=0;i<[hexString length];i++)

    {

        unichar hex_char1 = [hexString characterAtIndex:i]; ////两位16进制数中的第一位(高位*16)

        int int_ch1;

        if(hex_char1 >= '0' && hex_char1 <='9')

            int_ch1 = (hex_char1-48)*16;   //// 0 Ascll - 48

        else if(hex_char1 >= 'A' && hex_char1 <='F')

            int_ch1 = (hex_char1-55)*16; //// A Ascll - 65

        else

            return nil;

        i++;

        

        unichar hex_char2 = [hexString characterAtIndex:i]; ///两位16进制数中的第二位(低位)

        int int_ch2;

        if(hex_char2 >= '0' && hex_char2 <='9')

            int_ch2 = (hex_char2-48); //// 0 Ascll - 48

        else if(hex_char2 >= 'A' && hex_char2 <='F')

            int_ch2 = hex_char2-55; //// A Ascll - 65

        else

            return nil;

        

        tempbyt[0] = int_ch1+int_ch2;  ///将转化后的数放入Byte数组里

        [bytes appendBytes:tempbyt length:1];

    }

    return bytes;

}

@end

 

posted on 2014-03-16 21:28  阿里海瑟要  阅读(297)  评论(0编辑  收藏  举报