美文网首页socket
NSdata 与 Byte数组相互转换

NSdata 与 Byte数组相互转换

作者: 仟度 | 来源:发表于2017-01-07 14:40 被阅读0次

    NSData 与 ByteNSData-> Byte数组

    NSString *testString = @"1234567890";

    NSData *testData = [testString dataUsingEncoding: NSUTF8StringEncoding];

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

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

    printf("testByte = %d\n",testByte[i]);

    Byte数组-> NSData

    Byte byte[] = {0,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21,22,23};

    NSData *adata = [[NSData alloc] initWithBytes:byte length:24];

    Byte数组->16进制数

    Byte *bytes = (Byte *)[aData bytes];

    NSString *hexStr=@"";

    for(int i=0;i<[encryData 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];

        }

    }

    相关文章

      网友评论

        本文标题:NSdata 与 Byte数组相互转换

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