一、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<
}
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<
}
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);
}
}
轉為逆序(低位在前) 和 逆序轉為正序...&spm=1001.2101.3001.5002&articleId=114713405&d=1&t=3&u=2d1c0b49fcda4cb38c7344c70b379f0c)
247

被折叠的 条评论
为什么被折叠?



