Convert hexadecimal string (hex) to a binary string - java

I found the following way hex to binary conversion:
String binAddr = Integer.toBinaryString(Integer.parseInt(hexAddr, 16));
While this approach works for small hex numbers, a hex number such as the following
A14AA1DBDB818F9759
Throws a NumberFormatException.
I therefore wrote the following method that seems to work:
private String hexToBin(String hex){
String bin = "";
String binFragment = "";
int iHex;
hex = hex.trim();
hex = hex.replaceFirst("0x", "");
for(int i = 0; i < hex.length(); i++){
iHex = Integer.parseInt(""+hex.charAt(i),16);
binFragment = Integer.toBinaryString(iHex);
while(binFragment.length() < 4){
binFragment = "0" + binFragment;
}
bin += binFragment;
}
return bin;
}
The above method basically takes each character in the Hex string and converts it to its binary equivalent pads it with zeros if necessary then joins it to the return value.
Is this a proper way of performing a conversion? Or am I overlooking something that may cause my approach to fail?
Thanks in advance for any assistance.

BigInteger.toString(radix) will do what you want. Just pass in a radix of 2.
static String hexToBin(String s) {
return new BigInteger(s, 16).toString(2);
}

Fast, and works for large strings:
private String hexToBin(String hex){
hex = hex.replaceAll("0", "0000");
hex = hex.replaceAll("1", "0001");
hex = hex.replaceAll("2", "0010");
hex = hex.replaceAll("3", "0011");
hex = hex.replaceAll("4", "0100");
hex = hex.replaceAll("5", "0101");
hex = hex.replaceAll("6", "0110");
hex = hex.replaceAll("7", "0111");
hex = hex.replaceAll("8", "1000");
hex = hex.replaceAll("9", "1001");
hex = hex.replaceAll("A", "1010");
hex = hex.replaceAll("B", "1011");
hex = hex.replaceAll("C", "1100");
hex = hex.replaceAll("D", "1101");
hex = hex.replaceAll("E", "1110");
hex = hex.replaceAll("F", "1111");
return hex;
}

Integer.parseInt(hex,16);
System.out.print(Integer.toBinaryString(hex));
Parse hex(String) to integer with base 16 then convert it to Binary String using toBinaryString(int) method
example
int num = (Integer.parseInt("A2B", 16));
System.out.print(Integer.toBinaryString(num));
Will Print
101000101011
Max Hex vakue Handled by int is FFFFFFF
i.e. if FFFFFFF0 is passed ti will give error

With all zeroes:
static String hexToBin(String s) {
String preBin = new BigInteger(s, 16).toString(2);
Integer length = preBin.length();
if (length < 8) {
for (int i = 0; i < 8 - length; i++) {
preBin = "0" + preBin;
}
}
return preBin;
}

public static byte[] hexToBin(String str)
{
int len = str.length();
byte[] out = new byte[len / 2];
int endIndx;
for (int i = 0; i < len; i = i + 2)
{
endIndx = i + 2;
if (endIndx > len)
endIndx = len - 1;
out[i / 2] = (byte) Integer.parseInt(str.substring(i, endIndx), 16);
}
return out;
}

import java.util.*;
public class HexadeciamlToBinary
{
public static void main()
{
Scanner sc=new Scanner(System.in);
System.out.println("enter the hexadecimal number");
String s=sc.nextLine();
String p="";
long n=0;
int c=0;
for(int i=s.length()-1;i>=0;i--)
{
if(s.charAt(i)=='A')
{
n=n+(long)(Math.pow(16,c)*10);
c++;
}
else if(s.charAt(i)=='B')
{
n=n+(long)(Math.pow(16,c)*11);
c++;
}
else if(s.charAt(i)=='C')
{
n=n+(long)(Math.pow(16,c)*12);
c++;
}
else if(s.charAt(i)=='D')
{
n=n+(long)(Math.pow(16,c)*13);
c++;
}
else if(s.charAt(i)=='E')
{
n=n+(long)(Math.pow(16,c)*14);
c++;
}
else if(s.charAt(i)=='F')
{
n=n+(long)(Math.pow(16,c)*15);
c++;
}
else
{
n=n+(long)Math.pow(16,c)*(long)s.charAt(i);
c++;
}
}
String s1="",k="";
if(n>1)
{
while(n>0)
{
if(n%2==0)
{
k=k+"0";
n=n/2;
}
else
{
k=k+"1";
n=n/2;
}
}
for(int i=0;i<k.length();i++)
{
s1=k.charAt(i)+s1;
}
System.out.println("The respective binary number is : "+s1);
}
else
{
System.out.println("The respective binary number is : "+n);
}
}
}

public static byte[] hexToBytes(String string) {
int length = string.length();
byte[] data = new byte[length / 2];
for (int i = 0; i < length; i += 2) {
data[i / 2] = (byte)((Character.digit(string.charAt(i), 16) << 4) + Character.digit(string.charAt(i + 1), 16));
}
return data;
}

Related

Java: Filling a String up to a certain point

