正序 逆序写 java_C語言版和JAVA版 把一個字節正序(高位在前)轉為逆序(低位在前) 和 逆序轉為正序...

一、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);

}

}


本文来自互联网用户投稿,文章观点仅代表作者本人,不代表本站立场,不承担相关法律责任。如若转载,请注明出处。 如若内容造成侵权/违法违规/事实不符,请点击【内容举报】进行投诉反馈!

相关文章

立即
投稿

微信公众账号

微信扫一扫加关注

返回
顶部