munir hussain

Greenhorn

Posts: 5

posted 8 years ago

how to convert a hex or binary number to decimal and vice versa using a any java mehtod, can any one help.

and one more thing that can any one explain how computer store and retrieve - sign . i am so much confusd with left and right shifts

i have mentioned 2 codes here.

1). our input variable "int num = 0xFFFFFFE". how after all operations the result

comes in decimal

.

2). we have a String array of hex numbers. how the resultant values are searched in the array.and why weare doing

"[(b >> 4) & 0x0f]" and [b & 0x0f].

any solid reasons

examle1

// Left shifting as a quick way to multiply by 2.

class MultByTwo {

public static void main(String args[]) {

int i;

int num = 0xFFFFFFE;

for(i=0; i<4; i++) {

num = num << 1;

System.out.println(num);

}

}

}

The program generates the following output:

536870908

1073741816

2147483632

-3

example 3

// Masking sign extension.

class HexByte {

static public void main(String args[]) {

char hex[] = {

'0', '1', '2', '3', '4', '5', '6', '7',

'8', '9', 'a', 'b', 'c', 'd', 'e', 'f'

};

byte b = (byte) 0xf1;

System.out.println("b = 0x" + hex[(b >> 4) & 0x0f] + hex[b & 0x0f]);

}

}

Here is the output of this program:

b = 0xf1

and one more thing that can any one explain how computer store and retrieve - sign . i am so much confusd with left and right shifts

i have mentioned 2 codes here.

1). our input variable "int num = 0xFFFFFFE". how after all operations the result

comes in decimal

.

2). we have a String array of hex numbers. how the resultant values are searched in the array.and why weare doing

"[(b >> 4) & 0x0f]" and [b & 0x0f].

any solid reasons

examle1

// Left shifting as a quick way to multiply by 2.

class MultByTwo {

public static void main(String args[]) {

int i;

int num = 0xFFFFFFE;

for(i=0; i<4; i++) {

num = num << 1;

System.out.println(num);

}

}

}

The program generates the following output:

536870908

1073741816

2147483632

-3

example 3

// Masking sign extension.

class HexByte {

static public void main(String args[]) {

char hex[] = {

'0', '1', '2', '3', '4', '5', '6', '7',

'8', '9', 'a', 'b', 'c', 'd', 'e', 'f'

};

byte b = (byte) 0xf1;

System.out.println("b = 0x" + hex[(b >> 4) & 0x0f] + hex[b & 0x0f]);

}

}

Here is the output of this program:

b = 0xf1

muneer hussain ali

Campbell Ritchie

Marshal

Posts: 54909

155

posted 8 years ago

Please use the CODE button and maintain indentation; what you quoted is difficult to read.

Your question is too big for a complete answer. I suggest you convert binary to decimal by working from right to left:

Find a decent book, eg Alan Clements

You halve the number repeatedly, adding the remainder from right to left to your binary number.

