Home Upgrade Search Memberlist Extras Hacker Tools Award Goals Help Wiki Contact

Java 

decimal to hex

Hey,

I want to write a programm which can convert decimal to hex but without the "toHexString".

I very new in programming/java so its kinda hard for me at this point.

Right now I have this (plz dont laugh :D)
PHP Code

<?php 
public static void main(String[] args) {
        
int n = Integer.parseInt(args[0]);
        
int m;
        while (
n > 0){
            
m = n%16;
            
n = n/16;
            
System.out.println(n);
            
System.out.println(m);
        } 

My Idea with this loop was the following:

for example n=22, then the loop will
22/16 = 1 mod: 6 --> 6
1/16 = 0 mod: 1 --> 1

so the right hex number would be 16. Yea I know I need to make the number >9 change to A, B, C... I could solve this with switch, I think. Can someone help me how to get it done?
Currently you have it so it only handles 1 byte (up to 0xFF). If someone puts in a number greater than 255, what do you want to happen?
(Nov 6, 2016 08:10 AM)Stealth™ Wrote:  Currently you have it so it only handles 1 byte (up to 0xFF). If someone puts in a number greater than 255, what do you want to happen?

I only want numbers till 255
(Nov 6, 2016 08:38 AM)coreqt Wrote:  I only want numbers till 255

Then you should research bit-shifts and bit-masking to help implement this.


Possibly Related Threads...
Thread / Author Replies Views Last Post
[Python] by Will PattersonSep 7, 2017 05:22 AM 2 174 Sep 7, 2017 05:22 AM
Last Post: HFDone
[Python] by billybluiceezOct 28, 2016 04:58 PM 6 208 Oct 28, 2016 04:58 PM
Last Post: billybluiceez
[C] by D_MuLaSep 18, 2016 04:38 PM 15 480 Sep 18, 2016 04:38 PM
Last Post: Dostoevsky