Computing.Net > Forums > Programming > Base to Hexadecimal

Computer Problems? Computing.Net has over 1,000,000 posts about all things technology related! Over 90% answered within 24 hours! Click here to start participating now! Also, be sure to check out the New User Guide.

Base to Hexadecimal

Reply to Message Icon

Name: chamander
Date: September 11, 2003 at 09:13:58 Pacific
OS: Windows XP
CPU/Ram: 1.67ghz XP + 512ddr
Comment:

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?



Sponsored Link
Ads by Google

Response Number 1
Name: jtoad
Date: September 11, 2003 at 10:50:28 Pacific
Reply:

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


0

Response Number 2
Name: Clicker
Date: September 11, 2003 at 17:15:29 Pacific
Reply:

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 example

base 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 = 1010

now hex is in 16 bits so the final answer here is
0000 0000 0000 1010 = this is 10 in hex

you 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


0

Response Number 3
Name: philipl
Date: September 12, 2003 at 09:29:13 Pacific
Reply:

#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;
}


0

Response Number 4
Name: philip
Date: September 12, 2003 at 09:30:32 Pacific
Reply:

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));
}


0

Response Number 5
Name: philipl
Date: September 12, 2003 at 09:37:29 Pacific
Reply:

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.


0

Related Posts

See More



Response Number 6
Name: philipl
Date: September 12, 2003 at 09:38:20 Pacific
Reply:

And this

else if (i>=10 && i=36)
printf("%c",'a'+(i-10));
}


0

Response Number 7
Name: philipl
Date: September 12, 2003 at 09:42:22 Pacific
Reply:

Damn,
The first if statment should be
if (i10)
printf("%c", i+'0');
sorry about that


0

Response Number 8
Name: borelli35
Date: September 13, 2003 at 22:16:33 Pacific
Reply:

=================================================
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


0

Response Number 9
Name: borelli35
Date: September 13, 2003 at 22:32:29 Pacific
Reply:

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

borelli35


0

Response Number 10
Name: Don Arnett
Date: September 13, 2003 at 22:50:03 Pacific
Reply:

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).


0

Response Number 11
Name: Don Arnett
Date: September 13, 2003 at 22:51:41 Pacific
Reply:


Test for today:

How would you represent the decimal value 57 in base 49??


0

Response Number 12
Name: borelli35
Date: September 14, 2003 at 15:41:06 Pacific
Reply:

=================================================
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



0

Response Number 13
Name: Don Arnett
Date: September 14, 2003 at 16:14:30 Pacific
Reply:

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.



0

Response Number 14
Name: borelli35
Date: September 14, 2003 at 16:36:47 Pacific
Reply:

=================================================
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


0

Sponsored Link
Ads by Google
Reply to Message Icon

Can Someone help? C Progr... IE settings



Post Locked

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


Go to Programming Forum Home


Sponsored links

Ads by Google


Results for: Base to Hexadecimal

function to convert between bases www.computing.net/answers/programming/function-to-convert-between-bases/13985.html

Integer to Hexadecimal (Standard C) www.computing.net/answers/programming/integer-to-hexadecimal-standard-c/14515.html

DEC to HEX conversion? www.computing.net/answers/programming/dec-to-hex-conversion/3613.html