简体   繁体   English

没有预定义方法的二进制到十六进制(JAVA)

[英]binary to hexa decimal without predefined method (JAVA)

I'm looking for a method that converts binary numbers to hexa decimals (JAVA).我正在寻找一种将二进制数转换为十六进制数 (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.我已经尝试了一些东西,但它让我觉得十六进制十进制包括字符。

thanks in advance!提前致谢!

It's really a crappy question.这真的是一个很糟糕的问题。 You should explain what you have come up with and show the code you've tried so far.你应该解释你想出的东西并展示你迄今为止尝试过的代码。

So here's a binary number:所以这是一个二进制数:

0101111010110010

Split it into groups of four bits (a bit is a binary digit, ie 1 or 0):将其分成四位一组(一位是二进制数字,即 1 或 0):

0101 1110 1011 0010

Now the funny thing is that each group of four bits has a maximum value of....现在有趣的是,每组四位的最大值为......

1111 = 8 + 4 + 2 + 1 = 15

Does that ring a bell?这是否敲响了警钟? Here are the 'digits' in hexadecimal :以下是十六进制的“数字”:

1, 2, 3, 4, 5, 6, 7, 8, 9, 10, A, B, C, D, E, F

What's the maximum value of a single hexadecimal digit?单个十六进制数字的最大值是多少?

15

So this means you can simply translate each group of four bits into a hexadecimal digit:所以这意味着您可以简单地将每组四位转换为十六进制数字:

0101 1110 1011 0010

4+1 8+4+2 8+2+1 2

5    14   11   2

5    E    B    2

5EB2  

What is the exact issue or problem here?这里的确切问题是什么?

Use Integer.toHexString(num) to convert使用 Integer.toHexString(num) 进行转换

For your requirement first of all you have to convert binary no into decimal and then into hexadecimal.对于您的要求,首先您必须将二进制 no 转换为十进制,然后再转换为十六进制。 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.如果您有任何疑问,请告诉我。

Thanks...谢谢...

Try this function from my library out.从我的图书馆试试这个功能。 You do not do any calculation with this function.您不使用此函数进行任何计算。 Only string compare, and you can convert as much binary to hexadecimal as you like.只进行字符串比较,您可以根据需要将尽可能多的二进制转换为十六进制。 As long as the limitation on how long a string can be.只要限制字符串的长度即可。

public static String binhexZ(String input)
{
    String map = "0000,0,0001,1,0010,2,0011,3,0100,4,0101,5,0110,6,0111,7,1000,8,1001,9,1010,A,1011,B,1100,C,1101,D,1110,E,1111,F";
    String output = "";
    int i = 0;

    while (input.length() % 4 != 0){
        input = "0" + input;    
    }

    while (i < input.length()){
        output += map.charAt(map.indexOf(input.substring(i, i + 4)) + 5);
        i = i + 4;
    }

    output = output.replaceAll("^0+", "");
    output = output.length() < 1? "0" : output;

    return output;
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM