
-----------------------------------
java
Sat Jan 08, 2005 1:23 pm

Convert Help
-----------------------------------
Can anyone here explain to me how to convert a binary number into a hexadecimal in Java RTP?

I've searched on this site, but could not find anything.

Any help is appreciated

Thank You

-----------------------------------
rizzix
Sat Jan 08, 2005 1:25 pm


-----------------------------------
u mean convert binary strings to hex? have u taken a  look at: [url=http://www.compsci.ca/v2/viewtopic.php?t=6459]Conversions: Number  String ;)

-----------------------------------
java
Sat Jan 08, 2005 1:34 pm


-----------------------------------
Are there ways of converting any binary number into hexadecimal without any built-in methods?

-----------------------------------
Andy
Sat Jan 08, 2005 4:08 pm


-----------------------------------
then u'd have to use some math... as you noe, every four digits of binary can be converted to 1 digit of hex, so all u need is an index to convert four binary numbers in to an hex digit. u can do that in several ways, but the easiest would be take the four digits, change in to base 10, then use an array to locate what the hex number using the base 10 value

-----------------------------------
wtd
Sat Jan 08, 2005 4:34 pm


-----------------------------------
Are there ways of converting any binary number into hexadecimal without any built-in methods?

Yes, but the singular redeeming aspect of Java is the massive library of existing code you can leverage to do the heavy lifting for you.  If you don't learn to make use of that resource, there's absolutely no point in learning Java.

And I'm referring to Suns Java, not Holt's ridiculous Ready To Program.

My $0.015. :)

-----------------------------------
java
Sat Jan 08, 2005 7:35 pm


-----------------------------------
then u'd have to use some math... as you noe, every four digits of binary can be converted to 1 digit of hex, so all u need is an index to convert four binary numbers in to an hex digit. u can do that in several ways, but the easiest would be take the four digits, change in to base 10, then use an array to locate what the hex number using the base 10 value

Can you please help me out with the code?, I'm completely lost with what you said. 

Yes, but the singular redeeming aspect of Java is the massive library of existing code you can leverage to do the heavy lifting for you.  If you don't learn to make use of that resource, there's absolutely no point in learning Java.

And I'm referring to Suns Java, not Holt's ridiculous Ready To Program.

My $0.015. :)

Thanks, but I'm using Ready To Program and I can not use the built-in functions.

-----------------------------------
Andy
Sat Jan 08, 2005 8:11 pm


-----------------------------------
im not a java pro, but i do know the syntax since it is the same as c++
lets suppose you have a binary number with four digits 

int num=1011;
char hexletter[17];
char hex;
hexletter="0123456789ABCDEF";
hex=hexletter[(num/125)+(num-(num/1000)-(num%100))/25+(num-(num/100)-(num%10))/5+(num%10)];


-----------------------------------
wtd
Sat Jan 08, 2005 9:14 pm


-----------------------------------
Well, I don't want to post direct code, but I can give you the closest thing to psuedo-code.

-- Turn an integer into a string
convertToStr :: Int -> Int -> String
-- Regardless of radix, zero is an empty string
convertToStr 0 _ = ""
-- With any other number...
convertToStr n r = convertToStr nextNumber r ++ thisDigitStr
  where 
    -- All possible digits
    digits       = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ"
    -- Find the last digit by "mod"ing 
    -- the number by the radix
    thisDigit    = n `mod` r
    -- Find the digit in the string which
    -- corresponds to the digit we just calculated
    thisDigitStr = [digits !! thisDigit]
    -- The next time we run the function
    -- we use the number input minus the last digit
    nextNumber   = n `div` r

A sample run:

convertToStr 255 16

16 indicates this is conversion to hex.

convertToStr 255 16
convertToStr 15 16 ++ [digits !! 15]
convertToStr 15 16 ++ "F"
[digits !! 15] ++ "F"
convertToStr 0 16 ++ "F" ++ "F"
"" ++ "F" ++ "F"
"FF"

-----------------------------------
java
Sat Jan 08, 2005 10:57 pm


-----------------------------------
Andy, wtd, thanks for your help, really appreciate it

-----------------------------------
java
Sat Jan 15, 2005 2:55 pm


-----------------------------------
I've spent a week on trying to figure this out, so far I havent been able to do it and Andy's code isnt working for me. I would really appreciate it if someone can give me specific code that would convert binary into hexadecimal because right now I have no clue. Thanks

-----------------------------------
wtd
Sat Jan 15, 2005 5:01 pm


-----------------------------------
I've spent a week on trying to figure this out, so far I havent been able to do it and Andy's code isnt working for me. I would really appreciate it if someone can give me specific code that would convert binary into hexadecimal because right now I have no clue. Thanks

The thing is, people here are reluctant to just give away complete source code.  It's just asking for plagiarism, and we don't want to alienate teachers because we believe that compsci.ca can be a great learning tool.

If we help with plagiarism, teachers will tell students not to frequent this site and we'll lose an opportunity to help.

-----------------------------------
Andy
Sat Jan 15, 2005 5:44 pm


-----------------------------------
my way only works for four digit binary numbers, if u want it to work with all binary numbers, then you'd have to break it down in digits of 4

-----------------------------------
Andy
Sat Jan 15, 2005 6:14 pm


-----------------------------------
sry the code i posted last time was slitely buggy.. sory for the inconvenience

	int num=1111;
	char hexletter[17]="0123456789ABCDEF";
	char hex;
	hex=hexletter[(num/125)+((num%1000)-(num%100))/25+((num%100)-(num%10))/5+(num%10)];


