I need some help with my Comp Sci class

Page 2 - Seeking answers? Join the AnandTech community: where nearly half-a-million members share solutions and discuss the latest tech.

RaynorWolfcastle

Diamond Member
Feb 8, 2001
8,968
16
81
hexadecimal is base 16
so any number between 0 and 15 can be a digit. Of course, no one actually uses two characters for a digit since that defeats the purpose so they write it as follows

0 through 9 is the same as in decmal
10 becomes A
11 - B
12 - C
13 - D
14 - E
15 - F

for example:

Decimal / hex
5 / 5
10/ A
20 / 15
32 / 20

etc. etc. I think the windows calculator has a hex conversion button
 

Nocturnal

Lifer
Jan 8, 2002
18,927
0
76
Originally posted by: dighn
Originally posted by: Nocturnal
I totally get the binary to decimal right now. But going from decimal to hex, and then from hex to octal I do not fully understand. My teacher was RUSHING big time towards the end of class and he then asked if we had any questions and right before I was about to go up and talk to him he rushed out the door in order to give out cd-rs with the newest Java SDK on it to students.

LOL

anyway
conversions between bin, hex and oct are really easy, see my posts above

as for decimal to hex use the same kind of conversion process as binary


Would it be any possiblity that you have AOL Instant Messanger or someone who understaneds this thoroughly has AIM and wouldn't mind chatting with me about it?
 

dighn

Lifer
Aug 12, 2001
22,820
4
81
sorry i only got icq but judging from the posts i'm sure others with it would

edit

ok wtf i forgot i had it installed

handle is dighn
 

charrison

Lifer
Oct 13, 1999
17,033
1
81
Originally posted by: Nocturnal
I totally get the binary to decimal right now. But going from decimal to hex, and then from hex to octal I do not fully understand. My teacher was RUSHING big time towards the end of class and he then asked if we had any questions and right before I was about to go up and talk to him he rushed out the door in order to give out cd-rs with the newest Java SDK on it to students.

Decimal<->binary is the "hardest"

binary<->hex
binary<->oct
is easy
when converting to oct to hex, is easiest to oct->binary->hex
 

Nocturnal

Lifer
Jan 8, 2002
18,927
0
76
So would this be correct for the decimal number 805 converted into a hex:

805

5x1=5
0x16=0
8x16^2=2048
 

Supermercado

Diamond Member
Jan 18, 2002
5,893
0
76
Originally posted by: Nocturnal
So would this be correct for the decimal number 805 converted into a hex:

805

5x1=5
0x16=0
8x16^2=2048
You've got that backwards. If your hex number is 805, your decimal number is 5+2048=2053. Good way to remember it: In hex, you have more digits than you do in decimal, so it takes fewer of them to represent the same number as it does in decimal. Works the same way with binary and decimal. Say you have the number 8 in decimal. That's only 1 digit, but it takes 4 (1000) to represent the same number in binary.

 

charrison

Lifer
Oct 13, 1999
17,033
1
81
Originally posted by: Nocturnal
So would this be correct for the decimal number 805 converted into a hex:

805

5x1=5
0x16=0
8x16^2=2048

more like
805/16^2=3 remainder 37
37/16=2 remainder 5
5/1=5

325hex =805 dec
 

dighn

Lifer
Aug 12, 2001
22,820
4
81
take 805 for example

first divide by 16, the result of the division is 50, with 5 as remainder. the 5 goes to the right side of the hex number: 5
the result of the division is 50, divide this by 16 again, result is 3 remainder 2, 2 goes to one place left of 5: 25
the result of the division is 3, divide by 16, result is 0 remainder 3, 3 goes to the left of 2: 325
result of the last division is 0. end of conversion

as u may see this conversion process is not difficult if u get the pattern.
 

Nocturnal

Lifer
Jan 8, 2002
18,927
0
76
Ok, so can someone go over it with me again. All of this "base," "base 2," "base 10," etc etc, you use it with a set of binary numbers. When and why do you use base 2 over base 10?
 

dighn

Lifer
Aug 12, 2001
22,820
4
81
Originally posted by: Nocturnal
Ok, so can someone go over it with me again. All of this "base," "base 2," "base 10," etc etc, you use it with a set of binary numbers. When and why do you use base 2 over base 10?

