Hi friends,
Can any one tell me the java program that how to convert Octa Decimal to HexaDecimal without using inbuild functions.
I tried a lot but not successful.
Please Please Please post the program...
Thanks in advance..
Hi friends,
Can any one tell me the java program that how to convert Octa Decimal to HexaDecimal without using inbuild functions.
I tried a lot but not successful.
Please Please Please post the program...
Thanks in advance..
Following 's multiply-and-add idea, below is a compact, clear Java implementation that converts an octal string to a hexadecimal string without using any built-in base-conversion helpers (no Integer.parseInt(..., 8), no toHexString, no new BigInteger(octal, 8)). It parses the octal digits left-to-right into an accumulator and then emits hex digits by repeated division by 16. 's point about learning is noted: this shows the algorithm so the mechanics are understood, not just a copy/paste solution.
Key points: the code validates digits (0–7), trims whitespace, supports an optional leading minus sign, and uses BigInteger so very large octal values won’t overflow. Time complexity is O(n) to parse plus O(k) to produce k hex digits.
import java.math.BigInteger;
public class OctalToHex {
private static final BigInteger BASE8 = BigInteger.valueOf(8);
private static final BigInteger BASE16 = BigInteger.valueOf(16);
public static String octalToHex(String octal) {
if (octal == null) throw new IllegalArgumentException("Null input");
octal = octal.trim();
if (octal.isEmpty()) throw new IllegalArgumentException("Empty input");
boolean negative = false;
if (octal.charAt(0) == '-') {
negative = true;
octal = octal.substring(1);
if (octal.isEmpty()) throw new IllegalArgumentException("No digits after '-'");
}
BigInteger acc = BigInteger.ZERO;
for (int i = 0; i < octal.length(); i++) {
char c = octal.charAt(i);
if (c < '0' || c > '7') throw new IllegalArgumentException("Invalid octal digit: " + c);
acc = acc.multiply(BASE8).add(BigInteger.valueOf(c - '0'));
}
if (acc.equals(BigInteger.ZERO)) return "0";
StringBuilder hex = new StringBuilder();
while (acc.signum() > 0) {
BigInteger[] qr = acc.divideAndRemainder(BASE16);
int rem = qr[1].intValue();
hex.append(rem < 10 ? (char)('0' + rem) : (char)('A' + rem - 10));
acc = qr[0];
}
if (negative) hex.append('-');
return hex.reverse().toString();
}
public static void main(String[] args) {
System.out.println(octalToHex("17")); // F
System.out.println(octalToHex("10")); // 8
System.out.println(octalToHex("0")); // 0
System.out.println(octalToHex("-377")); // -FF
}
} Troubleshooting notes:
long accumulator can be used instead of BigInteger, but watch for overflow. "0007" returns "7".Jump to Post— JamesCherrill 4,733There are lots of people here who will freely give their time to help you become the best Java programmer you can be. There's nobody here who is interested in helping you cheat or doing your homework for you.
DaniWeb Member Rules (which you agreed to when you signed up) …
There are lots of people here who will freely give their time to help you become the best Java programmer you can be. There's nobody here who is interested in helping you cheat or doing your homework for you.
DaniWeb Member Rules (which you agreed to when you signed up) include:
"Do provide evidence of having done some work yourself if posting questions from school or work assignments"
http://www.daniweb.com/community/rules
This is something you can easily do yourself! Just take your numeral one digit at a time from left to right and keep an accumulator that starts at 0. For each digit, multiply the accumulator by the base and then add the value of the digit. You can reverse the process to print out the number in whatever base you like by using % and division, but of course that will get the digits from right to left so you'd have to construct the string before writing it.
We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.