C RUBY-ON-RAILS MYSQL ASP.NET DEVELOPMENT RUBY .NET LINUX SQL-SERVER REGEX WINDOWS ALGORITHM ECLIPSE VISUAL-STUDIO STRING SVN PERFORMANCE APACHE-FLEX UNIT-TESTING SECURITY LINQ UNIX MATH EMAIL OOP LANGUAGE-AGNOSTIC VB6 MSBUILD

C++ Simple Converting from Binary to Decimal

By : user2956635
Date : November 22 2020, 10:56 AM
I hope this helps you . In you can copy structs, unions etc but can not copy arrays directly. Becuase of this reason, you can not return an array and assign it to another array.
Instead of somearr[8] you can use std::array or std::vector. These stl containers can be copied and thus will solve your problem.
code :

Share :

Converting decimal to binary and then binary to unknown format?

By : Grodahn
Date : March 29 2020, 07:55 AM
I hope this helps . First of all, 645 in base 2 is 1010000101. (Your code gives an incorrect result for some reason).
Once you realize that, I think it's fairly simple to see that the "unknown format" can be reached by simply breaking the binary value into chunks of 8 bits (adding 0's at the beginning where necessary) to give your intermediate binary format, and then converting each chunk (byte) into decimal (base 10).

iProblems with substr, integers and loops in converting decimal to binary and binary to decimal

By : user2970766
Date : March 29 2020, 07:55 AM
To fix this issue Finally, I've managed to edit my code :)
to Konstantinos Chalkias I found out what I had to do :D
code :
``````package decimalBinary;

import java.io.IOException;

public class DecBin {
public static void main(String[] args) throws NumberFormatException, IOException {
int conargs1,conargs2;
// conargs1 is input
// conargs2 is mode, if = 1 then dec2bin, if = 2 then bin2dec
System.out.println("Enter number: ");
System.out.println("Enter mode: ");
System.out.println(conargs2==1 ? "Dec2Bin" : (conargs2==2 ? "Bin2Dec" : "none"));
if (args.length == 2){ // TO BINARY
if(conargs2==1){
System.out.println(DecBin.dec2bin(String.valueOf(conargs1)));
}else if (conargs2==2){ // TO DECIMAL
System.out.println(DecBin.bin2dec(String.valueOf(conargs1)));
}
System.exit(0);
}
}

public static String dec2bin(String arg){
String out = null;
String tmp;
long i, x;
int maxpower = 30;
x = Integer.parseInt(arg);

if (x == 0){
return "0";
}else if (x > 0){ // positive decimals
if (x > Math.pow(2, maxpower)) {
return "should be no larger than " + String.valueOf(2 ^ maxpower);
}
out = "";

int binary[] = new int[30];
int index = 0;
while(x > 0){
binary[index++] = (int) (x % 2);
x = x/2;
}

for(i = index-1;i >= 0;i--){
out = out+binary[(int) i];
}
}else{ // negative decimals
// x = -x;
x = Math.abs(x); // convert positive of decimal to binary
if (x > Math.pow(2, maxpower)) {
return "should be no larger than " + String.valueOf(2 ^ maxpower);
}
long xBinaryPositive;
xBinaryPositive = Long.parseLong(DecBin.dec2bin(String.valueOf(x)));
String xBinaryInverted;
out = "";

xBinaryInverted = String.valueOf(xBinaryPositive).replace('0', '2').replace('1', '0').replace('2', '1');
long xBinaryInvertedIntVal;
xBinaryInvertedIntVal = DecBin.bin2dec(xBinaryInverted);
out = DecBin.dec2bin(String.valueOf(xBinaryInvertedIntVal+1));
}
return out;
}

public static long bin2dec(String binaryInput){
long binary = Long.parseLong(binaryInput);
long decimal = 0;
long power = 0;
while(true){
if(binary == 0){
break;
} else {
long tmp = binary % 10;
decimal += tmp * Math.pow(2, power);
binary = binary/10;
power++;
}
}
return decimal;
}
}
``````

Arduino Converting Decimal to Binary to Decimal

By : user6801869
Date : March 29 2020, 07:55 AM
seems to work fine Since you are going through an array 0-7 of 1s, you should be using bit shifting:
code :
``````int convertBinToDec(boolean Bin[]) {
int ReturnInt = 0;
for (int i = 0; i < 8; i++) {
if (Bin[7 - i]) {
Serial.print("Set Bit ");
Serial.print(i);
ReturnInt += 1<<i;
Serial.print(" ==> ");
Serial.print(1<<i);
Serial.print(", ");
}
}
return ReturnInt;
}

DecToBin: 1 -> 00000001 -> Set Bit 0 ==> 1, 1
DecToBin: 2 -> 00000010 -> Set Bit 1 ==> 1, 2
DecToBin: 3 -> 00000011 -> Set Bit 0 ==> 1, Set Bit 1 ==> 1, 3
DecToBin: 4 -> 00000100 -> Set Bit 2 ==> 2, 4
DecToBin: 5 -> 00000101 -> Set Bit 0 ==> 1, Set Bit 2 ==> 2, 5
DecToBin: 6 -> 00000110 -> Set Bit 1 ==> 1, Set Bit 2 ==> 2, 6
DecToBin: 7 -> 00000111 -> Set Bit 0 ==> 1, Set Bit 1 ==> 1, Set Bit 2 ==> 2, 7
DecToBin: 8 -> 00001000 -> Set Bit 3 ==> 4, 8
``````

converting the binary to decimal when binary is stored in an array

Date : March 29 2020, 07:55 AM
wish of those help this is the code I have written but it gives incorrect answers and I can't figure out why , For arbitrary sized byte[] you can use BigInteger.
code :
``````public void test(String[] args) {
byte[] b = new byte[] {1,2,3,4,5};
BigInteger bi = new BigInteger(b);
System.out.println(bi.toString(10));
}
``````

Python program: a tool to convert binary numbers to decimal, and decimal numbers to binary, and a simple text-only menu

By : Raju
Date : March 29 2020, 07:55 AM
may help you . Well, there you go :P Decided to use a switch object because it was interesting. However i'm finding it tough to exit the switch with input '5'
code :
``````class switch(object):
value = None
def __new__(class_, value):
class_.value = value
return True

def case(*args):
return any((arg == switch.value for arg in args))

def bintodec(binnum):
return int(str(binnum), 2)

try:
first = int(first,2)
second = int(second,2)
except:
pass
print('result')
print(int(first) + int(second))
print('')

def sub(first, second):
try:
first = int(first,2)
second = int(second,2)
except:
pass
print('result')
print(int(second) - int(first))
print('')

while True:
print ("What do you want to do?")
print ("1. Enter the first number")
print ("2. Enter the second number")
print ("3. Add the two numbers together")
print ("4. Subtract the second number from the first")
print ("5. Exit the program\n")

if case(1):
ask_for_1_num = input("Enter the first number\n")
break
if case(2):
ask_for_2_num = input("Enter the second number\n")
break
if case(3):
break
if case(4):