answersLogoWhite

0

First of: I can do that in a simple for loop:

public static long bin2dec(char[] input) {

long dec = 0; // return value

for (int i = 0; i < input.length; i++) // No curly braces because its just one line; IT WILL WORK, but you can add curly braces

if (input[i] - 48 > 1) return null; // same here

else dec = (dec << 1) + input[i] - 48; // and here

return dec;

}

The recursion variant of the bin2dec can crash the stack:

public static long bin2dec(char[] input) {

char[] nextInput = new char[input.length - 1]; // char-array for the next recursion

for (int i = 0; i < newInput.length; i++) nextInput[i] = input[i];

return (input[input.length - 1] - 48) + bin2dec(nexInput) << 1;

}

User Avatar

Wiki User

12y ago

What else can I help you with?

Continue Learning about Engineering

Write a Program to convert decimal number into hexadecimal no without using function and string?

This is not a question.


How do you write a c program to convert binary to decimal by using while statement?

write a c++ program to convert binary number to decimal number by using while statement


Write a program that prints a table of the binary octal and hexadecimal equivalents of the decimal numbers in the range 1 through to 256?

import java.util.Scanner; public class NumberSystem { public void displayConversion() { Scanner input = new Scanner(System.in); System.out.printf("%-20s%-20s%-20s%-20s\n", "Decimal", "Binary", "Octal", "Hexadecimal"); for ( int i = 1; i &lt;= 256; i++ ) { String binary = Integer.toBinaryString(i); String octal = Integer.toOctalString(i); String hexadecimal = Integer.toHexString(i); System.out.format("%-20d%-20s%-20s%-20s\n", i, binary, octal, hexadecimal); } } // returns a string representation of the decimal number in binary public String toBinaryString( int dec ) { String binary = " "; while (dec &gt;= 1 ) { int value = dec % 2; binary = value + binary; dec /= 2; } return binary; } //returns a string representation of the number in octal public String toOctalString( int dec ) { String octal = " "; while ( dec &gt;= 1 ) { int value = dec % 8; octal = value + octal; dec /= 8; } return octal; } public String toHexString( int dec ) { String hexadecimal = " "; while ( dec &gt;= 1 ) { int value = dec % 16; switch (value) { case 10: hexadecimal = "A" + hexadecimal; break; case 11: hexadecimal = "B" + hexadecimal; break; case 12: hexadecimal = "C" + hexadecimal; break; case 13: hexadecimal = "D" + hexadecimal; break; case 14: hexadecimal = "E" + hexadecimal; break; case 15: hexadecimal = "F" + hexadecimal; break; default: hexadecimal = value + hexadecimal; break; } dec /= 16; } return hexadecimal; } public static void main( String args[]) { NumberSystem apps = new NumberSystem(); apps.displayConversion(); } }


Write a C program that takes a binary file as input and finds error check using different mechanisms?

write a c program that takes a binary file as input and finds error check using different mechanisms.


How do you convert hexadecimal 542 to binary?

Write each hexadecimal digit straight into binary: 5 = 0101 4 = 0100 2 = 0010 So 0x542 = 0101 0100 0010 = 10101000010 (without the spaces). Hex digit to binary conversion: 0 = 0000, 1 = 0001, 2 = 0010, 3 = 0011 4 = 0100, 5 = 0101, 6 = 0110, 7 = 0111 8 = 1000, 9 = 1001, a = 1010, b = 1011 c = 1100, d = 1101, e = 1110, f = 1111

Related Questions

Write a program to convert a 2 digit BCD number into hexadecimal number?

Write a program to convert a 2-digit BCD number into hexadecimal


How do you write the number 90 in binary?

90 = 1011010 (or 5A in hexadecimal).


2 Write a program to convert a 2-digit BCD number into hexadecimal?

WRITE A PROGRAM TO CONVERT A 2-DIGIT bcd NUMBER INTO HEXADECIMAL


Why are MAC addresses written as six pair of Hexadecimal numbers?

Any data is stored internally in the computer as binary digits, but those are "bulky" - you need 4 binary digits for every hexadecimal digit, so hexadecimal is really a kind of shortcut to write out binary numbers.Decimal is another option, but conversion between binary and decimal is more cumbersome than with hexadecimal. Therefore, for the new IP addresses (IP version 6), they decided to write them down in hexadecimal, instead of the decimal that is used for IPv4.


How do you write one billion and thirteen?

Binary: 1001010100000010111110010010000010 Decimal: 1,000,000,013 Hexadecimal: 0x2540BE482


Write a program that prints a table of the binary, octal and hexadecimal equivalents of the decimal numbers in the range 1 through 256?

import java.util.Scanner; public class NumberSystem { public void displayConversion() { Scanner input = new Scanner(System.in); System.out.printf("%-20s%-20s%-20s%-20s\n", "Decimal", "Binary", "Octal", "Hexadecimal"); for ( int i = 1; i &lt;= 256; i++ ) { String binary = Integer.toBinaryString(i); String octal = Integer.toOctalString(i); String hexadecimal = Integer.toHexString(i); System.out.format("%-20d%-20s%-20s%-20s\n", i, binary, octal, hexadecimal); } } // returns a string representation of the decimal number in binary public String toBinaryString( int dec ) { String binary = " "; while (dec &gt;= 1 ) { int value = dec % 2; binary = value + binary; dec /= 2; } return binary; } //returns a string representation of the number in octal public String toOctalString( int dec ) { String octal = " "; while ( dec &gt;= 1 ) { int value = dec % 8; octal = value + octal; dec /= 8; } return octal; } public String toHexString( int dec ) { String hexadecimal = " "; while ( dec &gt;= 1 ) { int value = dec % 16; switch (value) { case 10: hexadecimal = "A" + hexadecimal; break; case 11: hexadecimal = "B" + hexadecimal; break; case 12: hexadecimal = "C" + hexadecimal; break; case 13: hexadecimal = "D" + hexadecimal; break; case 14: hexadecimal = "E" + hexadecimal; break; case 15: hexadecimal = "F" + hexadecimal; break; default: hexadecimal = value + hexadecimal; break; } dec /= 16; } return hexadecimal; } public static void main( String args[]) { NumberSystem apps = new NumberSystem(); apps.displayConversion(); } }


How do you convert hexadecimal numbers into binary numbers?

Binary to hexadecimal is very easy because hexadecimal numbers are designed specifically so that each hex digit is exactly 4 bits (i.e. 16 different values). So if you had this binary number: binary: 100011011011110101000100001 You could put in commas every four places (starting on the left): binary: 100,0110,1101,1110,1010,0010,0001 Then you could write the hex values immediately below: binary: 0100,0110,1101,1110,1010,0010,0001 hex: 4 6 D E A 2 1 and the hex value would be 46DEA21.


How do you write the program for the hexadecimal 210?

int main (void) { puts ("210H"); return 0; }


Write a Program to convert decimal number into hexadecimal no without using function and string?

This is not a question.


Write a C program to convert hexadecimal number into decimal number?

pongada punda vayanungala ..................


How do you write a c program to convert binary to decimal by using while statement?

write a c++ program to convert binary number to decimal number by using while statement


Write a program that prints a table of the binary octal and hexadecimal equivalents of the decimal numbers in the range 1 through to 256?

import java.util.Scanner; public class NumberSystem { public void displayConversion() { Scanner input = new Scanner(System.in); System.out.printf("%-20s%-20s%-20s%-20s\n", "Decimal", "Binary", "Octal", "Hexadecimal"); for ( int i = 1; i &lt;= 256; i++ ) { String binary = Integer.toBinaryString(i); String octal = Integer.toOctalString(i); String hexadecimal = Integer.toHexString(i); System.out.format("%-20d%-20s%-20s%-20s\n", i, binary, octal, hexadecimal); } } // returns a string representation of the decimal number in binary public String toBinaryString( int dec ) { String binary = " "; while (dec &gt;= 1 ) { int value = dec % 2; binary = value + binary; dec /= 2; } return binary; } //returns a string representation of the number in octal public String toOctalString( int dec ) { String octal = " "; while ( dec &gt;= 1 ) { int value = dec % 8; octal = value + octal; dec /= 8; } return octal; } public String toHexString( int dec ) { String hexadecimal = " "; while ( dec &gt;= 1 ) { int value = dec % 16; switch (value) { case 10: hexadecimal = "A" + hexadecimal; break; case 11: hexadecimal = "B" + hexadecimal; break; case 12: hexadecimal = "C" + hexadecimal; break; case 13: hexadecimal = "D" + hexadecimal; break; case 14: hexadecimal = "E" + hexadecimal; break; case 15: hexadecimal = "F" + hexadecimal; break; default: hexadecimal = value + hexadecimal; break; } dec /= 16; } return hexadecimal; } public static void main( String args[]) { NumberSystem apps = new NumberSystem(); apps.displayConversion(); } }