I am working on a project but I am stuck at one point. I want the result string always to be 128 characters long. When my String is shorter, I want to replace it with "0"s. I tried many ways but none of them worked so I decided to ask it here.
This is something I have tried:
String s = "This is a String!";
if(s.length() < 128){
s.replace(s.length(), 128, "0");
System.out.println(s);
}
If you wanted your short String right-padded with zeros:
String s = "SomeTestString!";
if(s.length() < 128) {
s = String.format("%-128s", s ).replace(' ', '0');
} else {
s = s.substring(0,128);
}
System.out.println(s);
If you wanted your short String replaced with zeros:
String s = "SomeTestString!";
if(s.length() < 128) {
s = s.replace(s.length(), 128, "0");
} else {
s = s.substring(0,128) ;
}
System.out.println(s);
If you wanted short String to have 128 chars '0':
StringBuffer buf = new StringBuffer(128);
for (int i = 0; i < 128; i++) {
buf.append("0");
}
System.out.println(buf);
Yeah, at least I practiced a bit :).
String s = "This is a String!";
if(s.length() < 128){
while (s.length <= 128){
s += "0";
}
System.out.println(s);
}
Here's one way to right pad a String.
public class Snippet {
public static void main(String[] args) {
int length = 128;
String s = "This is a String!";
System.out.println(new Snippet().rightPadString(s, length, '0'));
}
public String rightPadString(String s, int length, char c) {
if (s.length() < length) {
StringBuilder builder = new StringBuilder(length);
builder.append(s);
for (int i = s.length(); i < length; i++) {
builder.append(c);
}
s = builder.toString();
} else {
s = s.substring(0, length);
}
return s;
}
}

Conversion IPv6 to long and long to IPv6

How should I perform conversion from IPv6 to long and vice versa?
So far I have:
public static long IPToLong(String addr) {
String[] addrArray = addr.split("\\.");
long num = 0;
for (int i = 0; i < addrArray.length; i++) {
int power = 3 - i;
num += ((Integer.parseInt(addrArray[i], 16) % 256 * Math.pow(256, power)));
}
return num;
}
public static String longToIP(long ip) {
return ((ip >> 24) & 0xFF) + "."
+ ((ip >> 16) & 0xFF) + "."
+ ((ip >> 8) & 0xFF) + "."
+ (ip & 0xFF);
}
Is it correct solution or I missed something?
(It would be perfect if the solution worked for both ipv4 and ipv6)
You can also use java.net.InetAddress. It works with both ipv4 and ipv6 (all formats).
The original answer has been updated to use unsigned ints per OTrains's comment.
public static BigInteger ipToBigInteger(String addr) throws UnknownHostException {
InetAddress a = InetAddress.getByName(addr);
byte[] bytes = a.getAddress();
return new BigInteger(1, bytes);
}
An IPv6 address is a 128-bit number as described here. A long in Java is represented on 64 bits, so you need another structure, like a BigDecimal or two longs (a container with an array of two longs or simply an array of two longs) in order to store an IPv6 address.
Below is an example (just to provide you an idea):
public class Asd {
public static long[] IPToLong(String addr) {
String[] addrArray = addr.split(":");//a IPv6 adress is of form 2607:f0d0:1002:0051:0000:0000:0000:0004
long[] num = new long[addrArray.length];
for (int i=0; i<addrArray.length; i++) {
num[i] = Long.parseLong(addrArray[i], 16);
}
long long1 = num[0];
for (int i=1;i<4;i++) {
long1 = (long1<<16) + num[i];
}
long long2 = num[4];
for (int i=5;i<8;i++) {
long2 = (long2<<16) + num[i];
}
long[] longs = {long2, long1};
return longs;
}
public static String longToIP(long[] ip) {
String ipString = "";
for (long crtLong : ip) {//for every long: it should be two of them
for (int i=0; i<4; i++) {//we display in total 4 parts for every long
ipString = Long.toHexString(crtLong & 0xFFFF) + ":" + ipString;
crtLong = crtLong >> 16;
}
}
return ipString;
}
static public void main(String[] args) {
String ipString = "2607:f0d0:1002:0051:0000:0000:0000:0004";
long[] asd = IPToLong(ipString);
System.out.println(longToIP(asd));
}
}
An IPv6 address can not be stored in long. You can use BigInteger instead of long.
public static BigInteger ipv6ToNumber(String addr) {
int startIndex=addr.indexOf("::");
if(startIndex!=-1){
String firstStr=addr.substring(0,startIndex);
String secondStr=addr.substring(startIndex+2, addr.length());
BigInteger first=ipv6ToNumber(firstStr);
int x=countChar(addr, ':');
first=first.shiftLeft(16*(7-x)).add(ipv6ToNumber(secondStr));
return first;
}
String[] strArr = addr.split(":");
BigInteger retValue = BigInteger.valueOf(0);
for (int i=0;i<strArr.length;i++) {
BigInteger bi=new BigInteger(strArr[i], 16);
retValue = retValue.shiftLeft(16).add(bi);
}
return retValue;
}
public static String numberToIPv6(BigInteger ipNumber) {
String ipString ="";
BigInteger a=new BigInteger("FFFF", 16);
for (int i=0; i<8; i++) {
ipString=ipNumber.and(a).toString(16)+":"+ipString;
ipNumber = ipNumber.shiftRight(16);
}
return ipString.substring(0, ipString.length()-1);
}
public static int countChar(String str, char reg){
char[] ch=str.toCharArray();
int count=0;
for(int i=0; i<ch.length; ++i){
if(ch[i]==reg){
if(ch[i+1]==reg){
++i;
continue;
}
++count;
}
}
return count;
}
Vinod's answer is right. But there are still some things that can be improved.
First, in method 'countChar', 'continue' should be replaced by 'break'.
And second, Some boundary conditions must be considered.
public static BigInteger ipv6ToNumber(String addr) {
int startIndex = addr.indexOf("::");
if (startIndex != -1) {
String firstStr = addr.substring(0, startIndex);
String secondStr = addr.substring(startIndex + 2, addr.length());
BigInteger first = new BigInteger("0");
BigInteger second = new BigInteger("0");
if (!firstStr.equals("")) {
int x = countChar(addr, ':');
first = ipv6ToNumber(firstStr).shiftLeft(16 * (7 - x));
}
if (!secondStr.equals("")) {
second = ipv6ToNumber(secondStr);
}
first = first.add(second);
return first;
}
String[] strArr = addr.split(":");
BigInteger retValue = BigInteger.valueOf(0);
for (int i = 0; i < strArr.length; i++) {
BigInteger bi = new BigInteger(strArr[i], 16);
retValue = retValue.shiftLeft(16).add(bi);
}
return retValue;
}
public static int countChar(String str, char reg){
char[] ch=str.toCharArray();
int count=0;
for(int i=0; i<ch.length; ++i){
if(ch[i]==reg){
if(ch[i+1]==reg){
++i;
break;
}
++count;
}
}
return count;
}

