Welcome to collectivesolver - Programming & Software Q&A with code examples. A website with trusted programming answers. All programs are tested and work.

Contact: aviboots(AT)netvision.net.il

Semrush - keyword research tool

Create your online store today with Shopify

Turn ChatGPT, Claude, Gemini, And CoPilot Into Your Personal Assistant, Business Coach, Content Creator, And More

AFFILIATE MARKETING Your all-in-one performance engine Manage affiliates, creators, and customer referrals in one unified platform—turning every partnership into measurable growth

Secure & Reliable Web Hosting, Free Domain, Free SSL, 1-Click WordPress Install, Expert 24/7 Support

Disclosure: My content contains affiliate links.

43,236 questions

56,139 answers

573 users

How to convert hexadecimal digit to a decimal value in Java

2 Answers

0 votes
public class MyClass {
    public static void main(String args[]) {
        String hex = "C";
        char ch = hex.charAt(0);
       
        if (ch <= 'F' && ch >= 'A') {
           int value = ch - 'A' + 10;
           System.out.println("Decimal value = " + value);
        } 
        else if (Character.isDigit(ch)) {
            System.out.println("Decimal value = " + ch);
        }
        else {
            System.out.println("Hex digit error");
        }
    }
}


/*
run:

Decimal value = 12

*/

 



answered Jun 13, 2019 by avibootz
0 votes
public class MyClass {
    static int convert_hex_to_dec(String hex) {
        char ch = hex.charAt(0);
       
        if (ch <= 'F' && ch >= 'A') {
           return ch - 'A' + 10;
        } 
        else if (Character.isDigit(ch)) {
            return ch - '0';
        }
        else {
            return -1;
        }
    }
    public static void main(String args[]) {
        System.out.println(convert_hex_to_dec("C"));
        System.out.println(convert_hex_to_dec("3"));
        System.out.println(convert_hex_to_dec("x"));
    }
}



/*
run:

12
3
-1

*/

 



answered Jun 13, 2019 by avibootz

Related questions

1 answer 236 views
1 answer 178 views
1 answer 196 views
1 answer 201 views
1 answer 233 views
...