-1

I am very new to coding and was presented with this java program that converts a hex number to decimal the line of code I'm having a really hard time grasping is this

val = 16*val + d;

I hope someone can explain to me where does the value for val comes from on the right side of the equation, thank you in advance

/*
 * Click nbfs://nbhost/SystemFileSystem/Templates/Licenses/license-default.txt to change this license
 * Click nbfs://nbhost/SystemFileSystem/Templates/Classes/Main.java to edit this template
 */
package javaapplication;

/**
 *
 * @author Rado
 */
public class JavaApplication {

    /**
     * @param args the command line arguments
     */
    public static void main(String[] args)

{

    // TODO code application logic here

    String digits = "0123456789ABCDEF";  

    String myhex="ABC";

       int val = 0;  

       for (int i = 0; i < myhex.length(); i++)  

       {  

         char c = myhex.charAt(i);  

         int d = digits.indexOf(c);  
         
        

         val = 16*val + d;  

       }  

  System.out.println(val);

 }
}
Aitsuken
  • 126
  • 10
Rado
  • 1
  • 2
  • Already a good explanation here [Java Hexadecimal to Decimal conversion: Custom Logic](https://stackoverflow.com/a/52676496/1248974) – chickity china chinese chicken Oct 22 '22 at 05:42
  • Not sure what you are asking, but the "where does the value for val comes from on the right side of the equation" so `val` on the right comes from the previous iteration of the loop. The first iteration val is 0. Next iteration it's the value of `d`. After that it's the value of `d` times 16, after that it's d1*16*16 + d2 * 16, etc. You can try printing out the value of `val` each loop, and you should also try doing some of the calculations by hand so you understand them better. – markspace Oct 22 '22 at 06:15
  • yup doing the actual math really helped thank you – Rado Oct 22 '22 at 06:57

2 Answers2

0

It seems like you didn't understand the logic behind this.

But let's assume you did and let's try break up the code:

The first thing is the String that you are working with. We have "myhex" String which is the hexadecimal representation of the value that we want to convert into decimal format.

String myhex="ABC";

And we have another String "digits". We use that String for our calculation with help of .indexOf(char character) method. It is a build-in method for Strings and used to find index of characters we are looking for. It returns the index of character we specified in brackets.(For example in "digits" String we have 'A' character which is tenth in that String but if 'A' was in the begining, it would return int 0.

 String digits = "0123456789ABCDEF"; 

Don't forget that we have already declared an int "val" before the loop. So we won't loose the value in the loop section.

int val = 0;  

Then we have a for loop and we set "myhex"'s length as boundary of our loop. We go through characters of "myhex" by declaring a new character with .charAt(int index, which is 0 at the start of the loop and at the first execution we will have 'A' as our first "c" char).

char c = myhex.charAt(i);

After that we look which value it represents by assigning d

int d = digits.indexOf(c);  

The rest is just finding decimal value(base ten) -> logic

If you have problems with understanding something. The first thing to do is figure out whether you have trouble with the logic or the code.

Digit by digit

val = 16*val + d;

And printing the decimal value after the loop

System.out.println(val);
Aitsuken
  • 126
  • 10
0

Just to answer your question about the variable 'val' on the right side of the equation.

val = (16 * val) * d;

'val' is being redeclared to a new value by: val = (previous 'val' times 16) plus the value of 'd'.

This is done every iteration, three times in this case for length 3 of 'myhex', of each character from my hex and checking if it exist in the digit string.

Here's a modification of your code with comments that can help you understand the execution of the code through each iteration.

public class JavaApplication {
    public static void main(String[] args)  {
        String digits = "0123456789ABCDEF";
        String myhex="ABC";
        int val = 0;
        
        /* Iterate the length of the string myhex, in this case, 3 iterations */
        for (int i = 0; i < myhex.length(); i++)    {
            /* c is equals to the character at index 'i' of myhex */
            char c = myhex.charAt(i);
            
            /* let d equal the index of the first occurence of 'c' in digits */
            int d = digits.indexOf(c);
         
            /* reassign val to (val * 16) + d */
            val = 16 * val + d;
            System.out.println("Value of 'val' at iteration " + i + ":\t" + val); // print out val for every iteration of 'i'
       }
        
       System.out.println("final value of 'val': " + val); // final value of val after all iterations
    }
}
Dennis LLopis
  • 315
  • 2
  • 6