Computer Science Canada

Convert Help

Author:  java [ 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

Author:  rizzix [ Sat Jan 08, 2005 1:25 pm ]
Post subject: 

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

Author:  java [ Sat Jan 08, 2005 1:34 pm ]
Post subject: 

Are there ways of converting any binary number into hexadecimal without any built-in methods?

Author:  Andy [ Sat Jan 08, 2005 4:08 pm ]
Post 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

Author:  wtd [ Sat Jan 08, 2005 4:34 pm ]
Post 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

Author:  java [ Sat Jan 08, 2005 7:35 pm ]
Post 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.

Author:  Andy [ Sat Jan 08, 2005 8:11 pm ]
Post 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)];

Author:  wtd [ Sat Jan 08, 2005 9:14 pm ]
Post 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"

Author:  java [ Sat Jan 08, 2005 10:57 pm ]
Post subject: 

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

Author:  java [ Sat Jan 15, 2005 2:55 pm ]
Post 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

Author:  wtd [ Sat Jan 15, 2005 5:01 pm ]
Post 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.

Author:  Andy [ Sat Jan 15, 2005 5:44 pm ]
Post 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

Author:  Andy [ Sat Jan 15, 2005 6:14 pm ]
Post 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

Author:  java [ Sun Jan 16, 2005 1:35 pm ]
Post 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";

Author:  Andy [ Sun Jan 16, 2005 4:02 pm ]
Post 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

Author:  wtd [ Sun Jan 16, 2005 6:45 pm ]
Post subject: 

java wrote:
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";


What are you using to compile this?

Author:  java [ Sun Jan 16, 2005 8:13 pm ]
Post subject: 

wtd wrote:
What are you using to compile this?


I dont understand your question.

Author:  wtd [ Sun Jan 16, 2005 8:28 pm ]
Post subject: 

java wrote:
wtd wrote:
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?

Author:  Andy [ Sun Jan 16, 2005 9:33 pm ]
Post subject: 

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?

Author:  rizzix [ Sun Jan 16, 2005 9:34 pm ]
Post subject: 

jeez. why would that matter? huh? Confused

Author:  java [ Sun Jan 16, 2005 10:00 pm ]
Post subject: 

I use Ready to Program

Author:  wtd [ Sun Jan 16, 2005 11:17 pm ]
Post subject: 

java wrote:
I use Ready to Program


Well, there's your problem.

:: sigh ::

Author:  java [ Mon Jan 17, 2005 2:16 pm ]
Post subject: 

wtd wrote:


Well, there's your problem.

:: sigh ::


Crying or Very sad Then what am I supposed to do?

Author:  McKenzie [ Mon Jan 17, 2005 4:03 pm ]
Post subject: 

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:
code:
char hexletter[17]="0123456789ABCDEF";

use:
code:
String hexletter="0123456789ABCDEF";

There are other minor changes, but you need to know enough Java to overcome them.

Author:  Andy [ Mon Jan 17, 2005 6:53 pm ]
Post subject: 

wait.. so u cant have an character array style string in java?

Author:  rizzix [ Mon Jan 17, 2005 6:56 pm ]
Post subject: 

no u can only have a character style array in java:
code:
char[4] theChars = {'a', 'b', 'c', '\0'};

Author:  Andy [ Mon Jan 17, 2005 7:03 pm ]
Post subject: 

Oooo.. the [] has to be beside the char.. thats y it didnt work on my friends compiler.. thx rizzix

Author:  java [ Mon Jan 17, 2005 10:54 pm ]
Post subject: 

a
a

Author:  Andy [ Tue Jan 18, 2005 5:58 pm ]
Post subject: 

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

Author:  java [ Tue Jan 18, 2005 8:30 pm ]
Post subject: 

Andy wrote:
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.

Author:  Andy [ Thu Jan 20, 2005 5:16 pm ]
Post subject: 

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


: