Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.java.programmer > #33534 > unrolled thread

hexadecimal to decimal conversion not working

Started bypuja219@gmail.com
First post2016-01-26 09:13 -0800
Last post2016-01-28 06:47 +0000
Articles 6 — 5 participants

Back to article view | Back to comp.lang.java.programmer


Contents

  hexadecimal to decimal conversion not working puja219@gmail.com - 2016-01-26 09:13 -0800
    Re: hexadecimal to decimal conversion not working Eric Sosman <esosman@comcast-dot-net.invalid> - 2016-01-26 13:10 -0500
    Re: hexadecimal to decimal conversion not working 4ndre4 <4ndre4@4ndre4.com.invalid> - 2016-01-26 20:04 +0000
    Re: hexadecimal to decimal conversion not working Jeff Higgins <jeff@invalid.invalid> - 2016-01-27 02:01 +0000
      Re: hexadecimal to decimal conversion not working Arne Vajhøj <arne@vajhoej.dk> - 2016-01-27 21:54 -0500
        Re: hexadecimal to decimal conversion not working Jeff Higgins <jeff@invalid.invalid> - 2016-01-28 06:47 +0000

#33534 — hexadecimal to decimal conversion not working

Frompuja219@gmail.com
Date2016-01-26 09:13 -0800
Subjecthexadecimal to decimal conversion not working
Message-ID<59b82ad2-c749-4c47-94d3-893b32ede458@googlegroups.com>
import java.io.*;
class Hexa_Deci
{ static String s="";
    static String str="";static String p="";
    static int num=0;static int pos=-1;static int copy=0;
  static int a[]={10,11,12,13,14,15};
 static String b[]={"A","B","C","D","E","F"};
 
    public static void main()throws IOException
    { InputStreamReader read=new InputStreamReader(System.in);
     BufferedReader br=new BufferedReader(read);
     System.out.println("Enter The Decimal Number");
     int N=Integer.parseInt(br.readLine());
     System.out.println("Hexadecimal Equivalent:"+" "+deciToHexa(N));
     System.out.println("Enter The Hexadecimal Number");
      str=br.readLine();
     int l=str.length();
     copy=l;
     System.out.println("Decimal Equivalent:"+" "+hexaToDeci(l));
    }
   static String deciToHexa(int n1)
    {if(n1==0)
     return "";
     else
    { int d=n1%16;
        if(d<10)
        s=deciToHexa(n1/16)+Integer.toString(d);
      else
      {for(int i=0;i<6;i++)
       { if(d==a[i])
       s=deciToHexa(n1/16)+b[i];
    }
}
    return s;
}
}
 static int hexaToDeci(int L1)
     {if(L1==0)
     return 0;
     else
    {pos++; 
        L1--;
        if(pos<copy-1)
      p=str.substring(pos,pos+1);
     else
      p=str.substring(pos);
        if((Character.isDigit(p.charAt(0)))==true)
        num=hexaToDeci(L1)+(int)(Math.pow(16,L1))*Integer.parseInt(p);
        else
        {for(int i=0;i<6;i++)
        if(p==b[i])
        {num=hexaToDeci(L1)+(int)(Math.pow(16,L1))*a[i]+hexaToDeci(L1);
          break;
        }
    
}  
}
return num;
}

}

[toc] | [next] | [standalone]


#33536

FromEric Sosman <esosman@comcast-dot-net.invalid>
Date2016-01-26 13:10 -0500
Message-ID<n88cl3$6kv$1@dont-email.me>
In reply to#33534
On 1/26/2016 12:13 PM, puja219@gmail.com wrote:
> import java.io.*;
> [...]

     There are many things that could be said about the code, but
first: Exactly how is the conversion "not working?"  What input
did you try, what output did you expect, and what output (or error
message) did you get instead?

     "Not working" is too vague; be more specific.

-- 
esosman@comcast-dot-net.invalid
"Don't be afraid of work. Make work afraid of you." -- TLM

[toc] | [prev] | [next] | [standalone]


#33538

From4ndre4 <4ndre4@4ndre4.com.invalid>
Date2016-01-26 20:04 +0000
Message-ID<n88jbt$98e$6@dont-email.me>
In reply to#33534
On 26/01/2016 17:13, puja219@gmail.com wrote:
[...]
Have you at least debugged your code?

-- 
4ndre4
"The use of COBOL cripples the mind; its teaching should, therefore, be 
regarded as a criminal offense." (E. Dijkstra)

[toc] | [prev] | [next] | [standalone]


#33540

FromJeff Higgins <jeff@invalid.invalid>
Date2016-01-27 02:01 +0000
Message-ID<n898d5$88o$1@dont-email.me>
In reply to#33534
On Tue, 26 Jan 2016 09:13:24 -0800, puja219 wrote:

In the guideforschool Class 10 post entitled
"Program on Hexadecimal to Decimal Number Conversion"
you can see the code you posted with better formatting
and comments. You could compare your code with theirs.
It's really too bad they don't provide a forum for students.

Or you could look in the Java Platform API Specification,
also called "the javadocs" for these methods which could make
your coding easier to learn and read.

java.lang.Integer.parseInt(String s, int radix)
java.lang.Integer.toString(int i, int radix)

Also read 
"How To Ask Questions The Smart Way" 
Eric Steven Raymond, and Rick Moen
on the web.

[toc] | [prev] | [next] | [standalone]


#33546

FromArne Vajhøj <arne@vajhoej.dk>
Date2016-01-27 21:54 -0500
Message-ID<n8bvsm$15tb$1@gioia.aioe.org>
In reply to#33540
On 1/26/2016 9:01 PM, Jeff Higgins wrote:
> Or you could look in the Java Platform API Specification,
> also called "the javadocs" for these methods which could make
> your coding easier to learn and read.
>
> java.lang.Integer.parseInt(String s, int radix)
> java.lang.Integer.toString(int i, int radix)

Depending on whether the purpose is to convert between
decimal and hex or the purpose is to learn imperative
programming.

:-)

Arne

[toc] | [prev] | [next] | [standalone]


#33547

FromJeff Higgins <jeff@invalid.invalid>
Date2016-01-28 06:47 +0000
Message-ID<n8cdit$959$1@dont-email.me>
In reply to#33546
On Wed, 27 Jan 2016 21:54:16 -0500, Arne Vajhøj wrote:

> On 1/26/2016 9:01 PM, Jeff Higgins wrote:
>> Or you could look in the Java Platform API Specification, also called
>> "the javadocs" for these methods which could make your coding easier to
>> learn and read.
>>
>> java.lang.Integer.parseInt(String s, int radix)
>> java.lang.Integer.toString(int i, int radix)
> 
> Depending on whether the purpose is to convert between decimal and hex
> or the purpose is to learn imperative programming.

Regulations and Syllabuses - ICSE 
<http://www.cisce.org/pdf/ICSE-Class-X-Syllabus-Year-2016/20.Computer%
20Application.pdf>
Specimen Question Papers - ICSE 
<http://www.cisce.org/pdf/ICSE-Class-X-Specimen-Question-Papers-2014/
Computer%20Applications.pdf>

> 
> :-)
> 
> Arne

[toc] | [prev] | [standalone]


Back to top | Article view | comp.lang.java.programmer


csiph-web