Posted: Sat Jan 08, 2005 1:34 pm Post subject: (No subject)
Are there ways of converting any binary number into hexadecimal without any built-in methods?
Andy
Posted: Sat Jan 08, 2005 4:08 pm Post subject: (No subject)
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
Posted: Sat Jan 08, 2005 4:34 pm Post subject: (No subject)
java wrote:
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
Posted: Sat Jan 08, 2005 7:35 pm Post subject: (No subject)
Andy wrote:
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.
wtd wrote:
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
Posted: Sat Jan 08, 2005 8:11 pm Post subject: (No subject)
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
code:
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
Posted: Sat Jan 08, 2005 9:14 pm Post subject: (No subject)
Well, I don't want to post direct code, but I can give you the closest thing to psuedo-code.
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
Posted: Sat Jan 08, 2005 10:57 pm Post subject: (No subject)
Andy, wtd, thanks for your help, really appreciate it
java
Posted: Sat Jan 15, 2005 2:55 pm Post subject: (No subject)
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
Posted: Sat Jan 15, 2005 5:01 pm Post subject: (No subject)
java wrote:
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
Posted: Sat Jan 15, 2005 5:44 pm Post subject: (No subject)
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
Posted: Sat Jan 15, 2005 6:14 pm Post subject: (No subject)
sry the code i posted last time was slitely buggy.. sory for the inconvenience
code:
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
Posted: Sun Jan 16, 2005 1:35 pm Post subject: (No subject)
wtd wrote:
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.
Andy wrote:
sry the code i posted last time was slitely buggy.. sory for the inconvenience
code:
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[17]="0123456789ABCDEF";
Andy
Posted: Sun Jan 16, 2005 4:02 pm Post subject: (No subject)
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