జావాలో బైనరీ నుండి దశాంశ మార్పిడి
0 మరియు 1 అనే 2 అంకెలు మాత్రమే ఉన్న సంఖ్యలను బైనరీ సంఖ్యలు అంటారు. అవి బేస్ 2 నంబర్ సిస్టమ్లో మాత్రమే వ్యక్తీకరించబడతాయి. బైనరీ సిస్టమ్ ఆధునిక కంప్యూటర్ల స్థానిక డిజిటల్ భాష అని మనకు తెలుసు. కానీ మనం దశాంశ సంఖ్యలకు ఎక్కువగా అలవాటు పడ్డాం. వాస్తవానికి, మీరు బైనరీ సంఖ్యలను దశాంశంగా మరియు వైస్ వెర్సాగా మార్చవచ్చు. దిగువ రేఖాచిత్రంలో మీరు బైనరీ నుండి దశాంశ మార్పిడికి ఉదాహరణను చూడవచ్చు. 10101 అనేది గణిత గణనలను వివరించడానికి ఉపయోగించే బైనరీ సంఖ్య.
- జావా పద్ధతిని ఉపయోగించడం
- అనుకూల తర్కాన్ని ఉపయోగించడం
జావా ప్రీబిల్ట్ మెథడ్
స్ట్రింగ్ను పూర్ణాంకానికి మార్చడానికి జావా మనకు Integer.parseInt() పద్ధతిని అందిస్తుంది . parseInt() పద్ధతి పూర్ణాంక తరగతికి చెందినది .వాక్యనిర్మాణం
public static int parseInt(String binaryNumber, int radix)
ఉదాహరణ
public class ConvertingBinaryToDecimal {
public static void main(String args[]){
String binaryNumber="10101";
int decimalNumber=Integer.parseInt(binaryNumber,2);
System.out.println(decimalNumber);
}
}
అవుట్పుట్
21
కస్టమ్ లాజిక్ ఉపయోగించి
పూర్ణాంకాన్ని స్వీకరించి, దానిని దశాంశ సంఖ్యకు మార్చే ప్రోగ్రామ్ను కూడా మనం జావాలో వ్రాయవచ్చు. దీన్ని అర్థం చేసుకోవడానికి ఒక ఉదాహరణ చూద్దాం.ఉదాహరణ
public class ConvertingBinaryToDecimal {
// function for converting binary to decimal number
public static int getDecimalNumber(int binaryNumber){
int decimalNumber = 0;
int power = 0;
while(binaryNumber > 0){
//taking the rightmost digit from binaryNumber
int temp = binaryNumber%10;
//now multiplying the digit and adding it to decimalNumber variable
decimalNumber += temp*Math.pow(2, power);
//removing the rightmost digit from binaryNumber variable
binaryNumber = binaryNumber/10;
//incrementing the power variable by 1 to be used as power for 2
power++;
}
return decimalNumber;
}
public static void main(String args[]){
System.out.println("Decimal value is: "+getDecimalNumber(111010));
System.out.println("Decimal value is: "+getDecimalNumber(001010));
}
}
అవుట్పుట్
దశాంశ విలువ: 58 దశాంశ విలువ: 24
GO TO FULL VERSION