Java实现十进制的整数转为二进制的字符串

1.描述:输入一个十进制的整数,返回该整数对应二进制数的字符串

2.JDK的实现(java.lang.Integer#toBinaryString)

  public static String toBinaryString(int i) {
        return toUnsignedString0(i, 1);
   }

    /**
     * Convert the integer to an unsigned number.
     */
    private static String toUnsignedString0(int val, int shift) {
        // assert shift > 0 && shift <=5 : "Illegal shift value";
        int mag = Integer.SIZE - Integer.numberOfLeadingZeros(val);
        int chars = Math.max(((mag + (shift - 1)) / shift), 1);
        char[] buf = new char[chars];

        formatUnsignedInt(val, shift, buf, 0, chars);

        // Use special constructor which takes over "buf".
        return new String(buf, true);
    }

    /**
     * Format a long (treated as unsigned) into a character buffer.
     * @param val the unsigned int to format
     * @param shift the log2 of the base to format in (4 for hex, 3 for octal, 1 for binary)
     * @param buf the character buffer to write to
     * @param offset the offset in the destination buffer to start at
     * @param len the number of characters to write
     * @return the lowest character  location used
     */
     static int formatUnsignedInt(int val, int shift, char[] buf, int offset, int len) {
        int charPos = len;
        int radix = 1 << shift;
        int mask = radix - 1;
        do {
            buf[offset + --charPos] = Integer.digits[val & mask];
            val >>>= shift;
        } while (val != 0 && charPos > 0);

        return charPos;
    }

    final static char [] DigitTens = {
        '0', '0', '0', '0', '0', '0', '0', '0', '0', '0',
        '1', '1', '1', '1', '1', '1', '1', '1', '1', '1',
        '2', '2', '2', '2', '2', '2', '2', '2', '2', '2',
        '3', '3', '3', '3', '3', '3', '3', '3', '3', '3',
        '4', '4', '4', '4', '4', '4', '4', '4', '4', '4',
        '5', '5', '5', '5', '5', '5', '5', '5', '5', '5',
        '6', '6', '6', '6', '6', '6', '6', '6', '6', '6',
        '7', '7', '7', '7', '7', '7', '7', '7', '7', '7',
        '8', '8', '8', '8', '8', '8', '8', '8', '8', '8',
        '9', '9', '9', '9', '9', '9', '9', '9', '9', '9',
        } ;

 

3.自定义实现

 

public static String toBinaryString(Integer number) {
        StringBuilder sb = new StringBuilder();
        for (int n = number; n > 0 ; n /= 2) {
            sb.append(n %  2);
        }
        return sb.toString();
    }

 

转载于:https://www.cnblogs.com/assange/p/7414550.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
可以使用以下方法将输入的十进制整数转换为二进制字符串: 1. 将输入的十进制整数除以2,得到商和余数。 2. 将余数存储在一个字符数组或字符串中。 3. 将商作为下一次计算的输入。 4. 重复步骤1-3,直到商为0。 5. 反转字符数组或字符串,得到二进制字符串。 以下是一个示例程序: ```c #include <stdio.h> #include <string.h> int main() { int decimal; char binary[32]; int i = 0; printf("Enter a decimal number: "); scanf("%d", &decimal); while (decimal != 0) { int remainder = decimal % 2; binary[i++] = remainder + '0'; decimal /= 2; } binary[i] = '\0'; printf("Binary equivalent: %s\n", strrev(binary)); return 0; } ``` 在上面的程序中,我们使用了两个变量:`decimal` 存储输入的十进制整数,`binary` 存储转换后的二进制字符串。我们还定义了一个整数变量 `i`,用于追踪 `binary` 数组中下一个可用的位置。 在主函数中,我们首先提示用户输入一个十进制整数,并使用 `scanf()` 函数将其读入 `decimal` 变量中。接下来,我们使用一个循环来将 `decimal` 转换为二进制字符串。在每个循环迭代中,我们计算 `decimal` 的余数并将其存储在 `binary` 数组中。我们还将 `decimal` 的值除以2,以便在下一次迭代中使用商作为输入。 循环结束后,我们在 `binary` 数组的末尾添加一个空字符 (`'\0'`),以便将其转换为一个字符串。最后,我们使用 `strrev()` 函数将 `binary` 字符串反转,并将其打印输出。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值