Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6619729
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T21:00:27+00:00 2026-05-25T21:00:27+00:00

for an assignment I have to write a program that will take in an

  • 0

for an assignment I have to write a program that will take in an 8 character string(hexadecimal) and then convert it to base 10. I am not allowed to use any outside classes to do this. I’m pretty sure I have it working properly… for positive numbers only. My problem is how to show negative numbers.
an example is that FFFFFFFA should print out as -6
This is my code so far

package hexconverter;

import java.util.*;

/**
 *
 * @author Steven
 */
public class Main {

    Scanner scanner = new Scanner(System.in);

    public void doWork() {



        System.err.println("Please enter the internal representation: ");
        String hex;
        hex = scanner.next();
        hex = hex.toUpperCase();

        long count = 1;
        long ans = 0;

        for (int i = 7; i >= 0; i--) {
            Character c = hex.charAt(i);

            if (c != '1' && c != '2' && c != '3' && c != '4' && c != '5' && c != '6' && c != '7' && c != '8' && c != '9') {
                int num = fixLetters(c);
                ans = ans + (num * count);
                count = count * 16;
            } else {

                String s = c.toString(c);
                long num = Integer.parseInt(s);
                ans = ans + (num * count);
                count = count * 16;
            }
        }

       if (ans > 2147483647) {
            System.out.println("is negative");


       } else {
            System.out.println(ans);
       }
    }

    public int fixLetters(Character c) {
        if (c.equals('A')) {
            return 10;
        } else if (c.equals('B')) {
            return 11;
        } else if (c.equals('C')) {
            return 12;
        } else if (c.equals('D')) {
            return 13;
        } else if (c.equals('E')) {
            return 14;
        } else if (c.equals('F')) {
            return 15;
        } else {
            return 0;
        }

    }

    public static void main(String[] args) {
        // TODO code application logic here
        Main a = new Main();
        a.doWork();
    }
}

I think my test for a negative integer is correct… since that is the highest value 32 bits can hold, anything over that will be an overflow so that means it should be negative.
From here I have no idea how to go about this. Any pointers or tips would be greatly appreciated. If there is no way to do it mathematically I feel like I am going to have to convert the hex into binary and then perform twos complements on it, but again I dont know where to start.

Thanks in advance

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-25T21:00:28+00:00Added an answer on May 25, 2026 at 9:00 pm

    In 32-bit 2’s complement binary representation, the value of a negative is exactly 2 ^ 32 less than the value of the same bit pattern in unsigned representation. You have already identified that the number might be negative; all that’s left to do is subtract 2 ^ 32.

    Of course, 2 ^ 32 (4294967296 in decimal, or 0x100000000 in hex) is a value that can’t be represented by Java’s “int” type, so you’ll need to use a “long”:

    if (ans > 2147483647) {
        // System.out.println("is negative");
        ans = ans - 0x100000000L;
        System.out.println(ans);
    } else {
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a class assignment where I have to write a program that will
For class I have an assignment: Write a C++ program that will output the
I have been asked to write a program using python for an assignment. I
As part of a homework assignment, I have to write a C program in
I have an assignment in my C programming class to write a program to
*My assignment is to write a program that stores an array of five int
I have to write a program in which main calls other functions that test
For a school assignment I have to write x86 assembly code, except I can't
I have to write a multiplayer pacman game in Java for a university assignment
I have an assignment about fftw and I was trying to write a small

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.