字符串NSString轉(zhuǎn)換byte ,byte反轉(zhuǎn)得到Nsstring

//

//? ViewController.m

//? qwe

//

//? Created by 余浩 on 17/5/25.

//? Copyright ? 2017年 余浩. All rights reserved.

//

#import "ViewController.h"

@interface ViewController ()

@end

@implementation ViewController

- (void)viewDidLoad {

[super viewDidLoad];

NSString *string = [NSString stringWithFormat:@"2565"];

NSData *testData = [string dataUsingEncoding: NSUTF8StringEncoding];

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

NSLog(@"data=====%@",testData);

Byte *newByte = (Byte *)malloc([testData length]);

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

newByte[i] = testByte[i]^1;

printf("Byte------------%hhu\n",testByte[i]);//%hhu? 指定signed char 或 unsigned char? ? char是1個(gè)字節(jié)

//? printf("new------------%hhu\n",newByte[i]);

int c = testByte[i] &0xff;? //int類(lèi)型32

NSLog(@"int c==========%d",c);

};

Byte *nByte = (Byte *)malloc(1);

nByte [0] = -127;

NSLog(@"nBytex==========%x",nByte[0]);

int d = nByte[0] &0xff;//0xff在c語(yǔ)言表示一個(gè)十六進(jìn)制無(wú)符號(hào)整數(shù),有符號(hào)轉(zhuǎn)成無(wú)符號(hào)

NSLog(@"d==========%d",d);

NSData *data = [NSData dataWithBytes:newByte length:[testData length]];

NSString *stingaa = [[NSString alloc]initWithData:data encoding:(NSUTF8StringEncoding)];

//反轉(zhuǎn)

NSData *testData1 = [stingaa dataUsingEncoding: NSUTF8StringEncoding];

Byte *testByte1 = (Byte *)[testData1 bytes];

Byte *newByte1 = (Byte *)malloc([testData1 length]);

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

newByte1[i] = testByte1[i]^1;

//? printf("newByte1------------%d\n",newByte1[i]);

};

Byte key[1] = {0x32};

NSData *data1 = [NSData dataWithBytes:newByte1 length:[testData length]];

NSString *stingaa1 = [[NSString alloc]initWithData:data1 encoding:(NSUTF8StringEncoding)];

NSLog(@"stingaa1=========%@",stingaa1);

//&(按位與)、|(按位或)、^(按位異或)、~ (按位取反)。

//1個(gè)字節(jié)是8位,二進(jìn)制8位:xxxxxxxx 范圍從

/**

00000000-11111111,表示0到255。

一位16進(jìn)制數(shù)(用二進(jìn)制表示是xxxx)最多只表示到15(即對(duì)應(yīng)16進(jìn)制的F),要表示到255,就還需要第二位。

所以1個(gè)字節(jié)=2個(gè)16進(jìn)制字符,一個(gè)16進(jìn)制位=0.5個(gè)字節(jié)

*/

}

- (void)didReceiveMemoryWarning {

[super didReceiveMemoryWarning];

// Dispose of any resources that can be recreated.

}

@end

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書(shū)系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容