Take a number 9865129 Divide by two, and look for a remainder 4932564 rem 1. The last number of your binary number is 1 Divide by two, and look for a remainder 2466282 rem 0. Add a 0 to the start of your binary number 01 Divide by two, and look for a remainder 1233141 rem 0. Add a 0 to the start of your binary number 001 Divide by two, and look for a remainder 616570 rem 1. Add a 1 to the start of your binary number 1001 Divide by two, and look for a remainder 308285 rem 0. Add a 0 to the start of your binary number 01001 Divide by two, and look for a remainder 154142 rem 1. Add a 1 to the start of your binary number 101001 Divide by two, and look for a remainder 77071 rem 0. Add a 0 to the start of your binary number 0101001 Divide by two, and look for a remainder 38535 rem 1. Add a 1 to the start of your binary number 10101001 Divide by two, and look for a remainder 19267 rem 1. Add a 1 to the start of your binary number 110101001 Divide by two, and look for a remainder 9633 rem 1. Add a 1 to the start of your binary number 1110101001 Divide by two, and look for a remainder 4816 rem 1. Add a 1 to the start of your binary number 11110101001 Divide by two, and look for a remainder 2408 rem 0. Add a 0 to the start of your binary number 011110101001 Divide by two, and look for a remainder 1204 rem 0. Add a 0 to the start of your binary number 0011110101001 Divide by two, and look for a remainder 602 rem 0. Add a 0 to the start of your binary number 00011110101001 Divide by two, and look for a remainder 301 rem 0. Add a 0 to the start of your binary number 000011110101001 Divide by two, and look for a remainder 150 rem 1. Add a 1 to the start of your binary number 1000011110101001 Divide by two, and look for a remainder 75 rem 0. Add a 0 to the start of your binary number 01000011110101001 Divide by two, and look for a remainder 37 rem 1. Add a 1 to the start of your binary number 101000011110101001 Divide by two, and look for a remainder 18 rem 1. Add a 1 to the start of your binary number 1101000011110101001 Divide by two, and look for a remainder 9 rem 0. Add a 0 to the start of your binary number 01101000011110101001 Divide by two, and look for a remainder 4 rem 1. Add a 1 to the start of your binary number 101101000011110101001 Divide by two, and look for a remainder 2 rem 0. Add a 0 to the start of your binary number 0101101000011110101001 Divide by two, and look for a remainder 1 rem 0. Add a 0 to the start of your binary number 00101101000011110101001 Divide by two, and look for a remainder 0 rem 1. Add a 1 to the start of your binary number 100101101000011110101001 Finished.

To convert back repeatedly take the last number off the binary number, and multiply it by a doubling factor.Start with 0. Take 100101101000011110101001; last digit is 1, add 1 = 1 Take 10010110100001111010100; last digit is 0, add 2 * 0; total = 1 Take 1001011010000111101010; last digit is 0, add 4 * 0; total = 1 Take 100101101000011110101; last digit is 1, add 8 * 1; total = 9 Take 10010110100001111010; last digit is 0, add 16 * 0; total = 9 Take 1001011010000111101; last digit is 1, add 32 * 1; total = 41 Take 100101101000011110; last digit is 0, add 64 * 0; total = 41 Take 10010110100001111; last digit is 1, add 128 * 1; total = 169 Take 1001011010000111; last digit is 1, add 256 * 1; total = 425 Take 100101101000011; last digit is 1, add 512 * 1; total = 937 Take 10010110100001; last digit is 1, add 1024 * 1; total = 1961 Take 1001011010000; last digit is 0, add 2048 * 0; total = 1961 Take 100101101000; last digit is 0, add 4096 * 0; total = 1961 Take 10010110100; last digit is 0, add 8192 * 0; total = 1961 Take 1001011010; last digit is 0, add 16384 * 0; total = 1961 Take 100101101; last digit is 1 add 32768 * 1; total = 34729 Take 10010110; last digit is 0 add 65536 * 0; total = 34729 Take 1001011; last digit is 1 add 131072 * 1; total = 165801 Take 100101; last digit is 1 add 262144 * 1; total = 427945 Take 10010; last digit is 0 add 524288 * 0; total = 427945 Take 1001; last digit is 1 add 1048576 * 1; total = 1476521 Take 100; last digit is 0 add 2097152 * 0; total = 1476521 Take 10; last digit is 0 add 4194304 * 0; total = 1476521 Take 1; last digit is 1 add 8388608 * 1; total = 9865129 Finished.

Your question is too big for a complete answer. I suggest you convert binary to decimal by working from right to left:

Find a decent book, eg Alan Clements

*the Principles of Computer Hardware*Oxford: Oxford University Press (I have 3rd edition (2000) ISBN 0-19-856453-8 but there is a 4th edition at least available). You then find how to convert from decimal to binary in chapter 4, pages 151-158.You halve the number repeatedly, adding the remainder from right to left to your binary number.

To convert back repeatedly take the last number off the binary number, and multiply it by a doubling factor.