C语言版和JAVA版 把一个字节正序(高位在前)转为逆序(低位在前) 和 逆序转为正序
2017-05-25 10:10
423 查看
一、C语言版 把一个字节正序(高位在前)转为逆序(低位在前) 和 逆序转为正序
结果:
44
22
Press any key to continue
2.JAVA版
// 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); } }
相关文章推荐
- java取模数据格式转换源码 宽高16*16 高位在前 转为 低位在前
- java__加一。数组内存放了一些个位数字,组成一个大数(从高位到低位),现在将这个数加 11,并输出加一以后的结果。
- 字符串逆序,字符串转为字符数组 、字符串转字节数组,字节数组转字符串、java中String、StringBuffer、StringBuilder
- 怎样得到一个int变量的高位字节和低位字节
- JAVA内存使用--如何计算一个Java对象占用的字节数
- 14、JAVA 判断一个数/字符串是不是回文?实现字符串的逆序,如“abcd”变成“dcba”
- JAVA内存使用--如何计算一个Java对象占用的字节数
- oracle和java 一个中文占几个字节
- 简单问题:LPARAM 是一个long(32bit),双字,怎么分别得到高位字和低位字的值。
- JAVA中获得一个有中文的字符串的字节长度
- java中一个汉字和一个字母所占内存字节比较以及后台验证的减半处理
- java 截取一个包含汉字的字符串的前n个字节的算法
- 读入一个整数,将各个数位上的数拆分下来并输出(从高位到低位)。
- 数据表示:字节 高位低位
- java里面一个字符占几个字节?
- 2. 编写一个Java应用程序,用户从键盘输入一个1~9999之间的数,程序将判断这个数是几位数,并判断这个数是否是回文数。回文数是指将该数含有的数字逆序排列后得到的数和原数相同,例如12121、32
- 将一个字节的bits逆序
- 【Java】编写一个截取字符串的函数,输入为一个字符串和字节数,输出为按字节截取的字符串。但是要保证汉字不被截半个,如“我ABC”4,应该截为“我AB”,输入“我ABC汉DEF”,6,应该输出为“我ABC”而不是“我ABC+汉的半个”。
- iOS 低位、高位字节转换为int
- 10、 java 编写一个截取字符串的函数,输入为一个字符串和字节数,输出为按字节截取的字符串。 要求不能出现截半的情况