Jump to content

Welcome to Pure Warfare - The #1 Community for Pures

Welcome to Pure Warfare - The #1 Community for Pures, like most online communities you must register to view or post in our community, but don't worry this is a simple free process that requires minimal information for you to signup. Be apart of Pure Warfare - The #1 Community for Pures by signing in or creating an account.
  • Start new topics and reply to others
  • Subscribe to topics and forums to get email updates
  • Get your own profile page and make new friends
  • Send personal messages to other members.

[Java] Binary Conversion


Jay

Recommended Posts

I made two functions - one to convert binary to decimal, and one to convert decimal to binary - out of boredom and as practice.

 

 

    private static int decodeBinary(String Bin) {
        int finalvalue = 0;
        for (int i=0;i<Bin.toString().length();i++) {
        finalvalue += (Math.pow(2,(Bin.toString().length()-i-1))) * (Character.getNumericValue(Bin.toString().charAt(i)));
        } return finalvalue;
    }
    private static String decToBin(Integer Dec) {
        int highest = 0;
        String retBin = "";
        Integer Total = Dec;
        for (int i=0;Math.pow(2,i) <= Dec;i++) { highest=i; }
        for (int i=highest;i>=0;i--) {
            if (Math.pow(2,i) > Total) { retBin = retBin + "0";
            } else {
                Total = (int) (Total - (Math.pow(2, i)));
                retBin = retBin + "1"; } }
        return retBin;
    }

 

decodeBinary("10100111001110") returns 10702

decToBin(10702) returns 10100111001110

emmawatsonjay.png

34rfuk5.png

(+`Obsession) jay is a legend

Link to comment
Share on other sites

  • 4 weeks later...
I'd probably comment on how awesome or cool this is, but I honestly have no idea what I'm looking at...

 

=[

 

 

my brain hurts.

 

 

my brain hurts.

 

 

one day ill know what this is.

 

^^

Proud @๖ۣۜHigh Council of the Most Hated Pure clan.
Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
  • Create New...