Hack Forums
[Java] decimal to hex - Printable Version

+- Hack Forums (https://hackforums.net)
+-- Forum: Tech (https://hackforums.net/forumdisplay.php?fid=444)
+--- Forum: Coding (https://hackforums.net/forumdisplay.php?fid=151)
+---- Forum: Software Development (https://hackforums.net/forumdisplay.php?fid=118)
+---- Thread: [Java] decimal to hex (/showthread.php?tid=5455699)



decimal to hex - coreqt - Nov 6, 2016

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?


* - Stealthâ„¢ - Nov 6, 2016

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?


* - coreqt - Nov 6, 2016

(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


* - Stealthâ„¢ - Nov 6, 2016

(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.