because computers work with 2 states. each digit can only handle 2 states so 0 and 1. oct and hex are just more compact ways of representing binary, they are also suitable for conversion to and from bin.

i mean DEADBEEF is a lot easier to write than 1101111010101011111011101111
 

pillage2001

Lifer
Sep 18, 2000
14,038
1
81
Originally posted by: Nocturnal
Ok, so can someone go over it with me again. All of this "base," "base 2," "base 10," etc etc, you use it with a set of binary numbers. When and why do you use base 2 over base 10?

Base 2 because you're working with computers and Base 10 because that's what you've been taught since you were young. More often then not, base 16 comes into life. :)
 

notfred

Lifer
Feb 12, 2001
38,241
4
0
I was bored, so I wrote a perl script to convert any base number to any other base number.

#!/usr/bin/perl

# convert number bases.

# usage: base.pl [input base] [output base] [input value]
# Example: convert from 567 base 10, to binary
# base.pl 10 2 567

# This will work on bases 2 - 26, a-z are used as digits after 0-9.

$sbase = $ARGV[0];
$ebase = $ARGV[1];
$value = $ARGV[2];

# standardize starting number (base 10)

# prepare values to be played with.
chomp $value;
$value = lc $value;
@values = split //, $value;
@values = reverse @values;

for(my $i = 0; $i < @values; $i++){
if($values[$i] =~ m/[a-z]/){
$values[$i] = ord($values[$i]) - 87;
}
}

# standardize starting number (base 10)
$mult = 1;
$i = 0;
while($i < @values){
$values[$i] *= $mult;
$mult *= $sbase;
$i++
}
my $base10 = 0;

for(my $i = 0; $i < @values; $i++){
$base10 += $values[$i];
}

# convert base 10 to final base
$output = '';

while($base10 >= $ebase){
$temp = $base10 % $ebase;
$base10 -= $temp;
$base10 /= $ebase;
if($temp > 9){
$output = chr($temp + 55) . $output;
}
else{
$output = $temp . $output;
}
}

if($base10 > 9){
$output = chr($base10 + 55) . $output;
}
else{
$output = $base10 . $output;
}

# print output
print "$output\n";
 

DaveSimmons

Elite Member
Aug 12, 2001
40,730
670
126
octal works exactly like decimal and binary except it's base 8 -- each digit has 8 distinct values (0-7) and is multiplied by 8 to the power of (how many digits left it is)

octal 101 = (1 x 64) + (0 x 8) + (1 x 1) = (1 x 8^2) + (0 x 8^1) + (1 x 8^0)

so to convert from octal to decimal the digits after the first are x8 x64 x512 ...

to convert from decimal to octal you divide by 8's just like by 2's for binary or 16's for hex.
 

PCMarine

Diamond Member
Oct 13, 2002
3,277
0
0
Ugh, I hated conversions to binary, hex, etc.

A bit too much busy work for my tastes :D
 

Nocturnal

Lifer
Jan 8, 2002
18,927
0
76
Ok, I'm having trouble with this: after I go from say 1-8 in octal, you need to start using two numbers. Even for binary, after so many numbers you need to use a different set or a different number for it to form another number. I am getting extremely confused with this part. So, if anyone can clarify this part I would love it. I also want to thank everyone who has helped me on AIM even though I did not understnad what you were saying, I took a little break and I'm back at it, again.
 

notfred

Lifer
Feb 12, 2001
38,241
4
0
Originally posted by: Nocturnal
Ok, I'm having trouble with this: after I go from say 1-8 in octal, you need to start using two numbers. Even for binary, after so many numbers you need to use a different set or a different number for it to form another number. I am getting extremely confused with this part. So, if anyone can clarify this part I would love it. I also want to thank everyone who has helped me on AIM even though I did not understnad what you were saying, I took a little break and I'm back at it, again.

0, 1, 2, 3, 4, 5, 6, 7, 10. 10 is: (1x8)+(0x1). to represent 9(decimal) you use 11 (1x8)+(1x1). When you get to three digits with 100 (64 in decimal), it will be (1x64)+(0x8)+(0x1)