A股上市公司传智教育(股票代码 003032)旗下技术交流社区北京昌平校区

 找回密码
 加入黑马

QQ登录

只需一步,快速开始

© a12366456 中级黑马   /  2015-7-30 23:40  /  648 人查看  /  18 人回复  /   0 人收藏 转载请遵从CC协议 禁止商业使用本文

十进制转二进制的代码谁有

18 个回复

倒序浏览
public static void toBin(int num){
         StringBuffer sb=new StringBuffer();
         while(num>0){
         sb.append(num%2);
         num=num/2;
    }
        System.out.println(sb.reverse());
}
回复 使用道具 举报
  1. public static String toBin(int num)
  2. {
  3.         StringBuffer sb = new StringBuffer();
  4.         while(num>0)
  5.         {
  6.                 sb.append(num%2);
  7.                 num = num /2;
  8.         }
  9.         return sb.reverse();
  10. }
复制代码
回复 使用道具 举报
  1. public static void toBin(int num)
  2. {        if(num>0)
  3.                 toBin(num/2);
  4.         System.out.println(num%2);
  5. }
复制代码
回复 使用道具 举报

if下面少了对{},把输出也括起来不然首位有0
回复 使用道具 举报
包含负数吗?
回复 使用道具 举报
class Demo_skill1
{
        private int num;
        public void ToBin(int num)
        {
                StringBuffer str = new StringBuffer();
                if(num > 0)//这里应该考虑小数和负数
                {
                        while(num > 0)
                        {
                        str.append(num%2);
                        num/=2;
                        }
                        str.reverse();
                        System.out.println(str);
                }
        }
}
回复 使用道具 举报
public static String toBin(int num)
{
        StringBuffer sb = new StringBuffer();
        while(num>0)
        {
                sb.append(num%2);
                num = num /2;
        }
        return sb.reverse();
}
回复 使用道具 举报 1 0
public class ToBinary{
   public static void main(String args[]){

    int i=100;
    String j=Integer.toBinaryString(i);
    System.out.println(j);

}
回复 使用道具 举报
HM_七月 发表于 2015-7-31 05:17
public static void toBin(int num){
         StringBuffer sb=new StringBuffer();
         while(num>0 ...

谢谢啦         
回复 使用道具 举报
yefeidd 发表于 2015-7-31 10:11
class Demo_skill1
{
        private int num;

嗯,谢谢,考虑的话更好一点
回复 使用道具 举报

谢谢               
回复 使用道具 举报

等下,我写了一个小数的。负数还没有写,因为要考虑多少位。
回复 使用道具 举报
package exam1;

/*
* 十进制转二进制。
* 对double数据进行取精度.
* @param value  double数据.
* @param scale  精度位数(保留的小数位数).
* @param roundingMode  精度取值方式.
* @return 精度计算后的数据.
*/


import java.math.BigDecimal;//大数字精确计算

class Demo_skill1
{
        private double num;
        public void ToBin(double num)
        {
                StringBuffer str = new StringBuffer();
                if(num > 0)//这里考虑小数
                {
                        int Inf = (int)num;//提取整数部分
                        BigDecimal ss = new BigDecimal(num);//声明并创建一个大数字类型对象ss
                        BigDecimal tt = new BigDecimal(Inf);//声明并创建一个大数字类型对象tt
                        ss = ss.subtract(tt);//提取小数部分
                        while(Inf > 0)//整数部分转化为二进制
                        {
                        str.append(Inf%2);
                        Inf/=2;
                        }
                        str.reverse();
                       
                        ss = ss.setScale(10,BigDecimal.ROUND_HALF_UP);//取10位精度
                        double xx = ss.doubleValue();//转换为double类型
                        if(xx > 0)//小数部分转化为二进制
                        {
                                str.append('.');
                                double flag = 0.0001;
                                while(flag!=0)
                                {
                                        xx = xx*2;
                                        int rr = (int)xx;
                                        str.append(rr);
                                        BigDecimal yy = new BigDecimal(xx);
                                        BigDecimal zz = new BigDecimal(rr);
                                        yy = yy.subtract(zz);
                                        yy = yy.setScale(10,BigDecimal.ROUND_HALF_UP);//取10位精度
                                        flag = yy.doubleValue();
                                        xx = yy.doubleValue();
                                }
                        }
                       
                        System.out.println(str);
                }
        }
}

public class skill1 {

        public static void main(String[] args) {
               
                Demo_skill1 test = new Demo_skill1();
                test.ToBin(1.125);
        }

}
回复 使用道具 举报
厉害厉害,最喜欢的还是:
  1. public staic void toBin(int i){
  2.     if(i>0){
  3.         toBin(i%2);
  4.         System.out.print(i/2);
  5.     }
  6. }
复制代码

简单明了,就是不支持输入为0了
回复 使用道具 举报
package exam1;

/*
* 十进制转二进制。
* 对double数据进行取精度.
* @param value  double数据.
* @param scale  精度位数(保留的小数位数).
* @param roundingMode  精度取值方式.
* @return 精度计算后的数据.
*/


import java.math.BigDecimal;//大数字精确计算

class Demo_skill1
{
        private double num;
        public void ToBin(double num)
        {
                StringBuffer str = new StringBuffer();
                if(num > 0)//这里考虑小数
                {
                        int Inf = (int)num;//提取整数部分
                        BigDecimal ss = new BigDecimal(num);//声明并创建一个大数字类型对象ss
                        BigDecimal tt = new BigDecimal(Inf);//声明并创建一个大数字类型对象tt
                        ss = ss.subtract(tt);//提取小数部分
                        while(Inf > 0)//整数部分转化为二进制
                        {
                        str.append(Inf%2);
                        Inf/=2;
                        }
                        str.reverse();
                       
                        ss = ss.setScale(10,BigDecimal.ROUND_HALF_UP);//取10位精度
                        double xx = ss.doubleValue();//转换为double类型
                        if(xx > 0)//小数部分转化为二进制
                        {
                                str.append('.');
                                double flag = 0.0001;
                                int kk = 10;//考虑无限循环的问题
                                while(flag!=0&&kk!=0)
                                {
                                        xx = xx*2;
                                        int rr = (int)xx;
                                        str.append(rr);
                                        BigDecimal yy = new BigDecimal(xx);
                                        BigDecimal zz = new BigDecimal(rr);
                                        yy = yy.subtract(zz);
                                        yy = yy.setScale(10,BigDecimal.ROUND_HALF_UP);//取10位精度
                                        flag = yy.doubleValue();
                                        xx = yy.doubleValue();
                                        kk--;
                                }
                        }
                       
                        System.out.println(str);
                }
        }
}

public class skill1 {

        public static void main(String[] args) {
               
                Demo_skill1 test = new Demo_skill1();
                test.ToBin(1.1);
        }

}
回复 使用道具 举报
望版主采纳,代码包括界面功能,可以实现2,8,10,16进制之间任意转换
  1. package test.version01;

  2. import java.awt.FlowLayout;
  3. import java.awt.Font;
  4. import java.awt.GridLayout;
  5. import java.awt.Panel;
  6. import java.awt.event.ActionEvent;
  7. import java.awt.event.ActionListener;

  8. import javax.swing.JButton;
  9. import javax.swing.JComboBox;
  10. import javax.swing.JFrame;
  11. import javax.swing.JLabel;
  12. import javax.swing.JOptionPane;
  13. import javax.swing.JPanel;
  14. import javax.swing.JTextField;

  15. /**
  16. * 程序功能:实现2进制,8进制,10进制,16进制之间的自由转换
  17. *
  18. * @author duanyitao
  19. *
  20. */
  21. public class Main extends JFrame implements ActionListener {

  22.         private static final long serialVersionUID = 1L;

  23.         private Font font = null;

  24.         private JLabel label1 = null;
  25.         private JLabel label2 = null;
  26.         private static JTextField textField1 = null;
  27.         private static JTextField textField2 = null;

  28.         private JPanel panel = null;

  29.         private JComboBox comboBox01 = null;
  30.         private JLabel label02 = null;
  31.         private JComboBox comboBox02 = null;
  32.         Object two = null;
  33.         Object eight = null;
  34.         Object ten = null;
  35.         Object sixteenth = null;
  36.         private JButton button = null;

  37.         public Main() {
  38.                 font = new Font("微软雅黑", Font.BOLD, 16);
  39.                 label1 = new JLabel("需要转换的数:");
  40.                 label1.setHorizontalAlignment(0);
  41.                 label1.setFont(font);
  42.                 label2 = new JLabel("转换后的结果:");
  43.                 label2.setHorizontalAlignment(0);
  44.                 label2.setFont(font);
  45.                 textField1 = new JTextField();
  46.                 textField2 = new JTextField();
  47.                 comboBox01 = new JComboBox();
  48.                 comboBox01.setFont(font);
  49.                 label02 = new JLabel("转");
  50.                 label02.setFont(font);
  51.                 comboBox02 = new JComboBox();
  52.                 comboBox02.setFont(font);

  53.                 two = makeObj("二进制");
  54.                 eight = makeObj("八进制");
  55.                 ten = makeObj("十进制");
  56.                 sixteenth = makeObj("十六进制");

  57.                 comboBox01.addItem(two);
  58.                 comboBox01.addItem(eight);
  59.                 comboBox01.addItem(ten);
  60.                 comboBox01.addItem(sixteenth);

  61.                 comboBox02.addItem(two);
  62.                 comboBox02.addItem(eight);
  63.                 comboBox02.addItem(ten);
  64.                 comboBox02.addItem(sixteenth);

  65.                 panel = new JPanel();
  66.                 panel.setLayout(new FlowLayout());
  67.                 panel.add(comboBox01);
  68.                 panel.add(label02);
  69.                 panel.add(comboBox02);
  70.                 button = new JButton("转换");
  71.                 button.addActionListener(this);
  72.                 button.setFont(font);
  73.                 this.setLayout(new GridLayout(3, 3));
  74.                 this.add(label1);
  75.                 this.add(textField1);
  76.                 this.add(panel);
  77.                 this.add(new Panel());
  78.                 this.add(new Panel());
  79.                 this.add(button);
  80.                 this.add(label2);
  81.                 this.add(textField2);

  82.                 this.setTitle("进制转换");
  83.                 this.setSize(675, 130);
  84.                 this.setDefaultCloseOperation(EXIT_ON_CLOSE);
  85.                 this.setLocationRelativeTo(null);
  86.                 this.setResizable(false);
  87.         }

  88.         private Object makeObj(final String item) {
  89.                 return new Object() {
  90.                         public String toString() {
  91.                                 return item;
  92.                         }
  93.                 };
  94.         }

  95.         public static void main(String[] args) {
  96.                 new Main().setVisible(true);
  97.         }

  98.         @Override
  99.         public void actionPerformed(ActionEvent e) {
  100.                 if (e.getSource().equals(button)) {
  101.                         try {
  102.                                 caculate();
  103.                         } catch (Exception e2) {
  104.                                 JOptionPane.showMessageDialog(this, "数据格式错误,请重新输入!");
  105.                                 textField1.setText("");
  106.                                 textField2.setText("");
  107.                         }
  108.                 }
  109.         }

  110.         private void caculate() {
  111.                 String str01 = comboBox01.getSelectedItem() + "";
  112.                 if (str01.equals("二进制")) {
  113.                         caculateTo(2);
  114.                 } else if (str01.equals("八进制")) {
  115.                         caculateTo(8);
  116.                 } else if (str01.equals("十进制")) {
  117.                         caculateTo(10);
  118.                 } else if (str01.equals("十六进制")) {
  119.                         caculateTo(16);
  120.                 }
  121.         }

  122.         private void caculateTo(int n) {
  123.                 String str02 = comboBox02.getSelectedItem() + "";
  124.                 if (str02.equals("二进制")) {
  125.                         changeAndSet(n, 2);
  126.                 } else if (str02.equals("八进制")) {
  127.                         changeAndSet(n, 8);
  128.                 } else if (str02.equals("十进制")) {
  129.                         changeAndSet(n, 10);
  130.                 } else if (str02.equals("十六进制")) {
  131.                         changeAndSet(n, 16);
  132.                 }
  133.         }

  134.         public static void changeAndSet(int src, int dest) {
  135.                 textField2.setText(Integer.toString(
  136.                                 Integer.parseInt(textField1.getText(), src), dest));
  137.         }

  138. }
复制代码
回复 使用道具 举报
段燚涛 发表于 2015-7-31 15:52
望版主采纳,代码包括界面功能,可以实现2,8,10,16进制之间任意转换

算法有问题的吧。负数转换成二进制是错误的。
回复 使用道具 举报
yefeidd 发表于 2015-8-1 10:17
算法有问题的吧。负数转换成二进制是错误的。

嗯,确实是没有考虑到负数的情况,谢谢楼主指出错误。能一起进步,很高兴。
回复 使用道具 举报
您需要登录后才可以回帖 登录 | 加入黑马