Tom's Guide | Tom's Hardware | Tom's Games
![]() |
![]() |
![]() |
Hey,
I need help with a problem in java. My project is to turn any given base 10 number into an hexadecimal number. I need to know how to do this. It is the algorithm i need since I already am a professional programmer?

int convert = ?;
stringbuffer strbuf = new stringbuffer( );
int rem =0;
int div =0;
string hex_val = new string( );
while( div != 1 && convert != 0 ) {
div = convert/16;
rem = convert%16;
convert = div;
//retrieve hex val of rem in table you create
hex_val = getHexVal( rem );
strbuf.append( hex_val );
}
stringbuffer hex_string = strbuf.reverse( );
//done

to convert decimal to hexadecimal you can use the following method
hex is a 16 bit number so you will have 16 digits, here is an examplebase 10 ---> hex
10/2 = 5, remainder 0
5/2 = 2, remaider 1
2/2 = 1, remainder 0
1/2= x, remainder 1
when dividing, you only work with whole numbers no fractions
how allign the remainders from bottom to top that's the binary = 1010now hex is in 16 bits so the final answer here is
0000 0000 0000 1010 = this is 10 in hexyou just append the zeros
so when you write your code, it'll be a good idea to work with arrays and keep track of the array index

#include
int atoi(char s[],int base){
int i,n;
n=0;
for (i=0; (s[i]>='0' && s[i]='a' &&
s[i]='0' && s[i]='a' && s[i]0)
itoa(num/base,base);
i=num%base;
if (i=10 && i4) || (argc36 || base2>=36){
printf("\nUSAGE: CONVERT 12 10 5\nBase in range 1 to 36\n\n");
return 0;
}answer=atoi(argv[1],base1);
printf("Convert %s in base %i to base %i is ",argv[1],base1,base2);
itoa(answer,base2);
printf("\n\n");
return 0;
}

Oh, it seems that it doesn't show al the code.
Forget the one above, I will divide it in two post...
#include
int atoi(char s[],int base){
int i,n;
n=0;
for (i=0; (s[i]>='0' && s[i]='a' &&
s[i]='0' && s[i]='a' && s[i]0)
itoa(num/base,base);
i=num%base;
if (i=10 && i=36)
printf("%c",'a'+(i-10));
}

It doesn't show my code correctly, i don't know why...
void itoa(int num, int base){
int i;
if (num/base>0)
itoa(num/base,base);
i=num%base;
if (i=10 && i=36)
printf("%c",'a'+(i-10));I think this is the important part.

=================================================
Clicker,Sorry to disagree but hexidecimal is NOT a 16 bit number. Hex can be ANY data or data type. The number of bits is completly unimportant as log as the data type is one the computer (or the compiler through software emulation) can manage. Hex (like oct, dec or binary) are all simply different ways of representing the EXACT SAME DATA.
It seems necessary to repeat this alot lately but this is a point that should be better understood by more programmers.
borelli35

For clarification, hex is a 16 DIGIT number and I can only assume that this is what was actually meant.
borelli35

borelli35
Your first response made sense and is correct, but the second response threw me.
I assume that you meant that hex is a 16 digit "number system".
Your comment about Clicker's comment about hex being a 16 bit number is correct. The number of bits depends upon the data type. A two byte value contains 16 bits, a four byte value contains 32 bits, etc. All values can be represented in hex, octal, decimal, base 7, base 49, whatever you want.Hex and binary (and to some extent octal) are used because they easily match the binary states of the hardware. Decimal is used because that's what we humans are used to (I suppose because we have 10 fingers and 10 toes).

=================================================
Don,I was trying to clarify that I realize that clicker was attempting to say that hex numbers where represented as a base 16 (0-F) or 16 digits as apposed to base 10 (0-10). I might have come off strong in the first reply but wanted to be clear about it and then looking at it decided to give clicker credit for possibly understanding part of it (even if thier explaination invalidated this).
In resonse of the 'test for the day', this would depend upon how you represented digits after 36 (upper case letters first and then lower case?). 0-10 are obvious. 11-36=A-Z. 37-49=a-m? If this was to be the case then the representation of decimal 57 in base 49 would be 17.
borelli35

I was careful to pick an example where it didn't matter what was used to represent the digits above 9. As long as the digits 0 - 9 are used as normal, then the answer would be 18 regardless of how the other digits were represented.

=================================================
What was I thinking? Naturally, 1 and 8 being the base 10 part of any number system above base 10 this would be the case. Yes, I made the classic 'off by 1' error here (I hate it when I do that!). It's been a while since I've had to do this in my head. I forgot that I actually enjoy this stuff! You know you are a computer/programming geek when....borelli35

![]() |
Can Someone help? C Progr...
|
IE settings
|

This post is quite old and has been locked from receiving new replies. Please create a new posting instead.
| Ads by Google |