this only works for four digit binary numbers

-----------------------------------
java
Sun Jan 16, 2005 1:35 pm


-----------------------------------
The thing is, people here are reluctant to just give away complete source code.  It's just asking for plagiarism, and we don't want to alienate teachers because we believe that compsci.ca can be a great learning tool.

If we help with plagiarism, teachers will tell students not to frequent this site and we'll lose an opportunity to help.

My teacher told me that I may use someone elses code, although I might lose some marks because the code is not completely mine, but I have to give them credit for it. So if you do decide to help me, I will guarantee you that I will place comment lines stating that I received this portion of code from you.

sry the code i posted last time was slitely buggy.. sory for the inconvenience

	int num=1111;
	char hexletter[17]="0123456789ABCDEF";
	char hex;
	hex=hexletter[(num/125)+((num%1000)-(num%100))/25+((num%100)-(num%10))/5+(num%10)];


this only works for four digit binary numbers

When I use this code I get a Syntax Error: Unexpected Symbol Ignored on this part char hexletter="0123456789ABCDEF";

-----------------------------------
Andy
Sun Jan 16, 2005 4:02 pm


-----------------------------------
hmmmm like i said, im a c++ programmer, not a java programmer. if that doesnt work then, simply write an array of characters called hexletter and declare it from 0 to F

-----------------------------------
wtd
Sun Jan 16, 2005 6:45 pm


-----------------------------------
The thing is, people here are reluctant to just give away complete source code.  It's just asking for plagiarism, and we don't want to alienate teachers because we believe that compsci.ca can be a great learning tool.

If we help with plagiarism, teachers will tell students not to frequent this site and we'll lose an opportunity to help.

My teacher told me that I may use someone elses code, although I might lose some marks because the code is not completely mine, but I have to give them credit for it. So if you do decide to help me, I will guarantee you that I will place comment lines stating that I received this portion of code from you.

sry the code i posted last time was slitely buggy.. sory for the inconvenience

	int num=1111;
	char hexletter[17]="0123456789ABCDEF";
	char hex;
	hex=hexletter[(num/125)+((num%1000)-(num%100))/25+((num%100)-(num%10))/5+(num%10)];


this only works for four digit binary numbers

When I use this code I get a Syntax Error: Unexpected Symbol Ignored on this part char hexletter="0123456789ABCDEF";

What are you using to compile this?

-----------------------------------
java
Sun Jan 16, 2005 8:13 pm


-----------------------------------
What are you using to compile this?

I dont understand your question.

-----------------------------------
wtd
Sun Jan 16, 2005 8:28 pm


-----------------------------------
What are you using to compile this?

I dont understand your question.

Are you using the standard Java tools from Sun, or Ready to Program?

-----------------------------------
Andy
Sun Jan 16, 2005 9:33 pm


-----------------------------------
i dunno that code works fine in c++.. i dont have a java compiler installed so i couldnt test it... wtd does it work with eclipse or jcreator?

-----------------------------------
rizzix
Sun Jan 16, 2005 9:34 pm


-----------------------------------
jeez. why would that matter? huh?  :?

-----------------------------------
java
Sun Jan 16, 2005 10:00 pm


-----------------------------------
I use Ready to Program

-----------------------------------
wtd
Sun Jan 16, 2005 11:17 pm


-----------------------------------
I use Ready to Program

Well, there's your problem.

:: sigh ::

-----------------------------------
java
Mon Jan 17, 2005 2:16 pm


-----------------------------------


Well, there's your problem.

:: sigh ::

 :cry: Then what am I supposed to do?

-----------------------------------
McKenzie
Mon Jan 17, 2005 4:03 pm


-----------------------------------
The problem has nothing to do with the compiler. Andy wrote C++ code. You need to convert that to Java. You need to know how to use strings in Java. e.g. instead of:
 char hexletter[17]="0123456789ABCDEF"; 
use:
String hexletter="0123456789ABCDEF"; 
There are other minor changes, but you need to know enough Java to overcome them.

-----------------------------------
Andy
Mon Jan 17, 2005 6:53 pm


-----------------------------------
wait.. so u cant have an character array style string in java?

-----------------------------------
rizzix
Mon Jan 17, 2005 6:56 pm


-----------------------------------
no u can only have a character style array in java:
char[4] theChars = {'a', 'b', 'c', '\0'};

-----------------------------------
Andy
Mon Jan 17, 2005 7:03 pm


-----------------------------------
Oooo.. the [] has to be beside the char.. thats y it didnt work on my friends compiler.. thx rizzix

-----------------------------------
java
Mon Jan 17, 2005 10:54 pm


-----------------------------------
a
a

-----------------------------------
Andy
Tue Jan 18, 2005 5:58 pm


-----------------------------------
read what rizzix wrote, and use that instead of my way of declaring the character array.. if u still dont get it then post here.. but at least try yourself

-----------------------------------
java
Tue Jan 18, 2005 8:30 pm


-----------------------------------
read what rizzix wrote, and use that instead of my way of declaring the character array.. if u still dont get it then post here.. but at least try yourself

I read what he wrote about the char array, but I don't understand it. I'm not the best at Java, so can you help me out by telling me what to put in my code and where. That would really help me out because I've been trying but getting no-where.

-----------------------------------
Andy
Thu Jan 20, 2005 5:16 pm


-----------------------------------
we've given you enough help.. we cant do your homework for you..  what rizzix posted wasnt even complicated.. and plus, u do have a compsci teacher who gets paid to help you
