您的位置:首页 > 编程语言 > C语言/C++

C语言版和JAVA版 把一个字节正序(高位在前)转为逆序(低位在前) 和 逆序转为正序

2017-05-25 10:10 423 查看
一、C语言版 把一个字节正序(高位在前)转为逆序(低位在前) 和 逆序转为正序

// xhrrj.cpp : Defines the entry point for the console application.
//

#include "stdafx.h"
//把一个字节 高位在前 转为 低位在前
unsigned char Byte_Change(unsigned char ter)
{
unsigned char i=0;
unsigned char tem=0;
for(i=0;i<8;i++)
{
tem=tem<<1; //低位向左移
tem=((ter>>i)&0x01)|tem; //低位的值
}
return tem;
}
//把一个字节 低位在前 转为 高位在前
unsigned char Byte_Change2(unsigned char ter)
{
unsigned char i=0;
unsigned char tem=0;
for(i=0;i<8;i++)
{
tem=tem>>1;
tem=((ter<<i)&0x80)|tem; //位的值
}
return tem;
}
void main(int argc, char* argv[])
{
unsigned char a=0;
a=Byte_Change(0x22);
printf("%02X\n",a);
a=Byte_Change2(0x44);
printf("%02X\n",a);
}


结果:

44

22

Press any key to continue

2.JAVA版

public class ByteChange {

static //把一个字节 高位在前 转为 低位在前
int Byte_Change(int ter)
{
int i=0;
int tem=0;
for(i=0;i<8;i++)
{
tem=tem<<1; //低位向左移
tem=((ter>>i)&0x01)|tem; //低位的值
}
return tem;
}
//把一个字节 低位在前 转为 高位在前
static int Byte_Change2(int ter)
{
int i=0;
int tem=0;
for(i=0;i<8;i++)
{
tem=tem>>1;
tem=((ter<<i)&0x80)|tem; //位的值
}
return tem;
}

public static void main(String[] args) {
// TODO Auto-generated method stub
int a=0;
a=Byte_Change(0x22);
System.out.printf("%02X\n",a);
a=Byte_Change2(0x44);
System.out.printf("%02X\n",a);
}

}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: 
相关文章推荐