Converting Hex to Binary to Decimal using While Loop

  • (2 Pages)
  • +
  • 1
  • 2

15 Replies - 2233 Views - Last Post: 24 March 2015 - 07:50 AM Rate Topic: -----

#1 JavaRob   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 8
  • Joined: 21-March 15

Converting Hex to Binary to Decimal using While Loop

Posted 21 March 2015 - 09:32 PM

Hi, I'm fairly new to coding and having a bit of trouble.

I am writing a program to

1. read a text file that contains 8 lines of hexadecimals
2. convert the lines to binary, and then decimal
3. Output to attached image


So far this is what I have:

 
/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package ramerros;

import java.io.File;
import java.io.FileNotFoundException;
import java.math.BigDecimal;
import java.math.BigInteger;
import java.util.ArrayList;
import java.util.Scanner;
import static javax.swing.text.html.HTML.Tag.I;

/**
 *
 * @author Jose
 */
public class Ramerros {
    public static void main (String[] args) throws FileNotFoundException
    {
        
        
        
         
        Scanner textDataFile = new Scanner(new File("/Desktop/RAMerrors2.txt"));

        while(textDataFile.hasNextLine()){
            
            
            

            ArrayList<String> textData = new ArrayList<String>();
            final String storedValue = textDataFile.nextLine();
            String line = storedValue;
           
            
            
            Scanner scanner = new Scanner(line);
            scanner.useDelimiter(",");
            while(scanner.hasNext()){
                textData.add(scanner.next());
                
                
                    
                }
            
            
                BigInteger bigint = new BigInteger(storedValue, 16);
                   final String bin = bigint.toString(2);
                    
                   
                       
                   
                        BigInteger bigInt = new BigInteger(bin,2);

                         BigDecimal bigDec = new BigDecimal(bigInt);
                            
                         
                   
                        System.out.println(storedValue);
                        System.out.println(bin);
                        System.out.println(bigDec);
                       
                       
                           
                       
                   
                   
                   
                  

    
                

         
                 
     
               
                
                
                
                
}
          



Currently this is my output:

 ABCDEFABC
101010111100110111101111101010111100
46118402748
1FFFFFFFF
111111111111111111111111111111111
8589934591
1A00D0000
110100000000011010000000000000000
6980173824
13A0A1EFF
100111010000010100001111011111111
5268709119
7A0EDF301
11110100000111011011111001100000001
32764719873
1A2329AFF
110100010001100101001101011111111
7016192767
3CDAEFFAD
1111001101101011101111111110101101
16335699885
16697D0FF
101100110100101111101000011111111
6016192767




I am trying to include 1 function that has all conversions and I'm able to return 1 value, for example is I set the return variable to storedValue, and I just have to simply run System.out.println(storedValue) to achieve the same output as my previous code.

I then need to write the variable into an array to properly display as my output.


Any help pushing me in the right direction will help...


Thanks guys, and this is my first post, glad to be here!!

Is This A Good Question/Topic? 0
  • +

Replies To: Converting Hex to Binary to Decimal using While Loop

#2 JavaRob   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 8
  • Joined: 21-March 15

Re: Converting Hex to Binary to Decimal using While Loop

Posted 21 March 2015 - 09:37 PM

Sorry, here is how my output should look (attached)

Attached image(s)

  • Attached Image

Was This Post Helpful? 0
  • +
  • -

#3 macosxnerd101   User is offline

  • Games, Graphs, and Auctions
  • member icon




Reputation: 12800
  • View blog
  • Posts: 45,992
  • Joined: 27-December 08

Re: Converting Hex to Binary to Decimal using While Loop

Posted 21 March 2015 - 10:38 PM

I would set it up as two methods:
public static int hexToDecimal(String hexString){

}

public static int decimalToBinary(int decimalValue){

}



NeoTifa's tutorial may be helpful for learning how to convert amongst number bases.
Was This Post Helpful? 0
  • +
  • -

#4 g00se   User is offline

  • D.I.C Lover
  • member icon

Reputation: 3744
  • View blog
  • Posts: 17,121
  • Joined: 20-September 08

Re: Converting Hex to Binary to Decimal using While Loop

Posted 22 March 2015 - 04:31 AM

You needn't create more than one instance of BigInteger, but actually that class won't really help. Like other classes in the API (imo) binary/hex representation is broken. e.g.

new BigInteger("-1).toString(2) will give you "-1" instead of a string of thirty-two ones

I've always used my own routines for visualization of bit patterns and non-decimal bases
Was This Post Helpful? 0
  • +
  • -

#5 JavaRob   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 8
  • Joined: 21-March 15

Re: Converting Hex to Binary to Decimal using While Loop

Posted 22 March 2015 - 07:18 AM

View Postmacosxnerd101, on 21 March 2015 - 10:38 PM, said:

I would set it up as two methods:
public static int hexToDecimal(String hexString){

}

public static int decimalToBinary(int decimalValue){

}



NeoTifa's tutorial may be helpful for learning how to convert amongst number bases.


Thank you


View Postg00se, on 22 March 2015 - 04:31 AM, said:

You needn't create more than one instance of BigInteger, but actually that class won't really help. Like other classes in the API (imo) binary/hex representation is broken. e.g.

new BigInteger("-1).toString(2) will give you "-1" instead of a string of thirty-two ones

I've always used my own routines for visualization of bit patterns and non-decimal bases



Is it possible to perform calculations of different data types in one method to output a single variable?

For example I'm more familiar with PHP coding, and I wrote this up and wanted to somehow write the same in java.

<?php


//converts hex value into a binary and then into a decimal and outputs decimal value//
function convertHex($value) {
$hex2Bin = base_convert($value,16,2);
$bin2Decimal = base_convert($hex2Bin,2,10);
return $bin2Decimal;    
}


//locates where the error is located and outputs the corresponding message//
function locateError($value){
if ($value >= 0 && $value <= 8589934592) {
    $errorMessage = 'Ram chip 0';
    return $errorMessage;
}

elseif ($value >= 8589934593 && $value <= 17179869184){
    $errorMessage = 'Ram chip 1';
    return $errorMessage;
}

elseif ($value >= 17179869185  && $value <= 25769803776){
    $errorMessage = 'Ram chip 2';
    return $errorMessage;
}

elseif ($value >= 25769803777  && $value <= 34359738368){
    $errorMessage = 'Ram chip 3';
    return $errorMessage;
}

elseif ($value >= 34359738369  && $value <= 42949672960){
    $errorMessage = 'Ram chip 4';
    return $errorMessage;
}

elseif ($value >= 42949672961  && $value <= 51539607552){
    $errorMessage = 'Ram chip 5';
    return $errorMessage;
}

elseif ($value >= 51539607553  && $value <= 60129542144){
    $errorMessage = 'Ram chip 6';
    return $errorMessage;
}

elseif ($value >= 60129542145  && $value <= 68719476736){
    $errorMessage = 'Ram chip 7';
    return $errorMessage;
}

else {
    $errorMessage = 'something went wrong';
    return $errorMessage;
}
}


//loads ramLocationerror into an array, we have 8 entires because the computer has 8 gigs of ram and each gig is in one ram chip//
$ramLocationerror = array('ABCDEFABC', '1FFFFFFFF', '1A00D0000', '13A0A1EFF','7A0EDF301','1A2329AFF','3CDAEFFAD','16697D0FF');

echo '<table style=/"width:100%/" border = /"1/">';

//foreach loop that prints out the values in the array, then prints out the decimal value, and the corresponding error location//
foreach ($ramLocationerror as $key => $value){
$convert = convertHex($value);
echo '<tr>';
echo '<td><b>Hex Value</b> ' . ' ' . $value . ' ' . '<b>Decimal Value:</b>' . ' ' . $convert . ' ' . '<b>Error location:</b>' . locateError($convert) . '</td>';
echo '</tr>';
}
echo '</table>';

?>




I needed something similar to that but for Java
Was This Post Helpful? 0
  • +
  • -

#6 macosxnerd101   User is offline

  • Games, Graphs, and Auctions
  • member icon




Reputation: 12800
  • View blog
  • Posts: 45,992
  • Joined: 27-December 08

Re: Converting Hex to Binary to Decimal using While Loop

Posted 22 March 2015 - 04:11 PM

That's actually not even good PHP code. There is a lot of redundancy. I would avoid trying to translate that to Java. The approach I suggested is much more modular and reusable.

If you aren't looking to implement the conversions yourself, you can use built-in library functionality to do such. Convert the hex string to decimal using the Integer.parseInt(String input, int radix) method. Then use the Integer.toBinaryString() method to convert the decimal representation to a binary string.
Was This Post Helpful? 0
  • +
  • -

#7 JavaRob   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 8
  • Joined: 21-March 15

Re: Converting Hex to Binary to Decimal using While Loop

Posted 22 March 2015 - 08:44 PM

I'm trying to write an if then statement using a Big Decimal and I'm getting this error:


integer number too large: 17179869184

bad operand types for binary operator '>='
first type: BigDecimal
second type: int


Here is the code:

BigInteger bigInt = new BigInteger(bin,2);

                         BigDecimal bigDec = new BigDecimal(bigInt);
                            
                         
                   
                        System.out.println(storedValue);
                        System.out.println(bin);
                        System.out.println(bigDec);
                       
                       
                           
                       if (bigDec >= 0 && bigDec <= 17179869184) 



how do you use if then statements for big decimal?

Sorry, here the code properly:

BigInteger bigInt = new BigInteger(bin,2);

                         BigDecimal bigDec = new BigDecimal(bigInt);
                            
                         
                   
                        System.out.println(storedValue);
                        System.out.println(bin);
                        System.out.println(bigDec);
                       
                       
                           
                       if (bigDec >= 0 && bigDec <= 17179869184) 


Was This Post Helpful? 0
  • +
  • -

#8 macosxnerd101   User is offline

  • Games, Graphs, and Auctions
  • member icon




Reputation: 12800
  • View blog
  • Posts: 45,992
  • Joined: 27-December 08

Re: Converting Hex to Binary to Decimal using While Loop

Posted 22 March 2015 - 08:46 PM

Related threads merged. Please stop duplicate posting.

The >= operator isn't defined on objects. See the BigInteger compareTo() method.
Was This Post Helpful? 0
  • +
  • -

#9 g00se   User is offline

  • D.I.C Lover
  • member icon

Reputation: 3744
  • View blog
  • Posts: 17,121
  • Joined: 20-September 08

Re: Converting Hex to Binary to Decimal using While Loop

Posted 23 March 2015 - 05:36 AM

Plus, use the approved String constructor for BigInteger to avoid problems
Was This Post Helpful? 0
  • +
  • -

#10 JavaRob   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 8
  • Joined: 21-March 15

Re: Converting Hex to Binary to Decimal using While Loop

Posted 23 March 2015 - 12:52 PM

I wrote multiple if then statements, when I run it through an array, it only prints 1 ram chip versus the corresponding chip. Does anyone know the problem?

tring s1;
                            if (j >= 0 && j <= 8589934592l){
                                        s1 = "0";
                                        
                                      
                              }
                            else if (j >= 8589934593l && j <= 17179869184l);
                            {
                                        s1 = "1";
                              }
                            if (j >= 17179869185l && j <= 25769803776l){
                                        s1 = "2";
                              }
                            else if (j >= 25769803777l && j <= 34359738368l);
                            {
                                        s1 = "3";
                                if (j >= 34359738369l && j <= 42949672960l){
                                        s1 = "4";
                              }
                            else if (j >= 42949672961l && j <= 51539607552l);
                            {
                                        s1 = "5";
                                if (j >= 51539607553l && j <= 60129542144l){
                                        s1 = "6";
                              }
                            else if (j >= 60129542145l && j <= 68719476736l);
                            {
                                        s1 = "7";
                                
                                
                                ArrayList<Object> listOfObjects = new ArrayList<Object>();
                                listOfObjects.add(storedValue);
                                listOfObjects.add(bin);
                                listOfObjects.add(bigDec);
                                listOfObjects.add("Chip#" +s1);
                                
                                
                                System.out.println(listOfObjects);




here is my current output, as you can see they all say ram chip#7 which is the last series in my if elseif statements.

[ABCDEFABC, 101010111100110111101111101010111100, 46118402748, Chip#7]
[1FFFFFFFF, 111111111111111111111111111111111, 8589934591, Chip#7]
[1A00D0000, 110100000000011010000000000000000, 6980173824, Chip#7]
[13A0A1EFF, 100111010000010100001111011111111, 5268709119, Chip#7]
[7A0EDF301, 11110100000111011011111001100000001, 32764719873, Chip#7]
[1A2329AFF, 110100010001100101001101011111111, 7016192767, Chip#7]
[3CDAEFFAD, 1111001101101011101111111110101101, 16335699885, Chip#7]
[16697D0FF, 101100110100101111101000011111111, 6016192767, Chip#7]


Was This Post Helpful? 0
  • +
  • -

#11 macosxnerd101   User is offline

  • Games, Graphs, and Auctions
  • member icon




Reputation: 12800
  • View blog
  • Posts: 45,992
  • Joined: 27-December 08

Re: Converting Hex to Binary to Decimal using While Loop

Posted 23 March 2015 - 12:54 PM

STOP duplicate posting. Consider this a formal warning. You have an open thread.

Closing.

Edit: I have merged your duplicate threads. In the future, please do not open a new thread for each continuation. Try to keep the guidelines in mind of one thread per project.
Was This Post Helpful? 0
  • +
  • -

#12 mike73   User is offline

  • D.I.C Addict
  • member icon

Reputation: 250
  • View blog
  • Posts: 918
  • Joined: 24-April 10

Re: Converting Hex to Binary to Decimal using While Loop

Posted 23 March 2015 - 01:28 PM

You are not chaining if else statements. You have a few if else statements, then a new if else chain, as well as some if statements within if statements.

comments in code:
02	                            if (j >= 0 && j <= 8589934592l){
03	                                        s1 = "0";
04	                                         
05	                                       
06	                              }
07	                            else if (j >= 8589934593l && j <= 17179869184l);
08	                            {
09	                                        s1 = "1";
10	                              }
11	                            if (j >= 17179869185l && j <= 25769803776l){ // new if
12	                                        s1 = "2";
13	                              }
14	                            else if (j >= 25769803777l && j <= 34359738368l);
15	                            {
16	                                        s1 = "3";
                                        // inner if
17	                                if (j >= 34359738369l && j <= 42949672960l){
18	                                        s1 = "4";
19	                              }
20	                            else if (j >= 42949672961l && j <= 51539607552l);
21	                            {
22	                                        s1 = "5";
                                        // inner if
23	                                if (j >= 51539607553l && j <= 60129542144l){
24	                                        s1 = "6";
25	                              }
26	                            else if (j >= 60129542145l && j <= 68719476736l);
27	                            {
28	                                        s1 = "7";
29	
                                    // no closing brace

This post has been edited by mike73: 23 March 2015 - 01:29 PM

Was This Post Helpful? 0
  • +
  • -

#13 JavaRob   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 8
  • Joined: 21-March 15

Re: Converting Hex to Binary to Decimal using While Loop

Posted 23 March 2015 - 04:17 PM

View Postmike73, on 23 March 2015 - 01:28 PM, said:

You are not chaining if else statements. You have a few if else statements, then a new if else chain, as well as some if statements within if statements.

comments in code:
02	                            if (j >= 0 && j <= 8589934592l){
03	                                        s1 = "0";
04	                                         
05	                                       
06	                              }
07	                            else if (j >= 8589934593l && j <= 17179869184l);
08	                            {
09	                                        s1 = "1";
10	                              }
11	                            if (j >= 17179869185l && j <= 25769803776l){ // new if
12	                                        s1 = "2";
13	                              }
14	                            else if (j >= 25769803777l && j <= 34359738368l);
15	                            {
16	                                        s1 = "3";
                                        // inner if
17	                                if (j >= 34359738369l && j <= 42949672960l){
18	                                        s1 = "4";
19	                              }
20	                            else if (j >= 42949672961l && j <= 51539607552l);
21	                            {
22	                                        s1 = "5";
                                        // inner if
23	                                if (j >= 51539607553l && j <= 60129542144l){
24	                                        s1 = "6";
25	                              }
26	                            else if (j >= 60129542145l && j <= 68719476736l);
27	                            {
28	                                        s1 = "7";
29	
                                    // no closing brace



Thanks so much, I actually finished earlier!

/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package ramerros;

import java.io.File;
import java.io.FileNotFoundException;
import java.math.BigDecimal;
import java.math.BigInteger;
import java.util.ArrayList;
import java.util.Scanner;
import static javax.swing.text.html.HTML.Tag.I;

/**
 *
 * @author me
 */
public class Ramerros 
{
    public static void main (String[] args) throws FileNotFoundException
    {
        Scanner textDataFile = new Scanner(new File("/location"));
        
        while(textDataFile.hasNextLine())
        {
            ArrayList<String> textData = new ArrayList<>();
            final String storedValue = textDataFile.nextLine();
            String line = storedValue;
            Scanner scanner = new Scanner(line);
            scanner.useDelimiter(",");
            BigInteger bigint = new BigInteger(storedValue, 16);
            final String bin = bigint.toString(2);
            BigInteger bigInt = new BigInteger(bin,2);
            BigDecimal bigDec = new BigDecimal(bigInt);
            long j = bigDec.longValue();
            String s1 = null;
            
            while(scanner.hasNext())
            {
                textData.add(scanner.next());
            }
            
            if (j >= 0 && j <= 8589934592l)
            {
            s1 = "0";
            }
            if (j >= 8589934593l && j <= 17179869184l)
            {
            s1 = "1";
            }
            if (j >= 17179869185l && j <= 25769803776l)
            {
            s1 = "2";
            }
            if (j >= 25769803777l && j <= 34359738368l)
            {
            s1 = "3";
            }
            if (j >= 34359738369l && j <= 42949672960l)
            {
            s1 = "4";
            }
            if (j >= 42949672961l && j <= 51539607552l)
            {
            s1 = "5";
            }
            if (j >= 51539607553l && j <= 60129542144l)
            {
            s1 = "6";
            }
            if (j >= 60129542145l && j <= 68719476736l)
            {
            s1 = "7";
            }
            ArrayList<Object> listOfObjects = new ArrayList<>();
            listOfObjects.add(storedValue);
            listOfObjects.add(bin);
            listOfObjects.add(bigDec);
            listOfObjects.add("Chip#" +s1);
            System.out.println(listOfObjects);
        }
    }
}

   


thanks a gain for all help
Was This Post Helpful? 0
  • +
  • -

#14 mike73   User is offline

  • D.I.C Addict
  • member icon

Reputation: 250
  • View blog
  • Posts: 918
  • Joined: 24-April 10

Re: Converting Hex to Binary to Decimal using While Loop

Posted 23 March 2015 - 05:04 PM

I would suggest making it all if-else-if-etc. what's the point of checking 7 extra conditions if the first one is true meaning the rest won't be?
Was This Post Helpful? 0
  • +
  • -

#15 JavaRob   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 8
  • Joined: 21-March 15

Re: Converting Hex to Binary to Decimal using While Loop

Posted 24 March 2015 - 07:38 AM

View Postmike73, on 23 March 2015 - 05:04 PM, said:

I would suggest making it all if-else-if-etc. what's the point of checking 7 extra conditions if the first one is true meaning the rest won't be?



Alright, thanks!
Was This Post Helpful? 0
  • +
  • -

  • (2 Pages)
  • +
  • 1
  • 2