java中有没有函数可以将整型数(十六进制)按位逆序排列

ltolll 2009-08-18 12:51:54
如,有数:
int a=0x131ca6e;
要求处理后得到:0x6eca3101
最好是java中自有函数。
如果手写,最好不要涉及平台和系统相关的特性。考虑效率和移植性。
...全文
289 9 打赏 收藏 转发到动态 举报
写回复
用AI写文章
9 条回复
切换为时间正序
请发表友善的回复…
发表回复
bigbug9002 2009-08-18
  • 打赏
  • 举报
回复
mport java.util.regex.*;
import java.util.*;

public class Test7{

public static void main(String[] args){
int i=0x1131ca6e;
System.out.println(getHex(i));
System.out.println(Integer.toHexString(Integer.reverse(i)));

}



public static String getHex(int x){
String intHex=Integer.toHexString(x);
StringBuilder sb=new StringBuilder();
int i;
for( i=intHex.length()-1;i-1>=0;i-=2){
sb.append(intHex.substring(i-1,i+1));
}
if(i==0){
sb.append(0+intHex.substring(0,1));
}
return sb.toString();

}
}
lovecj6185 2009-08-18
  • 打赏
  • 举报
回复
[Quote=引用 4 楼 ltolll 的回复:]
自己找到答案:
b=Integer.reverseBytes(a);
[/Quote]

试验了一下,好像不对吧
knightzhuwei 2009-08-18
  • 打赏
  • 举报
回复
public class Test {
public static void main(String[] args) {
int a=0x131ca6e;
System.out.println(Integer.toHexString(Integer.reverseBytes(a)));
}
}


输出

6eca3101
knightzhuwei 2009-08-18
  • 打赏
  • 举报
回复
[Quote=引用 4 楼 ltolll 的回复:]
自己找到答案:
b=Integer.reverseBytes(a);
[/Quote]
你确定???
knightzhuwei 2009-08-18
  • 打赏
  • 举报
回复

public class Test {
public static void main(String[] args) {
int a=0x131ca6e;
int b=1,c=1;
c=a%16;
a=a/16;
b=c;
while(a>0){
c=a%16;
a=a/16;
b=b*16+c;
}
System.out.println(Integer.toHexString(b));
}
}


输出
e6ac131
ltolll 2009-08-18
  • 打赏
  • 举报
回复
自己找到答案:
b=Integer.reverseBytes(a);
ThirstyCrow 2009-08-18
  • 打赏
  • 举报
回复
JDK 1.5以上可用:
Integer.reverse(a);
bigbug9002 2009-08-18
  • 打赏
  • 举报
回复
看错了
bigbug9002 2009-08-18
  • 打赏
  • 举报
回复

StringBuilder sb=new StringBuilder(Integer.toHexString(i));
return sb.reverse().toString();

62,628

社区成员

发帖
与我相关
我的任务
社区描述
Java 2 Standard Edition
社区管理员
  • Java SE
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