Home PHP C# C++ Android Java Javascript Python IOS SQL HTML Categories

Decimal to binary/hex using arrays


The problem is after the first loop, i is one greater than the last index. Just for example, say you enter 1: the do...while loop is entered, the digit 1 is put in array index 0, then i is incremented to 1.

Then, in the second loop, both indexes 1 and 0 are printed. You can solve this by decrementing i before entering this loop:

while (i >= 0){...}

You should be doing something like that anyway, because if you ended up using all 32 digits, you would try to access bin[32] and the program may crash or output gibberish.


The first loop's condition is infinite:

for (i = 0; i >= 0; i++){...}

It should be the same as your condition in binary:

for (i = 0; numb != 0; i++){...}

But you are not done yet because I've noticed you also have a bug in your printing:

if (hex[i] >= 10)
    switch (hex[i])
        case 10:
            cout << "A";
cout << hex[i];

If hex[i] is greater than or equal to 10, it gets printed twice, once as a hex letter and once as a decimal number. To solve this you could, for example, use continue instead of break in your switch (to skip the second print), or use else:

if (hex[i] >= 10)
    switch (hex[i])
        case 10:
            cout << "A";
    cout << hex[i];

You also need to make the same correction as in binary:

// decrementing i before entering the
//   vvv
for (i--; i >= 0; i--){...}

Your revision is not correct, hex should not have a nested loop. It was fine before, just with the corrections I've pointed out.

Categories : C++

Related to : Decimal to binary/hex using arrays
Fault in decimal to binary in c
When you get to the last (most significant) bit of 1024, the value of a is 10000000000 (10^10). Except it isn't. Because int is a 32-bit integer, the value of a overflows (twice) and ends up being 1410065408.

Categories : C
RegExp to return decimal number with optional 2-4 decimal dights
I think you are looking for something like this: .d{2}(0[1-9]|[1-9]{1,2})? starts with a dot and 2 digits .d{2} then an optional 0 followed by a digit 0[1-9] OR one or two non-zero digits [1-9]{1,2} Matches your examples correctly. If you want digits in front (your question states decimal number) you could add d* in front of the .... if you need anything more specific you'll have to clarif

Categories : Regex
Javascript convert decimal power to regular decimal
The expression you showed above, Math.pow(10, 2), actually evaluates to 100. But if you are given the string 1e+2, you can change it into 100 by calling parseFloat(): var s = '1e+2', // This is a string in scientific notation. x = parseFloat(s); // Here we convert it to the more widespread notation. document.write('"'+s+'" -> '+x); // Let's print out the values for testing pur

Categories : Javascript
Finding arrays within arrays
I don't know a great algorithm for this, but the brute force solution is pretty simple in Ruby: use the - operator to find the complement of one array in another, then check if it's empty. With casting so it works with hashes too, the code is something like def superset?(ary1, ary2) ary1 != ary2 && (ary2.to_a - ary1.to_a) == [] end def maximal_sets(arrays) arrays.reject{ |ary2| array

Categories : Ruby
C# 1 Decimal to 2 Decimals
looks like a formatting problem.. so why not simply use string.format? 1 line of code: (based on this post Using String Format to show decimal upto 2 places or simple integer) var fixed = string.Format("{0:0.00}", oldDecimalValue); and please make sure that your variables names don;y start with numeric character. instead of using: double 1decimal = 0.2; double 2decimalsOrMore = 0.33333;

Categories : C#
Recently Add
VS2013 - Error with multiple includes of same header
Multiplying two large integers stored as integer vectors
How Do I Create a max Functor?
Eigen sparse matrix multiplications seem to compute full matrix
zlib compress() produces awful compression rate
How to delete a window?
passing variable without a name to function
OMNeT++ disassembling received messages
How do I ignore certain file types with cppcheck?
Posting a message to UI from business logic
STL algorithm/functional
su -c replacement using c++
How to use a sdk (non-ros header/lib/binaries) in ros package?
using std::chrono::high_resolution_clock to write a frame 30 times per second
Extracting ints and shorts from a struct using AVX?
Finding Bridges in a graph C++ (BOOST)?
Qt Creator cannot find library with custom cmake step
running visual studio release build .exe file in different machines
class oop realions cpp
Declare interface and Implement the interface using multi-inheritance in C++
How to access all elements of boost::multi_index::multi_index_container<> by their indices?
Understanding what a double pointer to a reference does C++
ld: symbol(s) not found for architecture x86_64 when compiling c++
Disconnected node during Graph traversal
Const behavior in operator >>
Structuring C++ Application (directory and folders)
Sendkey Function Enter Key Issue
Why the memory do not release after funciton call?
Passing C++ functions as arguments without function pointer/inlined?
How to create multiple objects inside of for loop with different names?
© Copyright 2017 Publishing Limited. All rights reserved.