Java How to store/represent a String into long. Then from long to String

How would you store/represent a String into a long? Then jam it into an 8 byte array?
Things I've tried/working with
String eidString = "Awesome!";
ByteBuffer buf = ByteBuffer.allocate(8);
CharBuffer cbuf = buf.asCharBuffer();
cbuf.put(eidString);
byte[] eid = ByteBuffer.allocate(8).putLong(cbuf ??);
Attempt 2
Long l = Long.valueOf("Awesome!");
byte[] eid = ByteBuffer.allocate(8).putLong(l).array();
long p = ByteBuffer.wrap(eid).getLong();
System.out.println(p);
Attemp 3
String input = "hello long world";
byte[] bytes = input.getBytes();
LongBuffer tmpBuf = ByteBuffer.wrap(bytes).asLongBuffer();
long[] lArr = new long[tmpBuf.remaining()];
for (int i = 0; i < lArr.length; i++)
lArr[i] = tmpBuf.get();
System.out.println(input);
System.out.println(Arrays.toString(lArr));
// store longs...
// ...load longs
long[] longs = { 7522537965568945263L, 7955362964116237412L };
byte[] inputBytes = new byte[longs.length * 8];
ByteBuffer bbuf = ByteBuffer.wrap(inputBytes);
for (long l : longs)
bbuf.putLong(l);
System.out.println(new String(inputBytes));
You need to encode your string as a number and reverse it.
you have to determine the number of symbols you will need. e.g. 64 symbols need 6 bits. 32 symbols need 5 bits.
this will determine maximum length of a string. e.g. for 6 bits => 64/6 = 10 symbols, for 8 bits => 64/8 = 8 symbols. e.g. "hello long world" will not fit unless you assume not all a-z is available.
Once you have done this you can encode the symbols in the same way you would parse a 10 or 36 base number. To turn back into a String you can do the reverse (like printing a base 10 or 36 number)
What is the range of possible characters/symbols?
(you need to include a terminating symbol as the Strings can vary in length)
If you accept the limited character set of:
a,b,c,d,e,f,g,h,i,j,k,l,m,n,o,p,q,r,s,t,u,v,w,x,y,z,
A,B,C,D,E,F,G,H,I,J,K,L,M,N,O,P,Q,R,S,T,U,V,W,X,Y,Z,
0,1,2,3,4,5,6,7,8,9, , <-- space character
then you will have 63 symbols in your reduced alphabet R. Each symbol can be mapped to a 6 bit representation (64 combinations unique combination of bits). There is an implicit 64th symbol which is the empty symbol used to mark the termination of the string, which should be represented by 0x00. This leaves us with an alphabet R that maps to 6 bits as a bijection.
A long has 64 bits of information. Since 64/6 = 10, this means that we can store a string with a length of up to 10 characters from alphabet R in a Java long variable.
It is worth noting that even though R is a reduced alphabet and we have a string length limit of 10, we can still express meaningful English phrases, converted them to a long, and back again!
Java Code (64 bit mapping):
public static long stringToLong(String s) {
if(s.length() >10) { throw new IllegalArgumentException("String is too long: "+s); }
long out = 0L;
for(int i=0; i<s.length(); ++i) {
long m = reducedMapping(s.codePointAt(i));
if (m==-1) { throw new IllegalArgumentException("Unmapped Character in String: "+s); }
m <<= ((9-i)*6)+4;
out |= m;
}
return out;
}
public static String longToString(long l) {
String out = "";
long m = 0xFC00000000000000L;
for(int i=0; i<10; ++i,m>>>=6) {
int x =(int)( (l&m) >>> (((9-i)*6)+4));
if(x==0) { break; }
out += mapping[x];
}
return out;
}
public static long reducedMapping(int x) {
long out=-1;
if(x >= 97 && x <= 122) { out = (long)(x-96); } // 'a' => 1 : 0x01
else if(x >= 65 && x <= 90) { out = (long)(x-37); } // 'A' => 27 : 0x1B
else if(x >= 48 && x <= 57) { out = (long)(x-+5); } // '0' => 53 : 0x35
else if(x == 32 ) { out = 63L; } // ' ' => 63 : 0x3F
return out;
}
public static char[] mapping = {
'\n', //<-- unused/empty character
'a','b','c','d','e','f','g','h','i','j','k','l','m',
'n','o','p','q','r','s','t','u','v','w','x','y','z',
'A','B','C','D','E','F','G','H','I','J','K','L','M',
'N','O','P','Q','R','S','T','U','V','W','X','Y','Z',
'0','1','2','3','4','5','6','7','8','9',' '
};
Java Code (32 bit mapping):
public static long stringToLong32(String s) {
if(s.length() >12) { throw new IllegalArgumentException("String is too long: "+s); }
long out = 0L;
for(int i=0; i<s.length(); ++i) {
long m = reducedMapping32(s.codePointAt(i));
if (m==-1) { throw new IllegalArgumentException("Unmapped Character in String: "+s); }
m <<= ((11-i)*5)+4;
out |= m;
}
return out;
}
public static String longToString32(long l) {
String out = "";
long m = 0xF800000000000000L;
for(int i=0; i<12; ++i,m>>>=5) {
int x =(int)( (l&m) >>> (((11-i)*5)+4));
if(x==0) { break; }
out += mapping32[x];
}
return out;
}
public static long reducedMapping32(int x) {
long out=-1;
if(x >= 97 && x <= 122) { out = (long)(x-96); } // 'a' => 1 : 0x01
else if(x >= 65 && x <= 90) { out = (long)(x-64); } // 'A' => 1 : 0x01
else if(x >= 32 && x <= 34) { out = (long)(x-5); } // ' ','!','"' => 27,28,29
else if(x == 44 ) { out = 30L; } // ',' => 30 : 0x1E
else if(x == 46 ) { out = 31L; } // '.' => 31 : 0x1F
return out;
}
public static char[] mapping32 = {
'\n', //<-- unused/empty character
'a','b','c','d','e','f','g','h','i','j','k','l','m',
'n','o','p','q','r','s','t','u','v','w','x','y','z',
' ','!','"',',','.'
};
}
EDIT:
Use this class for a more generalized conversions. It allows Strings of any non empty set of characters to be uniquely mapped uniquely to a long and converted back to the original String again. Simply define any character set (char[]) through the constructor and use the resulting object to convert back and forth using str2long & long2str.
Java StringAndLongConverter Class:
import java.util.Arrays;
import java.util.regex.Pattern;
public class StringAndLongConveter {
/* .,.,.,.,.,.,.,.,.,.,.,.,.,.,.,.,., */
/* String <--> long ID Conversion */
/* `'`'`'`'`'`'`'`'`'`'`'`'`'`'`'`'`' */
/* --<[ Class Members ]>-- */
// Don't re-arrange, order-dependent initializations
private final char[] CHAR_MAP;
public final int NUM_ACTUAL_CHARS;
public final int NUM_MAPPED_CHARS;
public final int BIT_COUNT;
public final int MAX_NUM_CHARS;
public final long ROLLING_MASK;
public final long FORMAT_MASK;
public final long MIN_VALUE;
public final long MAX_VALUE;
public final Pattern REGEX_CHAR_VALIDATOR;
public StringAndLongConveter(char[] chars) {
if(chars == null ) { throw new IllegalArgumentException("Cannot Pass in null reference"); }
if(chars.length==0) { throw new IllegalArgumentException("Cannot Pass in empty set" ); }
CHAR_MAP = setCharMap(chars);
NUM_ACTUAL_CHARS = CHAR_MAP.length;
NUM_MAPPED_CHARS = NUM_ACTUAL_CHARS+1;
BIT_COUNT = calcMinBitsNeeded();
MAX_NUM_CHARS = calcMaxPossibleChars();
ROLLING_MASK = calcRollingMask();
FORMAT_MASK = calcFormatMask();
MIN_VALUE = calcIDMinVal();
MAX_VALUE = calcIDMaxVal();
REGEX_CHAR_VALIDATOR = createRegExValidator();
}
/* --<[ Dynamic Initialization Calculation Helper Methods ]>-- */
//Remove duplicates
private final char[] setCharMap(final char[] chars) {
char[] tmp = new char[chars.length];
int dupes = 0;
for(int i=0; i<chars.length; ++i) {
boolean dupeFound = false;
for(int j=0; !dupeFound && j<i; ++j) {
if(chars[i]==chars[j]) {
++dupes;
dupeFound = true;
}
}
if(!dupeFound) { tmp[i-dupes] = chars[i]; }
}
char[] out = new char[chars.length-dupes];
if(dupes==0) { out = chars; }
else {
for(int i=0; i<out.length; ++i) out[i] = tmp[i];
}
return out;
}
// calculate minimum bits necessary to encode characters uniquely
private final int calcMinBitsNeeded() {
if(NUM_MAPPED_CHARS==0) { return 0; }
int val,tmp,log;
val = NUM_MAPPED_CHARS;
tmp = Integer.highestOneBit(val); // returns only the highest set bit
tmp = tmp | (tmp-1); // propagate left bits
log = Integer.bitCount(tmp); // count bits (logarithm base 2)
return ((val&(val-1))==0) ? log-1 : log;
//return one less then bit count if even power of two
}
//Calculate maximum number of characters that can be encoded in long
private final int calcMaxPossibleChars() {
return Long.SIZE/BIT_COUNT;
}
//Calculate rolling mask for str <--> long conversion loops
private final long calcRollingMask() {
long mask = 0x0000000000000001L;
for(int i=1; i<BIT_COUNT; ++i) { mask |= mask << 1; }
for(int i=1; i<MAX_NUM_CHARS; ++i) { mask <<= BIT_COUNT; }
return mask;
}
//Calculate format mask for long input format validation
private final long calcFormatMask() {
//propagate lest significant set bit in rolling mask & negate resulting value
return ~(ROLLING_MASK | (ROLLING_MASK-1));
}
//Calculate min value of long encoding
//doubles as format specification for unused bits
private final long calcIDMinVal() {
return 0xAAAAAAAAAAAAAAAAL & FORMAT_MASK;
}
//Calculate max value of long encoding
private final long calcIDMaxVal(){
char maxChar = CHAR_MAP[CHAR_MAP.length-1];
char[] maxCharArr = new char[MAX_NUM_CHARS];
Arrays.fill(maxCharArr, maxChar);
return str2long(new String(maxCharArr));
}
//Dynamically create RegEx validation string for invalid characters
private final Pattern createRegExValidator() {
return Pattern.compile("^["+Pattern.quote(new String(CHAR_MAP))+"]+?$");
}
/* --<[ Internal Helper Methods ]>-- */
private static boolean ulongLessThen(long lh, long rh) {
return (((lh ^ rh) >> 63) == 0) ? lh < rh : (0x8000000000000000L & lh)==0;
}
private long charMapping(final char c) {
for(int i=0; i<CHAR_MAP.length; ++i)
if(CHAR_MAP[i]==c)
return i+1;
return -1;
}
/* --<[ String <--> long Conversion Methods ]>-- */
public final String long2str(final long n) {
String out = "";
if (ulongLessThen(n,MIN_VALUE) || ulongLessThen(MAX_VALUE,n)) { throw new IllegalArgumentException("Long Outside of Formatted Range: "+Long.toHexString(n)); }
if ((FORMAT_MASK & n) != MIN_VALUE) { throw new IllegalArgumentException("Improperly Formatted long"); }
long m = ROLLING_MASK;
for(int i=0; i<MAX_NUM_CHARS; ++i,m>>>=BIT_COUNT) {
int x =(int)( (n&m) >>> ((MAX_NUM_CHARS-i-1)*BIT_COUNT));//10|10 0111
if(x >= NUM_MAPPED_CHARS) { throw new IllegalArgumentException("Invalid Formatted bit mapping: \nlong="+Long.toHexString(n)+"\n masked="+Long.toHexString(n&m)+"\n i="+i+" x="+x); }
if(x==0) { break; }
out += CHAR_MAP[x-1];
}
return out;
}
public final long str2long(String str) {
if(str.length() > MAX_NUM_CHARS) { throw new IllegalArgumentException("String is too long: "+str); }
long out = MIN_VALUE;
for(int i=0; i<str.length(); ++i) {
long m = charMapping(str.charAt(i));
if (m==-1) { throw new IllegalArgumentException("Unmapped Character in String: "+str); }
m <<= ((MAX_NUM_CHARS-i-1)*BIT_COUNT);
out += m; // += is more destructive then |= allowing errors to be more readily detected
}
return out;
}
public final boolean isValidString(String str) {
return str != null && !str.equals("") //null or empty String
&& str.length() <= MAX_NUM_CHARS //too long
&& REGEX_CHAR_VALIDATOR.matcher(str).matches(); //only valid chars in string
}
public final char[] getMappedChars() { return Arrays.copyOf(CHAR_MAP,CHAR_MAP.length); }
}
Have fun encoding & decoding Strings & longs
To parse a String into a Long, can use the Long wrapper class.
String myString = "1500000";
Long myLong = Long.parseLong(myString);
To stuff it into an 8-byte array...
long value = myLong.longValue();
byte[] bytes = new byte[8];
for (int i = 0; i < bytes.length; i++) {
long mask = 0xFF00000000000000 >> (i * 8);
bytes[i] = (byte) (value & mask);
}
This example is big endian.
If you're encoding a String into a long, then you can do something like:
String myString = "HELLO";
long toLong = 0;
for (int i = 0; i < myString.length(); i++) {
long c = (long) myString.charAt(i);
int shift = (myString.length() - 1 - i) * 8;
toLong += c << shift;
}
This hasn't been tested. There might be a few things wrong with it.
You don't need to do that. String has a method called getBytes(). It do the convert for you directly. Call the following method with parameter "Hallelujah"
public static void strToLong(String s) throws IOException
{
byte[] bArr = s.getBytes();
for( byte b : bArr)
{
System.out.print(" " + b);
}
System.out.println();
System.out.write(bArr);
}
The result is
72 97 108 108 101 108 117 106 97 104
Hallelujah

In Java how do you convert a decimal number to base 36?

If I have a decimal number, how do I convert it to base 36 in Java?
Given a number i, use Integer.toString(i, 36).
See the documentation for Integer.toString
http://docs.oracle.com/javase/7/docs/api/java/lang/Integer.html#toString(int,%20int)
toString
public static String toString(int i, int radix)
....
The following ASCII characters are used as digits:
0123456789abcdefghijklmnopqrstuvwxyz
What is radix? You're in luck for Base 36 (and it makes sense)
http://docs.oracle.com/javase/7/docs/api/java/lang/Character.html#MAX_RADIX
public static final int MAX_RADIX 36
The following can work for any base, not just 36. Simply replace the String contents of code.
Encode:
int num = 586403532;
String code = "0123456789abcdefghijklmnopqrstuvwxyz";
String text = "";
int j = (int)Math.ceil(Math.log(num)/Math.log(code.length()));
for(int i = 0; i < j; i++){
//i goes to log base code.length() of num (using change of base formula)
text += code.charAt(num%code.length());
num /= code.length();
}
Decode:
String text = "0vn4p9";
String code = "0123456789abcdefghijklmnopqrstuvwxyz";
int num = 0;
int j = text.length();
for(int i = 0; i < j; i++){
num += code.indexOf(text.charAt(0))*Math.pow(code.length(), i);
text = text.substring(1);
}
First you have to convert your number it into the internal number format of Java (which happens to be 2-based, but this does not really matter here), for example by Integer.parseInt() (if your number is an integer less than 2^31). Then you can convert it from int to the desired output format. The method Integer.toString(i, 36) does this by using 0123456789abcdefghijklmnopqrstuvwxyz as digits (the decimal digits 0-9 and lower case english letters in alphabetic order). If you want some other digits, you can either convert the result by replacing the "digits" (for example toUpperCase), or do the conversion yourself - it is no magic, simply a loop of taking the remainder modulo 36 and dividing by 36 (with a lookup of the right digit).
If your number is longer than what int offers you may want to use long (with Long) or BigInteger instead, they have similar radix-converters.
If your number has "digits after the point", it is a bit more difficult, as most (finite) base-X-numbers are not exactly representable as (finite) base-Y-numbers if (a power of) Y is not a multiple of X.
This code works:
public class Convert {
public static void main(String[] args) {
int num= 2147483647;
String text="ABCD1";
System.out.println("num: " + num + "=>" + base10ToBase36(num));
System.out.println("text: " +text + "=>" + base36ToBase10(text));
}
private static String codeBase36 = "0123456789abcdefghijklmnopqrstuvwxyz";
//"0123456789 abcdefghij klmnopqrst uvwxyz"
//"0123456789 0123456789 0123456789 012345"
private static String max36=base10ToBase36(Integer.MAX_VALUE);
public static String base10ToBase36(int inNum) {
if(inNum<0) {
throw new NumberFormatException("Value "+inNum +" to small");
}
int num = inNum;
String text = "";
int j = (int)Math.ceil(Math.log(num)/Math.log(codeBase36.length()));
for(int i = 0; i < j; i++){
text = codeBase36.charAt(num%codeBase36.length())+text;
num /= codeBase36.length();
}
return text;
}
public static int base36ToBase10(String in) {
String text = in.toLowerCase();
if(text.compareToIgnoreCase(max36)>0) {
throw new NumberFormatException("Value "+text+" to big");
}
if(!text.replaceAll("(\\W)","").equalsIgnoreCase(text)){
throw new NumberFormatException("Value "+text+" false format");
}
int num=0;
int j = text.length();
for(int i = 0; i < j; i++){
num += codeBase36.indexOf(text.charAt(text.length()-1))*Math.pow(codeBase36.length(), i);
text = text.substring(0,text.length()-1);
}
return num;
}
}
If you dont want to use Integer.toString(Num , base) , for instance, in my case which I needed a 64 bit long variable, you can use the following code:
Using Lists in JAVA facilitates this conversion
long toBeConverted=10000; // example, Initialized by 10000
List<Character> charArray = new ArrayList<Character>();
List<Character> charArrayFinal = new ArrayList<Character>();
int length=10; //Length of the output string
long base = 36;
while(toBeConverted!=0)
{
long rem = toBeConverted%base;
long quotient = toBeConverted/base;
if(rem<10)
rem+=48;
else
rem+=55;
charArray.add((char)rem);
toBeConverted=quotient;
}
// make the array in the reverse order
for(int i=length-1;i>=0;--i){
if(i>=charArray.size()){
charArrayFinal.add((char) 48); // sends 0 to fix the length of the output List
} else {
charArrayFinal.add(charArray.get(i));
}
}
Example:
(278197)36=5YNP
Maybe I'm late to the party, but this is the solution I was using for getting Calc/Excel cell names by their index:
public static void main(final String[] args) {
final String base = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
System.out.println(toCustomBase(0, base));
System.out.println(toCustomBase(2, base));
System.out.println(toCustomBase(25, base));
System.out.println(toCustomBase(26, base));
System.out.println(toCustomBase(51, base));
System.out.println(toCustomBase(52, base));
System.out.println(toCustomBase(520, base));
}
public static String toCustomBase(final int num, final String base) {
final int baseSize = base.length();
if(num < baseSize) {
return String.valueOf(base.charAt(num));
}
else {
return toCustomBase(num / baseSize - 1, base) + base.charAt(num % baseSize);
}
}
Results:
A
C
Z
AA
AZ
BA
TA
Basically the solution accepts any custom radix. The idea was commandeered from here.
Not sure if the above answers did help but noting 'decimal' and 'to base36' I assume you want to convert a numeric value to base36. And as long as the Long value of the raw figure is within (0 - Long.MAX_VALUE):
String someNumericString = "9223372036854";
Long l = Long.valueOf(someNumericString);
String bases36 = Long.toString(l, 36);
System.out.println("base36 value: "+bases36);
output: 39p5pkj5i
Here is a method to convert base 10 to any given base.
public char[] base10Converter(int number, int finalBase) {
int quo;
int rem;
char[] res = new char[1];
do {
rem = number % finalBase;
quo = number / finalBase;
res = Arrays.copyOf(res, res.length + 1);
if (rem < 10) {
//Converting ints using ASCII values
rem += 48;
res[res.length - 1] = (char) rem;
} else {
//Convert int > 9 to A, B, C..
rem += 55;
res[res.length - 1] = (char) rem;
}
number /= finalBase;
} while (quo != 0);
//Reverse array
char[] temp = new char[res.length];
for (int i = res.length - 1, j = 0; i > 0; i--) {
temp[j++] = res[i];
}
return temp;
}
I got this code from this website in JavaScript, and this is my version in java:
public static String customBase (int N, String base) {
int radix = base.length();
String returns = "";
int Q = (int) Math.floor(Math.abs(N));
int R = 0;
while (Q != 0) {
R = Q % radix;
returns = base.charAt(R) + returns;
Q /= radix;
}
if(N == 0) {
return String.valueOf(base.toCharArray()[0]);
}
return N < 0 ? "-" + returns : returns;
}
This supports negative numbers and custom bases.
Decimal Addon:
public static String customBase (double N, String base) {
String num = (String.valueOf(N));
String[] split = num.split("\\.");
if(split[0] == "" || split[1] == "") {
return "";
}
return customBase(Integer.parseInt(split[0]), base)+ "." + customBase(Integer.parseInt(split[1]), base);
}
This can be helpful to you.The operation being performed on the 4 digit alphanumeric String and decimal number below 1679615. You can Modify code accordingly.
char[] alpaNum = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ".toCharArray();
String currentSeries = "";
int num = 481261;
String result = "";
String baseConversionStr = "";
boolean flag = true;
do
{
baseConversionStr = Integer.toString(num % 36) + baseConversionStr;
String position = "";
if(flag)
{
flag = false;
position = baseConversionStr;
}
else
{
position = Integer.toString(num % 36);
}
result += alpaNum[new Integer(position)];
num = num/36;
}
while (num > 0);
StringBuffer number = new StringBuffer(result).reverse();
String finalString = "";
if(number.length()==1)
{
finalString = "000"+articleNo;
}
else if(number.length()==2)
{
finalString = "00"+articleNo;
}
else if(number.length()==3)
{
finalString = "0"+articleNo;
}
currentSeries = finalString;

Converting an int to a binary string representation in Java?

What would be the best way (ideally, simplest) to convert an int to a binary string representation in Java?
For example, say the int is 156. The binary string representation of this would be "10011100".
Integer.toBinaryString(int i)
There is also the java.lang.Integer.toString(int i, int base) method, which would be more appropriate if your code might one day handle bases other than 2 (binary). Keep in mind that this method only gives you an unsigned representation of the integer i, and if it is negative, it will tack on a negative sign at the front. It won't use two's complement.
public static string intToBinary(int n)
{
String s = "";
while (n > 0)
{
s = ( (n % 2 ) == 0 ? "0" : "1") +s;
n = n / 2;
}
return s;
}
One more way- By using java.lang.Integer you can get string representation of the first argument i in the radix (Octal - 8, Hex - 16, Binary - 2) specified by the second argument.
Integer.toString(i, radix)
Example_
private void getStrtingRadix() {
// TODO Auto-generated method stub
/* returns the string representation of the
unsigned integer in concern radix*/
System.out.println("Binary eqivalent of 100 = " + Integer.toString(100, 2));
System.out.println("Octal eqivalent of 100 = " + Integer.toString(100, 8));
System.out.println("Decimal eqivalent of 100 = " + Integer.toString(100, 10));
System.out.println("Hexadecimal eqivalent of 100 = " + Integer.toString(100, 16));
}
OutPut_
Binary eqivalent of 100 = 1100100
Octal eqivalent of 100 = 144
Decimal eqivalent of 100 = 100
Hexadecimal eqivalent of 100 = 64
public class Main {
public static String toBinary(int n, int l ) throws Exception {
double pow = Math.pow(2, l);
StringBuilder binary = new StringBuilder();
if ( pow < n ) {
throw new Exception("The length must be big from number ");
}
int shift = l- 1;
for (; shift >= 0 ; shift--) {
int bit = (n >> shift) & 1;
if (bit == 1) {
binary.append("1");
} else {
binary.append("0");
}
}
return binary.toString();
}
public static void main(String[] args) throws Exception {
System.out.println(" binary = " + toBinary(7, 4));
System.out.println(" binary = " + Integer.toString(7,2));
}
}
This is something I wrote a few minutes ago just messing around. Hope it helps!
public class Main {
public static void main(String[] args) {
ArrayList<Integer> powers = new ArrayList<Integer>();
ArrayList<Integer> binaryStore = new ArrayList<Integer>();
powers.add(128);
powers.add(64);
powers.add(32);
powers.add(16);
powers.add(8);
powers.add(4);
powers.add(2);
powers.add(1);
Scanner sc = new Scanner(System.in);
System.out.println("Welcome to Paden9000 binary converter. Please enter an integer you wish to convert: ");
int input = sc.nextInt();
int printableInput = input;
for (int i : powers) {
if (input < i) {
binaryStore.add(0);
} else {
input = input - i;
binaryStore.add(1);
}
}
String newString= binaryStore.toString();
String finalOutput = newString.replace("[", "")
.replace(" ", "")
.replace("]", "")
.replace(",", "");
System.out.println("Integer value: " + printableInput + "\nBinary value: " + finalOutput);
sc.close();
}
}
Convert Integer to Binary:
import java.util.Scanner;
public class IntegerToBinary {
public static void main(String[] args) {
Scanner input = new Scanner( System.in );
System.out.println("Enter Integer: ");
String integerString =input.nextLine();
System.out.println("Binary Number: "+Integer.toBinaryString(Integer.parseInt(integerString)));
}
}
Output:
Enter Integer:
10
Binary Number: 1010
The simplest approach is to check whether or not the number is odd. If it is, by definition, its right-most binary number will be "1" (2^0). After we've determined this, we bit shift the number to the right and check the same value using recursion.
#Test
public void shouldPrintBinary() {
StringBuilder sb = new StringBuilder();
convert(1234, sb);
}
private void convert(int n, StringBuilder sb) {
if (n > 0) {
sb.append(n % 2);
convert(n >> 1, sb);
} else {
System.out.println(sb.reverse().toString());
}
}
Using built-in function:
String binaryNum = Integer.toBinaryString(int num);
If you don't want to use the built-in function for converting int to binary then you can also do this:
import java.util.*;
public class IntToBinary {
public static void main(String[] args) {
Scanner d = new Scanner(System.in);
int n;
n = d.nextInt();
StringBuilder sb = new StringBuilder();
while(n > 0){
int r = n%2;
sb.append(r);
n = n/2;
}
System.out.println(sb.reverse());
}
}
here is my methods, it is a little bit convince that number of bytes fixed
private void printByte(int value) {
String currentBinary = Integer.toBinaryString(256 + value);
System.out.println(currentBinary.substring(currentBinary.length() - 8));
}
public int binaryToInteger(String binary) {
char[] numbers = binary.toCharArray();
int result = 0;
for(int i=numbers.length - 1; i>=0; i--)
if(numbers[i]=='1')
result += Math.pow(2, (numbers.length-i - 1));
return result;
}
Using bit shift is a little quicker...
public static String convertDecimalToBinary(int N) {
StringBuilder binary = new StringBuilder(32);
while (N > 0 ) {
binary.append( N % 2 );
N >>= 1;
}
return binary.reverse().toString();
}
if the int value is 15, you can convert it to a binary as follows.
int x = 15;
Integer.toBinaryString(x);
if you have the binary value, you can convert it into int value as follows.
String binaryValue = "1010";
Integer.parseInt(binaryValue, 2);
This can be expressed in pseudocode as:
while(n > 0):
remainder = n%2;
n = n/2;
Insert remainder to front of a list or push onto a stack
Print list or stack
You should really use Integer.toBinaryString() (as shown above), but if for some reason you want your own:
// Like Integer.toBinaryString, but always returns 32 chars
public static String asBitString(int value) {
final char[] buf = new char[32];
for (int i = 31; i >= 0; i--) {
buf[31 - i] = ((1 << i) & value) == 0 ? '0' : '1';
}
return new String(buf);
}
My 2cents:
public class Integer2Binary {
public static void main(String[] args) {
final int integer12 = 12;
System.out.println(integer12 + " -> " + integer2Binary(integer12));
// 12 -> 00000000000000000000000000001100
}
private static String integer2Binary(int n) {
return new StringBuilder(Integer.toBinaryString(n))
.insert(0, "0".repeat(Integer.numberOfLeadingZeros(n)))
.toString();
}
}
This should be quite simple with something like this :
public static String toBinary(int number){
StringBuilder sb = new StringBuilder();
if(number == 0)
return "0";
while(number>=1){
sb.append(number%2);
number = number / 2;
}
return sb.reverse().toString();
}
public class BinaryConverter {
public static String binaryConverter(int number) {
String binary = "";
if (number == 1){
binary = "1";
return binary;
}
if (number == 0){
binary = "0";
return binary;
}
if (number > 1) {
String i = Integer.toString(number % 2);
binary = binary + i;
binaryConverter(number/2);
}
return binary;
}
}
In order to make it exactly 8 bit, I made a slight addition to #sandeep-saini 's answer:
public static String intToBinary(int number){
StringBuilder sb = new StringBuilder();
if(number == 0)
return "0";
while(number>=1){
sb.append(number%2);
number = number / 2;
}
while (sb.length() < 8){
sb.append("0");
}
return sb.reverse().toString();
}
So now for an input of 1 you get an output of 00000001
public static String intToBinaryString(int n) {
StringBuilder sb = new StringBuilder();
for (int i = 0; i < 32 && n != 0; i++) {
sb.append((n&1) == 1 ? "1" : "0");
n >>= 1;
}
return sb.reverse().toString();
}
We cannot use n%2 to check the first bit, because it's not right for negtive integer. We should use n&1.

Categories