-2

Background

I'm doing a college assignment, which requires me to draw a pattern represented by 4 Hexadecimal digits. So far, I've managed to create everything I need to do it, except a little tiny thing: I have a bug in my code, which renders Hexadecimal values between A - F (or Decimal 10-17) useless.

I created a method where I obtain a decimal value from an hexadecimal , and convert it to a binary value.

Here's a list of the results I've obtained:

 Decimal 1  -> Binary 1    -> CORRECT
 Decimal 2  -> Binary 1    -> Should be 10
 Decimal 3  -> Binary 11   -> CORRECT
 Decimal 4  -> Binary 1    -> Should be 100
 Decimal 5  -> Binary 101  -> CORRECT
 Decimal 6  -> Binary 11   -> Should be 110
 Decimal 7  -> Binary 111  -> CORRECT
 Decimal 8  -> Binary 1    -> Should be 1000
 Decimal 9  -> Binary 1001 -> CORRECT
 Decimal 10 -> Binary 101  -> Should be 1010
 Decimal 11 -> Binary 1101 -> Should be 1011
 Decimal 12 -> Binary 11   -> Should be 1100
 Decimal 13 -> Binary 1011 -> Should be 1101
 Decimal 14 -> Binary 111  -> Should be 1110
 Decimal 15 -> Binary 1111 -> CORRECT

The output is actually the reverse of what it should be. How can reverse the output, or make my code output the correct order in the first place?

Edit #2

The problem here, as refered to on the original post is that:

  for (int i = 0; i<=3; i++){ //loops trough my pattern array 
                            // Will receive the binary values
    while (nDecimal[i]!=0){
    pattern[i]= pattern[i]*10 + nDecimal[i]%2;
    nDecimal[i]/=2;
    System.out.println("Binary ["+i+"] " + pattern[i]);
}

is not working correctly, and is outputting the binary digits on the inverse order. In short, here's the code that does the conversion:

    int myDecimal;
    int result;
   while (myDecimal!=0){
    result= result*10 + myDecimal%2;
    myDecimal=2;

}
Oak
  • 693
  • 7
  • 13
  • Post the relevant code where you do the conversion. AFAIK you have to: 1) maintain the leading zeros on the left (or right) and 2) reverse the result. – Luiggi Mendoza May 11 '14 at 00:54
  • 1
    There is no decimal data type in Java. How can you index a "decimal" array and perform arithmetic on it??? (Maybe you should reveal how your variables are declared and initialized.) – Hot Licks May 11 '14 at 00:57
  • The code which converts Decimal to Binary is the one posted (The Hexadecimal to is quite precise, and yes, I've tested it). As per the rest of your comment, I agree with you, my issue is how should I do it. I remember a bit from digital systems logic that you can subtract something from a binary number to get it's reverse, but I honest to god cannot remember what it was exactly (I am very tired) – Oak May 11 '14 at 00:58
  • @Hot Licks gimme a second – Oak May 11 '14 at 00:59
  • Decimal? Like a double or float? And what's `Decimal 1` in java terms? like `1.0`? Please be more specific – Vince May 11 '14 at 01:10
  • Added a few details, and @VinceEmigh , I'm currently using Integers, which would make the Decimal number "1" be "1" in java. – Oak May 11 '14 at 01:12
  • I don't see any declarations. – Hot Licks May 11 '14 at 01:15
  • If you're in college, you need to learn how to write a coherent description of what it is you're doing. Come back when you've figured that out. – Hot Licks May 11 '14 at 01:17
  • "I'm currently using Integers. Decimal number '1' be '1' in java" - "Converting Decimal to Integer" Yeah.. I'm out. Good luck. EDIT: Decimal in java is either a `double` or a `float`, not an integer. Integers are whole numbers. – Vince May 11 '14 at 01:18
  • @VinceEmigh - "Decimal" means a base-10 numbering system. Neither integer nor float/double is base-10 in Java. – Hot Licks May 11 '14 at 01:22
  • You declare myDecimal as int and then index it? Surely the compiler does not accept that! – Hot Licks May 11 '14 at 01:24
  • @HotLicks http://wiki.answers.com/Q/The_difference_between_a_decimal_and_a_whole_number – Vince May 11 '14 at 01:24
  • @VinceEmigh - I wouldn't trust wikianswers as far as I could throw it. All you're saying is that *some* people believe that "decimal" means what any mathematician (or competent programmer) would call a "fraction". – Hot Licks May 11 '14 at 01:27
  • Regarding the declaration of myDecimal as an integer, and it being indexed was because I was editing the code so it would become simpler (Without dealing with an array, which freaked out some people). Evidently there is also an english issue here, as a Decimal number can refer to a number such as "0.2", but if I'm referring to bases, it can also mean the Decimal Base (Base-10), which is being converted to the Binary Base (Base-2) – Oak May 11 '14 at 01:28
  • @HotLicks This is like arguing about which is right; Advisor or Adviser. Yes, decimal is base 10. Yes, decimal and whole number can be seen as two different things depending on context. The point is, in this case, it could easily confuse people, and it should be referenced differently. No fowl play by the OP, his english is correct, its just not the best way to word things in this situation. Sorry for the misunderstanding, this is why I said "A decimal 'in Java'". – Vince May 11 '14 at 01:37
  • I clarified it in the title – Oak May 11 '14 at 01:42
  • 2
    @VinceEmigh "Decimal" is the correct word for "base 10" and the OP has used the word correctly and unambiguously. Your comments here ("Decimal ... is not an integer") are misleading and confusing. – Dawood ibn Kareem May 11 '14 at 01:47
  • I wouldn't quite go so far as to say that the OP has used "decimal" correctly. He appears to regard an `int` as a "decimal" value. – Hot Licks May 11 '14 at 01:52
  • 1
    How is "I created a method where I obtain a decimal value from an hexadecimal , and convert it to a binary value" incorrect? It contrasts the three systems. It means exactly the same as, and is equal in clarity to "I created a method where I obtain a base-10 value from a base-16 value, and convert it to a base-2 value". – Dawood ibn Kareem May 11 '14 at 02:00
  • Except nowhere that I see does he deal in base-10 values. (Unless you count the screwy way he's creating "binary".) – Hot Licks May 11 '14 at 02:09
  • nDecimal[i] is an array made out of four Base-10 numbers. And you are right, that's why I need help, to fix that screw up – Oak May 11 '14 at 02:12
  • @DavidWallace You are so wrong dude.. In this situation, java programming, decimal values have their own representations compared to whole numbers (short, byte, int, long == no decimal; float, double == decimal). Saying a number is a decimal in Java is assuming it has a decimal point somewhere. If it doesn't, don't consider it a decimal. Who cares what the dictionary considers "decimal" to be, **its confusing to refer to a number without a decimal point as a decimal in Java**. Dont encourage confusion just to sound smart.. – Vince May 11 '14 at 06:33
  • @VinceEmigh - It is obvious that when OP said "decimal" he meant "base 10". That's what "decimal" means. And contrary to your last comment, none of the six primitive types that you mentioned (including float and double) are stored in decimal - they're all stored in binary. Yes, there's also another meaning of "decimal", but it's obvious that that's not the meaning that OP was using. You attacked him/her for using the word correctly, and thereby displayed your own ignorance. Please stop now. – Dawood ibn Kareem May 11 '14 at 08:38
  • @DavidWallace You obviously just got here. The OP edited his post with more context, leading you to know he means base 10. Those weren't there before. I didnt attack, I simply noted that I was leaving due to lack of context and no way of fully understanding what he was trying to do, even after he attempted to explain. You came when the details were already here. Until you realize you the timeline, back off. I did nothing but point out the confusion I was having; all you did was ironicly call me ignorant. Grow up man.. In the future, make sure you understand whats going on before attacking.. – Vince May 11 '14 at 09:21
  • Actually, @VinceEmigh the first version I read was the original, which frankly I found perfectly clear. And although I'm sure your early comments were well-motivated, the tone of them made them seem to me like an attack. Moreover, your comment "Decimal in java is either a double or a float, not an integer" is simply untrue, and warranted correction. – Dawood ibn Kareem May 11 '14 at 09:37

2 Answers2

2

Oak, I'm gonna make this an answer, even though it's not really answering what you're asking, and hence I'm breaking the rules. But you need to understand it.

There is a big difference between the concepts of a numerical value and the possible representations of that value.

int, long, float, double and others represent numerical values. Only incidentally is the internal (to the computer) representation of those values binary -- first and foremost they represent "pure" numbers.

"Decimal", "binary", "hexadecimal", "octal" and a few others are representations of numbers. The exact same numeric quantity can be represented as any one of the above. Representations are only significant when a value must be "presented", either to a human or to some program or device that requires a non-native representation.

To create one of these representations the numeric value is converted into a character sequence -- 0...9 for decimal, 0/1 for binary, 0...F for hex, 0...7 for octal. (And, Vince, independent of the specific radix, one can insert a "radix point" between two digits to create a representation of a fractional value.)

Oak, converting the numeric values contained in an int into "binary" representation using the mechanism you're using is clever, though error-prone and of generally limited utility. You are making use of the fact that a numeric quantity is generally formatted as decimal so that if you process a number through a sort of polynomial it will display using only the decimal digits 0 and 1 and will therefore present a binary representation. There are far less convoluted way of generating a binary representation, however.

Hot Licks
  • 47,103
  • 17
  • 93
  • 151
  • I understand what you're saying, the issue is that the integer values that you consider as "whole" numbers are, in this case, representations on different bases, initially Hexadecimal, which is then converted to Decimal and Binary. The issue is with the Decimal -> Binary conversion. NOT with transforming decimal values within a certain number to a binary representation, but with transforming a whole number represented in Base-10 to one represented in base-2. I really appreciate the effort though, thank you – Oak May 11 '14 at 11:14
  • @Oak - You need to explain yourself better. You have not explained what one of your "representations" looks like. – Hot Licks May 11 '14 at 13:11
1

I managed to figure it out this morning, here's what I did:

                int tester;
                for (int i = 0; i<=3; i++){
                int multiplier = 1;
                    while (nDecimal[i]>0)
                    {
                        int remainder = nDecimal[i]%2;
                        nDecimal[i] = nDecimal[i]/2;
                        binary[i] = binary[i] +remainder*multiplier;
                        multiplier = multiplier * 10;
                    }}

What this code does is looping trough the nDecimal[] array values to transform itself into binary[]. Each "slot" in the nDecimal array is in the Decimal Base (Base-10), and the conversion is done to the Binary Base (Base-2) via the sucessive division method (Example : http://www.wikihow.com/Convert-from-Decimal-to-Binary)

I've posted the awnser to my problem as it might be useful to someone else in the future

Oak
  • 693
  • 7
  • 13
  • If all you want to do is *display* a number in its binary form, why not use [`Integer.toBinaryString(int)`](http://docs.oracle.com/javase/7/docs/api/java/lang/Integer.html#toBinaryString%28int%29)? – JonK May 11 '14 at 11:40
  • The goal is to be able to work around such expressions – Oak May 11 '14 at 11:41
  • Which is my primary gripe with academic exercises. The wheel has already been invented, re-inventing it is pointless (let's face it, outside of this assignment I'm 99.999% certain that you'll *never* do this again). – JonK May 11 '14 at 11:46
  • True, but the goal is to be able to create it should you need to ;) – Oak May 11 '14 at 14:40
  • That was part of my point - you won't need to. These features are built into the *standard* Java API, you're *always* going to have access to them. **That said**, I'm in danger of starting off a comment-discussion on this, so I'm going to stop. – JonK May 11 '14 at 18:58
  • Oak, you have completely misunderstood what's going on. I'm not sure from your code whether each `nDecimal[i]` is an `int` or a `long`, but either way, it is NOT represented in base 10, at least until you print it out. You should really try to understand Hot Licks' answer better. – Dawood ibn Kareem May 11 '14 at 19:02