智乐公司java的一笔试试题,测试你的编程能力

XJAVASunjava 2010-09-30 05:05:36
给你一二进制字符串00101111,要求在不使用Integer类的方法的情况下将其转换为“ox2f ”
方法如下:public String BinaryTohex(String binary){















}
...全文
200 14 打赏 收藏 转发到动态 举报
写回复
用AI写文章
14 条回复
切换为时间正序
请发表友善的回复…
发表回复
XJAVASunjava 2010-09-30
  • 打赏
  • 举报
回复
为什么在补齐二进制位数的时候不用一个循环呢?我觉得那样的话效率还是很高的,而且代码量也不多,更容易理解一些啊[Quote=引用 13 楼 xjavasunjava 的回复:]
很不错的算法,这个效率可能更高些。。。。

引用 12 楼 inhibitory 的回复:
Java code

package com.biao;

import java.util.HashMap;
import java.util.Map;

public class Binary2Hex {
private static Map<String, String> bTa……
[/Quote]
XJAVASunjava 2010-09-30
  • 打赏
  • 举报
回复
很不错的算法,这个效率可能更高些。。。。[Quote=引用 12 楼 inhibitory 的回复:]
Java code

package com.biao;

import java.util.HashMap;
import java.util.Map;

public class Binary2Hex {
private static Map<String, String> bTable;
static {
bTable = new Hash……
[/Quote]
Inhibitory 2010-09-30
  • 打赏
  • 举报
回复

package com.biao;

import java.util.HashMap;
import java.util.Map;

public class Binary2Hex {
private static Map<String, String> bTable;
static {
bTable = new HashMap<String, String>();
bTable.put("0000", "0");
bTable.put("0001", "1");
bTable.put("0010", "2");
bTable.put("0011", "3");
bTable.put("0100", "4");
bTable.put("0101", "5");
bTable.put("0110", "6");
bTable.put("0111", "7");
bTable.put("1000", "8");
bTable.put("1001", "9");
bTable.put("1010", "A");
bTable.put("1011", "B");
bTable.put("1100", "C");
bTable.put("1101", "D");
bTable.put("1110", "E");
bTable.put("1111", "F");
}

public static String toHex(String bStr) {
int ex = 4 - bStr.length() % 4;
switch (ex) {
case 1:
bStr = "0" + bStr;
break;
case 2:
bStr = "00" + bStr;
break;
case 3:
bStr = "000" + bStr;
break;
}

StringBuilder sb = new StringBuilder("0x");
for (int i = 0; i < bStr.length(); i += 4) {
sb.append(bTable.get(bStr.substring(i, i + 4)));
}

return sb.toString();
}

public static void main(String[] args) {
String b = "10100101111";
System.out.println(Binary2Hex.toHex(b));
}
}
Inhibitory 2010-09-30
  • 打赏
  • 举报
回复
补齐4的倍数位,然后按四位取,查表,输出结果
XJAVASunjava 2010-09-30
  • 打赏
  • 举报
回复
莫非你就是在我后面坐的那个哥么?[Quote=引用 7 楼 jaisokforron 的回复:]
呵呵,我也去做这份题了,我当时的思路是把它弄成十进制,然后转成十六进制
[/Quote]
haove 2010-09-30
  • 打赏
  • 举报
回复
[Quote=引用 4 楼 clariones 的回复:]
那咋就空口白牙写段程序,看看行不

Java code

String in = "001010100"; // 输入
String result = "0x"; // 保存结果
while(in.length % 4 != 0){
in = "0"+in; // 先补齐,4位的整数被
}

for(int i=0;i<in.lengt……
[/Quote]

简单而有效,好想法
jaisokforron 2010-09-30
  • 打赏
  • 举报
回复
这家公司出的游戏方面的题太难了
jaisokforron 2010-09-30
  • 打赏
  • 举报
回复
呵呵,我也去做这份题了,我当时的思路是把它弄成十进制,然后转成十六进制
clariones 2010-09-30
  • 打赏
  • 举报
回复
果然空手写代码有好多错误,上面代码有这些错误
1。 循环条件不对
2。 in每次没有去掉已经处理过的部分
3。 关键字 else if 写成了 elseif
4。 输入的字符串,前导0没有去掉

都是测试一下才发现的,正确可运行的代码如下

String in = "001010100"; // 输入
String result = "0x"; // 保存结果
while(in.length() % 4 != 0){
in = "0"+in; // 先补齐,4位的整数被
}


while(in.length()>0){
String piece = in.substring(0,4);
if(piece.equalsIgnoreCase("0000")){
if (result.length() > 2)
result += '0';
}else if(piece.equalsIgnoreCase("0001")){
result += '1';
}else if(piece.equalsIgnoreCase("0010")){
result += '2';
}else if(piece.equalsIgnoreCase("0011")){
result += '3';
}else if(piece.equalsIgnoreCase("0100")){
result += '4';
}else if(piece.equalsIgnoreCase("0101")){
result += '5';
}else if(piece.equalsIgnoreCase("0110")){
result += '6';
}else if(piece.equalsIgnoreCase("0111")){
result += '7';
}else if(piece.equalsIgnoreCase("1000")){
result += '8';
}else if(piece.equalsIgnoreCase("1001")){
result += '9';
}else if(piece.equalsIgnoreCase("1010")){
result += 'a';
}else if(piece.equalsIgnoreCase("1011")){
result += 'b';
}else if(piece.equalsIgnoreCase("1100")){
result += 'c';
}else if(piece.equalsIgnoreCase("1101")){
result += 'd';
}else if(piece.equalsIgnoreCase("1110")){
result += 'e';
}else if(piece.equalsIgnoreCase("1111")){
result += 'f';
}else{
result += "[What do you want to do?]";
}
in = in.substring(4);
}


System.out.println(result);


zhaolinger2 2010-09-30
  • 打赏
  • 举报
回复
如果是一个完整的方法的话,应该做的是:
1、判断binary是否只由0和1构成;
2、判断binary长度是否为8;
3、分成前后两个部分,各自转换。
clariones 2010-09-30
  • 打赏
  • 举报
回复
那咋就空口白牙写段程序,看看行不

String in = "001010100"; // 输入
String result = "0x"; // 保存结果
while(in.length % 4 != 0){
in = "0"+in; // 先补齐,4位的整数被
}

for(int i=0;i<in.length;i+=4){
String piece = in.substring(in.length-4);
if(piece.equalsIgnoreCase("0000")){
result += '0';
}elseif(piece.equalsIgnoreCase("0001")){
result += '1';
}elseif(piece.equalsIgnoreCase("0010")){
result += '2';
}elseif(piece.equalsIgnoreCase("0011")){
result += '3';
}elseif(piece.equalsIgnoreCase("0100")){
result += '4';
}elseif(piece.equalsIgnoreCase("0101")){
result += '5';
}elseif(piece.equalsIgnoreCase("0110")){
result += '6';
}elseif(piece.equalsIgnoreCase("0111")){
result += '7';
}elseif(piece.equalsIgnoreCase("1000")){
result += '8';
}elseif(piece.equalsIgnoreCase("1001")){
result += '9';
}elseif(piece.equalsIgnoreCase("1010")){
result += 'a';
}elseif(piece.equalsIgnoreCase("1011")){
result += 'b';
}elseif(piece.equalsIgnoreCase("1100")){
result += 'c';
}elseif(piece.equalsIgnoreCase("1101")){
result += 'd';
}elseif(piece.equalsIgnoreCase("1110")){
result += 'e';
}elseif(piece.equalsIgnoreCase("1111")){
result += 'f';
}else{
result += "[What do you want to do?]";
}
}

System.out.println(result);
XJAVASunjava 2010-09-30
  • 打赏
  • 举报
回复
楼上的给出具体代码哈,我当时也是用subString来截取的,但是没写出来。
后来我想到了先转换为十进制再转换为十六进制就可以搞定了
nkinuyasha 2010-09-30
  • 打赏
  • 举报
回复
每次截取四位,然后算出这四位二进制对应的十六进制就行了
jn789987 2010-09-30
  • 打赏
  • 举报
回复
这个锻练容易完成

substring截取字符串,对截出的数据进行判断 用字符串'1','0'进行判断

62,614

社区成员

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

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