iOS 16 进制与 10进制之间的 互转
2015-12-29 16:23
477 查看
最近自己也在做蓝牙开发,发现好多做蓝牙开发的都没有接触过C ,字节转换更是晕乎乎的,所以写下此篇博客,提供我转换的方法。
一、整形装换成两个字节
int value =
456;
unsigned
char byte1 = (value & 0xff00)>>8;
//高8位
unsigned
char byte2 = (value & 0xff);//低8位
NSLog(@"byte1= %x byte2= %x ",byte1,byte2);
byte1 = 1; byte2= c8;
二、两个字节转换成十进制整数
char bytes[]={byte1,byte2};
unsigned
char by1 = (bytes[0] &
0xff); //高8位
unsigned
char by2 = (bytes[1] &
0xff);//低8位
int temp = (by2|(by1<<8));
NSLog(@"temp = %d",temp);
temp = 456;
三、整形装换成三个字节
int value =134456;
unsigned
char byte1 = (value & 0xff0000)>>16;//最高8位
unsigned
char byte2 = (value & 0xff00)>>8;//中间8位
unsigned
char byte3 = (value & 0xff);//低8位
NSLog(@"byte1= %x byte2= %x byte3= %x ",byte1,byte2,byte3);
byte1= 2 byte2= d byte3= 38
四、三个字节转换成十进制整数
char bytes[]={byte1,byte2,byte3};
unsigned
char by1 = (bytes[0] &
0xff); //高8位
unsigned
char by2 = (bytes[1] &
0xff);//中8位
unsigned
char by3 = (bytes[2] &
0xff);//低8位
int temp = (by3|(by2<<8)|(by1<<16));
NSLog(@"temp = %d",temp);
temp = 134456
如果还有更多的自己接需要转换以此类推
一、整形装换成两个字节
int value =
456;
unsigned
char byte1 = (value & 0xff00)>>8;
//高8位
unsigned
char byte2 = (value & 0xff);//低8位
NSLog(@"byte1= %x byte2= %x ",byte1,byte2);
byte1 = 1; byte2= c8;
二、两个字节转换成十进制整数
char bytes[]={byte1,byte2};
unsigned
char by1 = (bytes[0] &
0xff); //高8位
unsigned
char by2 = (bytes[1] &
0xff);//低8位
int temp = (by2|(by1<<8));
NSLog(@"temp = %d",temp);
temp = 456;
三、整形装换成三个字节
int value =134456;
unsigned
char byte1 = (value & 0xff0000)>>16;//最高8位
unsigned
char byte2 = (value & 0xff00)>>8;//中间8位
unsigned
char byte3 = (value & 0xff);//低8位
NSLog(@"byte1= %x byte2= %x byte3= %x ",byte1,byte2,byte3);
byte1= 2 byte2= d byte3= 38
四、三个字节转换成十进制整数
char bytes[]={byte1,byte2,byte3};
unsigned
char by1 = (bytes[0] &
0xff); //高8位
unsigned
char by2 = (bytes[1] &
0xff);//中8位
unsigned
char by3 = (bytes[2] &
0xff);//低8位
int temp = (by3|(by2<<8)|(by1<<16));
NSLog(@"temp = %d",temp);
temp = 134456
如果还有更多的自己接需要转换以此类推
相关文章推荐
- 在CocoaPods上创建自己iOS程序的依赖管理
- iOS 隐藏状态栏
- IOS开发之保存图片到Documents目录及PNG,JPEG格式相互转换
- iOS系统定位权限弹出框和跳转系统设置
- ios tableview 适配横竖屏
- IOS开动画效果之──实现 pushViewController 默认动画效果
- iOS Core Animation 简明系列教程
- IOS 9人机界面指南(1)
- IOS开发 CABasicAnimation
- iOS 开发搜索框 - 简单实现
- iOS时间与时间戳相互转换
- 一步一步教你做ios推送
- 12月28号 ios设计简单操作
- IOS 图片拉伸技巧
- iOS 简单九宫格
- iOS 过期 注释
- iOS - 收到远程推送后的页面跳转
- iOS No architectures to compile for (ONLY_ACTIVE_ARCH=YES, active arch=x86_64, V
- IOS学习之蓝牙4.0
- NIOSII中的LWIP协议栈中的FD_ZERO,FD_SET,FD_ISSET