请教:用java编写将十六进制数转换为十进制数的方法

zzjh_2008 2008-11-20 12:45:57
请教:用java编写将十六进制数转换为十进制数的方法,该方法通过参数形式接收待转换的十六进制数。转换后的输出格式为:十六进制数XXX;十进制数YYY。其中XXX代表待转换的十六进制数,YYY代表转换后的十进制数。运行程序时,待转换的十六进制数通过命令行参数输入。
...全文
3257 11 打赏 收藏 转发到动态 举报
写回复
用AI写文章
11 条回复
切换为时间正序
请发表友善的回复…
发表回复
breezes2008 2009-03-02
  • 打赏
  • 举报
回复
2楼回答得好!支持。
renxws 2009-03-02
  • 打赏
  • 举报
回复
你写的方法很好但是有毛病!假如我输入“ssss”,将其转化成16进制 出现异常
dreamhunter_lan 2008-11-20
  • 打赏
  • 举报
回复
好像根本就不用转
比如:
输入:0x12
输出:18
KOOK_OKKO 2008-11-20
  • 打赏
  • 举报
回复

System.out.println(Integer.valueOf("ff",16));
yun00112300 2008-11-20
  • 打赏
  • 举报
回复
public static void main(String args[]) throws NumberFormatException, IOException{
BufferedReader in = new BufferedReader(new InputStreamReader(System.in));
System.out.println(Integer.parseInt(in.readLine().toString(), 16));
}
sunnylyy 2008-11-20
  • 打赏
  • 举报
回复
输入流?

楼主搞什么,不是写着通过命令行输入么?
shenjie1989 2008-11-20
  • 打赏
  • 举报
回复
import java.awt.*;
public class d2x extends Frame
{
int decimalValue= 0;
String baseXValue = new String("0");
TextField dDisplay,xDisplay;
//d2x constructor
d2x()
{
super("Decimal Converter");//set the title of the frame
MenuBar mb = new MenuBar();
Button d2Binary = new Button("Binary");
Button d2Octal = new Button("Octal");
Button d2Hex = new Button("Hex");
Button d2Base36 = new Button("Base36");
Panel p1 = new Panel();
Panel p2 = new Panel();
Panel p3 = new Panel();

//add a simple menu
Menu m = new Menu("Application");
m.add(new CheckboxMenuItem("Base 36 Active"));
m.add(new MenuItem("Exit"));

//add menu to menubar
mb.add(m);
setMenuBar(mb);//install this menu bar in the frame

//Add buttons to their own panel
p3.setLayout(new FlowLayout());
p3.add(d2Binary);
p3.add(d2Octal);
p3.add(d2Hex);
p3.add(d2Base36);

//Add text fields
Label dLabel = new Label("Enter Deecimal: ");
Label xLabel = new Label("Converted Value: ");
dDisplay = new TextField(Integer.toString(decimalValue),7);
xDisplay = new TextField(baseXValue,32);
xDisplay.setEditable(false);
p1.setLayout(new FlowLayout(FlowLayout.LEFT));
p2.setLayout(new FlowLayout(FlowLayout.LEFT));
p1.add(dLabel);
p1.add(dDisplay);
p2.add(xLabel);
p2.add(xDisplay);

//Add the panels
add("North",p1);
add("Center",p2);
add("South",p3);
}//end d2x constructor

public void start()
{
resize(400,150);
show();
}

public void updateXDisplay()
{
xDisplay.setText(baseXValue);
}

public boolean handleEvent(Event evt)
{
if (evt.target instanceof MenuItem)
{
if ("Exit".equals(((MenuItem)evt.target).getLabel()))
{
hide();
dispose();
System.exit(0);
return false;
}
return true;
}
else if(evt.target instanceof Button)
{
String whick = ((Button)evt.target).getLabel();
if (whick.equals("Binary"))
{
decimalValue = Integer.parseInt(dDisplay.getText());
baseXValue = Integer.toString(decimalValue,2);
}
if (whick.equals("Octal"))
{
decimalValue = Integer.parseInt(dDisplay.getText());
baseXValue = Integer.toString(decimalValue,8);
}
if (whick.equals("Hex"))
{
decimalValue = Integer.parseInt(dDisplay.getText());
baseXValue = Integer.toString(decimalValue,16);
}
if (whick.equals("36"))
{
decimalValue = Integer.parseInt(dDisplay.getText());
baseXValue = Integer.toString(decimalValue,36);
}
updateXDisplay();
return true;
}
return false;
}


public static void main(String args[])
{
d2x m = new d2x();
m.start();
}
}
zzjh_2008 2008-11-20
  • 打赏
  • 举报
回复
[Quote=引用 2 楼 sunnylyy 的回复:]
Java codepublic static void main(String[] args){
if(args.length != 1){
System.out.println("please input a hex number");
return;
}
try
{
int num = Integer.parseInt(args[0].substring(2), 16);
System.out.println(num);
}
catch(Exception e){

System.out.println("please input…
[/Quote]
没有输入流,而且不符合要求,能不能写的详细一点啊?
fuyou001 2008-11-20
  • 打赏
  • 举报
回复
[Quote=引用 2 楼 sunnylyy 的回复:]
Java codepublic static void main(String[] args){
if(args.length != 1){
System.out.println("please input a hex number");
return;
}
try
{
int num = Integer.parseInt(args[0].substring(2), 16);
System.out.println(num);
}
catch(Exception e){

System.out.println("please input…
[/Quote]
ZiSheng 2008-11-20
  • 打赏
  • 举报
回复

System.out.println(Integer.parseInt("1F",16));
sunnylyy 2008-11-20
  • 打赏
  • 举报
回复
public static void main(String[] args){
if(args.length != 1){
System.out.println("please input a hex number");
return;
}
try
{
int num = Integer.parseInt(args[0].substring(2), 16);
System.out.println(num);
}
catch(Exception e){

System.out.println("please input a correct hex number");
}
}

62,614

社区成员

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

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