没有预定义方法(JAVA)的二进制到六进制十进制 [英] binary to hexa decimal without predefined method (JAVA)

查看:92
本文介绍了没有预定义方法(JAVA)的二进制到六进制十进制的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在寻找一种将二进制数字转换为十六进制(JAVA)的方法.问题是它不能使用预定义的方法来完成,而我只是不知道如何去做.我已经尝试了一些方法,但是使我无法接受包含字符的十六进制数.

I'm looking for a method that converts binary numbers to hexa decimals (JAVA). Problem is that it can't be done with a predefined method and I just don't know how to do it. I've tried a few things but it throws me off that hexa decimals include chars.

提前谢谢!

推荐答案

根据您的要求,首先必须将二进制no转换为十进制,然后转换为十六进制.因此,请尝试按照您的要求运行该程序:

For your requirement first of all you have to convert binary no into decimal and then into hexadecimal. So please try this program it works as per your requirement :

import java.util.Scanner;

public class BinaryToHexa
{
    public static void main(String args[])
    {
        int binnum, rem;
        String hexdecnum="";
        int decnum=0;            

        char hex[]={'0','1','2','3','4','5','6','7','8','9','A','B','C','D','E','F'};
        Scanner scan = new Scanner(System.in);

        System.out.print("Enter Binary Number : ");
        binnum = scan.nextInt();        

        // converting the number in decimal format
        int i=0;      

        while(binnum>0)
        {
            rem = binnum%10;
            binnum=binnum/10;
            decnum = decnum + (int)(rem*Math.pow(2,i));
            i++;
        }     

        // converting the number in hexadecimal format
        while(decnum>0)
        {
            rem = decnum%16;
            hexdecnum = hex[rem] + hexdecnum;
            decnum = decnum/16;
        }

        System.out.print("Equivalent Hexadecimal Value is :\n");
        System.out.print(hexdecnum);

    }
}

如果您有任何疑问,请告诉我.

If you have any doubt please let me know.

谢谢...

这篇关于没有预定义方法(JAVA)的二进制到六进制十进制的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