getMessage()); return; } long KB = 1024 * 8; //no. of bits in 1KB long MB = 1024
ID: 3906262 • Letter: G
Question
getMessage());
return;
}
long KB = 1024 * 8; //no. of bits in 1KB
long MB = 1024 * KB; //no. of bits in 1MB
long GB = 1024 * MB; //no of bits in 1GB
while(inFile.hasNext())
{
String hex = inFile.next();
String bin = hexToBin(hex);
long deci = binToDeci(bin);
long chip = deci / GB;
System.out.printf("Ram Chip %d, Hex: %s, Decimal: %,d ", chip,
hex, deci );
//System.out.print(", Binary " + bin + " ");
}
inFile.close();
}
}
Explanation / Answer
import java.io.BufferedReader;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.util.Scanner;
public class MemoryCalculator {
//retursn a 4bit binary form for given hex char
private static String hexToBin(char hex)
{
int deci = 0;
StringBuilder bin = new StringBuilder("0000");
//get decimal value of the hex char
if(hex >= '0' && hex <= '9')
deci = hex - '0';
else if(hex >= 'A' && hex <= 'F')
deci = 10 + hex - 'A';
//repeatedly divide by 2 and set the remainder starting from last index
int index = 3;
while(deci != 0)
{
int rem = deci % 2;
bin.replace(index, index + 1, rem+"");
deci /= 2;
index--;
}
//System.out.println(hex + "= " + bin.toString());
return bin.toString();
}
//return binary form of the given hex value
private static String hexToBin(String hex)
{
String bin = "";
for(int i = 0; i < hex.length(); i++)
bin += hexToBin(hex.charAt(i));
return bin;
}
//convert a binary value to decimal
private static long binToDeci(String bin)
{
long decimal = 0;
long powerOf2 = 1; //starting power of 2 for last bit (LSB)
for(int i = bin.length() - 1; i >= 0; i--)
{
char ch = bin.charAt(i);
if(ch == '1')
decimal += powerOf2;//add the power of 2 only if the char is 1
powerOf2 *= 2; //next power
}
return decimal;
}
public static void main(String[] args) {
/*you can directly mention file path instead of inputting file name as content in file will be changed but not file name, here \ is used reason is is an escape character hence \ will add an one .
* if you need to input file name and process. Take user input for file name and then use InputStream function in try block instead of BufferReader to get classpath and add file name to it
*/
final String FILENAME = "C:\Users\VISHNU\Desktop\Java_Wokspace\Hello\src\RAMerrors";
long KB = 1024 * 8; //no. of bits in 1KB
long MB = 1024 * KB; //no. of bits in 1MB
long GB = 1024 * MB; //no of bits in 1GB
try (BufferedReader br = new BufferedReader(new FileReader(FILENAME))) {
String sCurrentLine;
while ((sCurrentLine = br.readLine()) != null) {
String hex = sCurrentLine.toUpperCase(); //will optimize code as memory address will be "0-F" or "0-f" but not in combination
String bin = hexToBin(hex);
long deci = binToDeci(bin);
long chip = deci / GB;
System.out.println("Ram Chip : "+chip+", Hex : "+hex+", Decimal : "+deci );
}
} catch (IOException e) {
e.printStackTrace();
}
}
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.