Programming C, C++, Java, PHP, Ruby, Turing, VB
Computer Science Canada 
Programming C, C++, Java, PHP, Ruby, Turing, VB  

Username:   Password: 
 RegisterRegister   
 Convert Help
Index -> Programming, Java -> Java Help
Goto page 1, 2, 3  Next
View previous topic Printable versionDownload TopicSubscribe to this topicPrivate MessagesRefresh page View next topic
Author Message
java




PostPosted: Sat Jan 08, 2005 1:23 pm   Post subject: 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
Sponsor
Sponsor
Sponsor
sponsor
rizzix




PostPosted: Sat Jan 08, 2005 1:25 pm   Post subject: (No subject)

u mean convert binary strings to hex? have u taken a look at: Conversions: Number <=> String Wink
java




PostPosted: 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




PostPosted: 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




PostPosted: 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. Smile
java




PostPosted: 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. Smile


Thanks, but I'm using Ready To Program and I can not use the built-in functions.
Andy




PostPosted: 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




PostPosted: 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


A sample run:

code:
convertToStr 255 16


16 indicates this is conversion to hex.

code:
convertToStr 255 16
convertToStr 15 16 ++ [digits !! 15]
convertToStr 15 16 ++ "F"
[digits !! 15] ++ "F"
convertToStr 0 16 ++ "F" ++ "F"
"" ++ "F" ++ "F"
"FF"
Sponsor
Sponsor
Sponsor
sponsor
java




PostPosted: Sat Jan 08, 2005 10:57 pm   Post subject: (No subject)

Andy, wtd, thanks for your help, really appreciate it
java




PostPosted: 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




PostPosted: 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




PostPosted: 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




PostPosted: 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




PostPosted: 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




PostPosted: 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
Display posts from previous:   
   Index -> Programming, Java -> Java Help
View previous topic Tell A FriendPrintable versionDownload TopicSubscribe to this topicPrivate MessagesRefresh page View next topic

Page 1 of 3  [ 31 Posts ]
Goto page 1, 2, 3  Next
Jump to:   


Style:  
Search: