Groovy web console

subscribe to the feed Subscribe
to this
site

toBinary

Published 2 months ago by Ram with tags binary
Actions  ➤ Edit in console Back to console Show/hide line numbers View recent scripts
import java.lang.StringBuilder;

def str = "Rama"
byte[] bytes = str.getByte()

//def val = Integer.toBinaryString(bytes)
//def val = toBinaryString(bytes)

def String toBinaryString(byte n) {      
StringBuilder sb = new StringBuilder("00000000");      
    for (int bit = 0; bit < 8; bit++) {          
        if (((n >> bit) & 1) > 0) {              
            sb.setCharAt(7 - bit, '1');          
        }      
    }      
    return sb.toString();  
}