Java中将整数值转换为十六进制的最佳代码

发布于 2024-09-01 11:05:12 字数 109 浏览 1 评论 0原文

我需要将整数值转换为十六进制。

我已经完成了一些逻辑性的工作,但我想要优化的解决方案。

编辑:抱歉,我忘记发布我不允许使用任何内置函数。

I need to convert integer value into hexadecimal.

I have done with some logical, but I want the optimized solutions.

EDIT : Sorry I forgot to post that I am not allowed to use any in-built functions.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

喜你已久 2024-09-08 11:05:15

简单:

String hex = Integer.toHexString(int);

基本上,它的作用是创建一个新字符串,然后调用 Integer 类中名为 toHexString 的方法,该方法需要一个 int arg。
因此,将您想要更改的 int 传递到此方法中,您将得到一个带有 int 的十六进制版本的字符串。

您可以将十六进制值放入 int 类型中,但据我所知,当您进行十六进制转换时,无法从 int 类型转换为另一种 int 类型。

请记住,您返回的值是字符串,因此您不能修改该值,否则您将收到数字格式异常。

Easy:

String hex = Integer.toHexString(int);

Basically what this does is creating a new string, and then calling a method from the Integer class called toHexString which needs an int arg.
So pass the int you wanna change into this method and you'll get a String with the hexadecimal version of your int back.

You can put hexadecimal values in int types, but you cannot convert from int type to another int type, as far as i know, when you are doing hexadecimal conversions.

Remember that the value you get back is a String, so you cannot modify the value, otherwise you'll get an number format exception.

深海里的那抹蓝 2024-09-08 11:05:15

假设您出于某种原因不想使用内置的 toHexString,这里有一种非常有效的方法:

 public static char toHexChar(int i) {
  i&=15;
  return (i<10)? (char)(i+48) : (char)(i+55);
 }

 public static String toHexString(int n) {
  char[] chars=new char[8];
  for (int i=0; i<8; i++) {
   chars[7-i]=toHexChar(n);
   n>>=4;
  };
  return new String(chars);
 }

Assuming you don't want to use the built in toHexString for some reason, here's one pretty efficient way to do it:

 public static char toHexChar(int i) {
  i&=15;
  return (i<10)? (char)(i+48) : (char)(i+55);
 }

 public static String toHexString(int n) {
  char[] chars=new char[8];
  for (int i=0; i<8; i++) {
   chars[7-i]=toHexChar(n);
   n>>=4;
  };
  return new String(chars);
 }
呆橘 2024-09-08 11:05:15

那么接下来看看 Integer.toHexString(int) 的实现。以下代码摘自java标准库中的Integer类。

public class Test {

    final static char[] digits = {
        '0' , '1' , '2' , '3' , '4' , '5' ,
        '6' , '7' , '8' , '9' , 'a' , 'b' ,
        'c' , 'd' , 'e' , 'f'
    };

    private static String intAsHex(int i) {
        char[] buf = new char[32];
        int charPos = 32;
        int radix = 1 << 4;
        int mask = radix - 1;
        do {
            buf[--charPos] = digits[i & mask];
            i >>>= 4;
        } while (i != 0);

        return new String(buf, charPos, (32 - charPos));
    }


    public static void main(String... args) {
        System.out.println(intAsHex(77));
    }
}

输出: 4d

Well then have a look at the implementation of Integer.toHexString(int). The following code is extracted from the Integer class in the java standard library.

public class Test {

    final static char[] digits = {
        '0' , '1' , '2' , '3' , '4' , '5' ,
        '6' , '7' , '8' , '9' , 'a' , 'b' ,
        'c' , 'd' , 'e' , 'f'
    };

    private static String intAsHex(int i) {
        char[] buf = new char[32];
        int charPos = 32;
        int radix = 1 << 4;
        int mask = radix - 1;
        do {
            buf[--charPos] = digits[i & mask];
            i >>>= 4;
        } while (i != 0);

        return new String(buf, charPos, (32 - charPos));
    }


    public static void main(String... args) {
        System.out.println(intAsHex(77));
    }
}

Output: 4d

囍笑 2024-09-08 11:05:15

检查这个

public class IntToHexa {
    public static void main(java.lang.String args[]){
        /*
         * Here we need an integer to convert.
         * [1]You can pass as command line argument
         * [2]You can get as input from console
         * [3]Take a constant. Here I'm taking a constant
         */
        int intToConvert = 450;
        java.lang.StringBuilder convertedHexa = new java.lang.StringBuilder("");
        while (intToConvert > 15){
            /*
         * If the reminder is less than 10, add the remainder. else get the equivalent hexa code
         * Here I'm getting the character code and adding the charater to the hexa string.
         * For that I'm getting the difference between the reminder and 10.
         * For example, if the reminder is 13, the reminder will be 3.
         * Then add that difference to 65. In this example, it will become 68.
         * Finally, get the quivalent char code of the result number. Here it will be D.
         * Same for number, I'm adding it to 48
         */
            convertedHexa.append(intToConvert % 16 < 10 ? ((char)(48 + (intToConvert % 16))) : ((char)(65 + (intToConvert % 16 - 10))));
            intToConvert /= 16;
        }
        convertedHexa.append(intToConvert % 16 < 10 ? ((char)(48 + (intToConvert % 16))) : ((char)(65 + (intToConvert % 16 - 10))));
        java.lang.System.out.println(convertedHexa.reverse());
    }
}

Check this

public class IntToHexa {
    public static void main(java.lang.String args[]){
        /*
         * Here we need an integer to convert.
         * [1]You can pass as command line argument
         * [2]You can get as input from console
         * [3]Take a constant. Here I'm taking a constant
         */
        int intToConvert = 450;
        java.lang.StringBuilder convertedHexa = new java.lang.StringBuilder("");
        while (intToConvert > 15){
            /*
         * If the reminder is less than 10, add the remainder. else get the equivalent hexa code
         * Here I'm getting the character code and adding the charater to the hexa string.
         * For that I'm getting the difference between the reminder and 10.
         * For example, if the reminder is 13, the reminder will be 3.
         * Then add that difference to 65. In this example, it will become 68.
         * Finally, get the quivalent char code of the result number. Here it will be D.
         * Same for number, I'm adding it to 48
         */
            convertedHexa.append(intToConvert % 16 < 10 ? ((char)(48 + (intToConvert % 16))) : ((char)(65 + (intToConvert % 16 - 10))));
            intToConvert /= 16;
        }
        convertedHexa.append(intToConvert % 16 < 10 ? ((char)(48 + (intToConvert % 16))) : ((char)(65 + (intToConvert % 16 - 10))));
        java.lang.System.out.println(convertedHexa.reverse());
    }
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文