Decimal to Hexadecimal Converter in Java - java

I have a homework assignment where I need to do three-way conversion between decimal, binary and hexadecimal. The function I need help with is converting a decimal into a hexadecimal. I have nearly no understanding of hexadecimal, nonetheless how to convert a decimal into hex. I need a function that takes in an int dec and returns a String hex. Unfortunately I don't have any draft of this function, I'm completely lost. All I have is this.
public static String decToHex(int dec)
{
String hex = "";
return hex;
}
Also I can't use those premade functions like Integer.toHexString() or anything, I need to actually make the algorithm or I wouldn't have learned anything.

One possible solution:
import java.lang.StringBuilder;
class Test {
private static final int sizeOfIntInHalfBytes = 8;
private static final int numberOfBitsInAHalfByte = 4;
private static final int halfByte = 0x0F;
private static final char[] hexDigits = {
'0', '1', '2', '3', '4', '5', '6', '7',
'8', '9', 'A', 'B', 'C', 'D', 'E', 'F'
};
public static String decToHex(int dec) {
StringBuilder hexBuilder = new StringBuilder(sizeOfIntInHalfBytes);
hexBuilder.setLength(sizeOfIntInHalfBytes);
for (int i = sizeOfIntInHalfBytes - 1; i >= 0; --i)
{
int j = dec & halfByte;
hexBuilder.setCharAt(i, hexDigits[j]);
dec >>= numberOfBitsInAHalfByte;
}
return hexBuilder.toString();
}
public static void main(String[] args) {
int dec = 305445566;
String hex = decToHex(dec);
System.out.println(hex);
}
}
Output:
1234BABE
Anyway, there is a library method for this:
String hex = Integer.toHexString(dec);

Simple:
public static String decToHex(int dec)
{
return Integer.toHexString(dec);
}
As mentioned here: Java Convert integer to hex integer

I need a function that takes in an int dec and returns a String hex.
I found a more elegant solution from
http://introcs.cs.princeton.edu/java/31datatype/Hex2Decimal.java.html . I changed a bit from the original ( see the edit )
// precondition: d is a nonnegative integer
public static String decimal2hex(int d) {
String digits = "0123456789ABCDEF";
if (d <= 0) return "0";
int base = 16; // flexible to change in any base under 16
String hex = "";
while (d > 0) {
int digit = d % base; // rightmost digit
hex = digits.charAt(digit) + hex; // string concatenation
d = d / base;
}
return hex;
}
Disclaimer: I ask this question in my coding interview. I hope this solution doesn't get too popular :)
Edit June 17 2016 : I added the base variable to give the flexibility to change into any base : binary, octal, base of 7 ...
According to the comments, this solution is the most elegant so I removed the implementation of Integer.toHexString() .
Edit September 4 2015 : I found a more elegant solution http://introcs.cs.princeton.edu/java/31datatype/Hex2Decimal.java.html

Consider dec2m method below for conversion from dec to hex, oct or bin.
Sample output is
28 dec == 11100 bin
28 dec == 34 oct
28 dec == 1C hex
public class Conversion {
public static void main(String[] argv) {
int x = 28; // sample number
if (argv.length > 0)
x = Integer.parseInt(argv[0]); // number from command line
System.out.printf("%d dec == %s bin\n", i, dec2m(x, 2));
System.out.printf("%d dec == %s oct\n", i, dec2m(x, 8));
System.out.printf("%d dec == %s hex\n", i, dec2m(x, 16));
}
static String dec2m(int N, int m) {
String s = "";
for (int n = N; n > 0; n /= m) {
int r = n % m;
s = r < 10 ? r + s : (char) ('A' - 10 + r) + s;
}
return s;
}
}

Here is the code for any number :
import java.math.BigInteger;
public class Testing {
/**
* #param args
*/
static String arr[] ={"0","1","2","3","4","5","6","7","8","9","A","B","C","D","E","F"};
public static void main(String[] args) {
String value = "214";
System.out.println(value + " : " + getHex(value));
}
public static String getHex(String value) {
String output= "";
try {
Integer.parseInt(value);
Integer number = new Integer(value);
while(number >= 16){
output = arr[number%16] + output;
number = number/16;
}
output = arr[number]+output;
} catch (Exception e) {
BigInteger number = null;
try{
number = new BigInteger(value);
}catch (Exception e1) {
return "Not a valid numebr";
}
BigInteger hex = new BigInteger("16");
BigInteger[] val = {};
while(number.compareTo(hex) == 1 || number.compareTo(hex) == 0){
val = number.divideAndRemainder(hex);
output = arr[val[1].intValue()] + output;
number = val[0];
}
output = arr[number.intValue()] + output;
}
return output;
}
}

Another possible solution:
public String DecToHex(int dec){
char[] hexDigits = {'0', '1', '2', '3', '4', '5', '6', '7', '8', '9',
'A', 'B', 'C', 'D', 'E', 'F'};
String hex = "";
while (dec != 0) {
int rem = dec % 16;
hex = hexDigits[rem] + hex;
dec = dec / 16;
}
return hex;
}

The easiest way to do this is:
String hexadecimalString = String.format("%x", integerValue);

I will use
Long a = Long.parseLong(cadenaFinal, 16 );
since there is some hex that can be larguer than intenger and it will throw an exception

Code to convert DECIMAL -to-> BINARY, OCTAL, HEXADECIMAL
public class ConvertBase10ToBaseX {
enum Base {
/**
* Integer is represented in 32 bit in 32/64 bit machine.
* There we can split this integer no of bits into multiples of 1,2,4,8,16 bits
*/
BASE2(1,1,32), BASE4(3,2,16), BASE8(7,3,11)/* OCTAL*/, /*BASE10(3,2),*/
BASE16(15, 4, 8){
public String getFormattedValue(int val){
switch(val) {
case 10:
return "A";
case 11:
return "B";
case 12:
return "C";
case 13:
return "D";
case 14:
return "E";
case 15:
return "F";
default:
return "" + val;
}
}
}, /*BASE32(31,5,1),*/ BASE256(255, 8, 4), /*BASE512(511,9),*/ Base65536(65535, 16, 2);
private int LEVEL_0_MASK;
private int LEVEL_1_ROTATION;
private int MAX_ROTATION;
Base(int levelZeroMask, int levelOneRotation, int maxPossibleRotation) {
this.LEVEL_0_MASK = levelZeroMask;
this.LEVEL_1_ROTATION = levelOneRotation;
this.MAX_ROTATION = maxPossibleRotation;
}
int getLevelZeroMask(){
return LEVEL_0_MASK;
}
int getLevelOneRotation(){
return LEVEL_1_ROTATION;
}
int getMaxRotation(){
return MAX_ROTATION;
}
String getFormattedValue(int val){
return "" + val;
}
}
public void getBaseXValueOn(Base base, int on) {
forwardPrint(base, on);
}
private void forwardPrint(Base base, int on) {
int rotation = base.getLevelOneRotation();
int mask = base.getLevelZeroMask();
int maxRotation = base.getMaxRotation();
boolean valueFound = false;
for(int level = maxRotation; level >= 2; level--) {
int rotation1 = (level-1) * rotation;
int mask1 = mask << rotation1 ;
if((on & mask1) > 0 ) {
valueFound = true;
}
if(valueFound)
System.out.print(base.getFormattedValue((on & mask1) >>> rotation1));
}
System.out.println(base.getFormattedValue((on & mask)));
}
public int getBaseXValueOnAtLevel(Base base, int on, int level) {
if(level > base.getMaxRotation() || level < 1) {
return 0; //INVALID Input
}
int rotation = base.getLevelOneRotation();
int mask = base.getLevelZeroMask();
if(level > 1) {
rotation = (level-1) * rotation;
mask = mask << rotation;
} else {
rotation = 0;
}
return (on & mask) >>> rotation;
}
public static void main(String[] args) {
ConvertBase10ToBaseX obj = new ConvertBase10ToBaseX();
obj.getBaseXValueOn(Base.BASE16,12456);
// obj.getBaseXValueOn(Base.BASE16,300);
// obj.getBaseXValueOn(Base.BASE16,7);
// obj.getBaseXValueOn(Base.BASE16,7);
obj.getBaseXValueOn(Base.BASE2,12456);
obj.getBaseXValueOn(Base.BASE8,12456);
obj.getBaseXValueOn(Base.BASE2,8);
obj.getBaseXValueOn(Base.BASE2,9);
obj.getBaseXValueOn(Base.BASE2,10);
obj.getBaseXValueOn(Base.BASE2,11);
obj.getBaseXValueOn(Base.BASE2,12);
obj.getBaseXValueOn(Base.BASE2,13);
obj.getBaseXValueOn(Base.BASE2,14);
obj.getBaseXValueOn(Base.BASE2,15);
obj.getBaseXValueOn(Base.BASE2,16);
obj.getBaseXValueOn(Base.BASE2,17);
System.out.println(obj.getBaseXValueOnAtLevel(Base.BASE2, 4, 1));
System.out.println(obj.getBaseXValueOnAtLevel(Base.BASE2, 4, 2));
System.out.println(obj.getBaseXValueOnAtLevel(Base.BASE2, 4, 3));
System.out.println(obj.getBaseXValueOnAtLevel(Base.BASE2, 4, 4));
System.out.println(obj.getBaseXValueOnAtLevel(Base.BASE16,15, 1));
System.out.println(obj.getBaseXValueOnAtLevel(Base.BASE16,30, 2));
System.out.println(obj.getBaseXValueOnAtLevel(Base.BASE16,7, 1));
System.out.println(obj.getBaseXValueOnAtLevel(Base.BASE16,7, 2));
System.out.println(obj.getBaseXValueOnAtLevel(Base.BASE256, 511, 1));
System.out.println(obj.getBaseXValueOnAtLevel(Base.BASE256, 511, 2));
System.out.println(obj.getBaseXValueOnAtLevel(Base.BASE256, 512, 1));
System.out.println(obj.getBaseXValueOnAtLevel(Base.BASE256, 512, 2));
System.out.println(obj.getBaseXValueOnAtLevel(Base.BASE256, 513, 2));
}
}

Here's mine
public static String dec2Hex(int num)
{
String hex = "";
while (num != 0)
{
if (num % 16 < 10)
hex = Integer.toString(num % 16) + hex;
else
hex = (char)((num % 16)+55) + hex;
num = num / 16;
}
return hex;
}

A better solution to convert Decimal To HexaDecimal and this one is less complex
import java.util.Scanner;
public class DecimalToHexa
{
public static void main(String ar[])
{
Scanner sc=new Scanner(System.in);
System.out.println("Enter a Decimal number: ");
int n=sc.nextInt();
if(n<0)
{
System.out.println("Enter a positive integer");
return;
}
int i=0,d=0;
String hx="",h="";
while(n>0)
{
d=n%16;`enter code here`
n/=16;
if(d==10)h="A";
else if(d==11)h="B";
else if(d==12)h="C";
else if(d==13)h="D";
else if(d==14)h="E";
else if(d==15)h="F";
else h=""+d;
hx=""+h+hx;
}
System.out.println("Equivalent HEXA: "+hx);
}
}

The following converts decimal to Hexa Decimal with
Time Complexity : O(n) Linear Time with out any java inbuilt function
private static String decimalToHexaDecimal(int N) {
char hexaDecimals[] = { '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'A', 'B', 'C', 'D', 'E', 'F' };
StringBuilder builder = new StringBuilder();
int base= 16;
while (N != 0) {
int reminder = N % base;
builder.append(hexaDecimals[reminder]);
N = N / base;
}
return builder.reverse().toString();
}

Related

Is there a better way to store and retrieve the values of letters given by their position in the alphabet? [duplicate]

This question already has answers here:
How to map character to numeric position in java?
(7 answers)
Closed 2 years ago.
I've got an assignment to write a program that adds the digits of an integer and the letters in a word (the value of each letter is given by its position in the alphabet, e.g. j = 10). After a while of trying to figure out a way to store and retrieve the value of each letter I decided to use a HashMap... but I wonder if there are better ways to do it (I'm sure there are)... so I'd like to get some suggestions on how to improve the code and learn some new things... thanks!
import java.util.HashMap;
import java.util.Scanner;
public class NLS {
public static HashMap<Character, Integer> alphabet;
static {
alphabet = new HashMap<>();
alphabet.put('a', 1); alphabet.put('b', 2); alphabet.put('c', 3); alphabet.put('d', 4);
alphabet.put('e', 5); alphabet.put('f', 6); alphabet.put('g', 7); alphabet.put('h', 8);
alphabet.put('i', 9); alphabet.put('j', 10); alphabet.put('k', 11); alphabet.put('l', 12);
alphabet.put('m', 13); alphabet.put('n', 14); alphabet.put('ñ', 15); alphabet.put('o', 16);
alphabet.put('p', 17); alphabet.put('q', 18); alphabet.put('r', 19); alphabet.put('s', 20);
alphabet.put('t', 21); alphabet.put('u', 22); alphabet.put('v', 23); alphabet.put('w', 24);
alphabet.put('x', 25); alphabet.put('y', 26); alphabet.put('z', 27);
}
public static void main(String[] args) {
char keepGoing;
do {
int sum;
Scanner scan = new Scanner(System.in);
System.out.println("\n\n\n____________________________________________________________");
System.out.println("\n\tEnter a number [integer] or a word/sentence [no spaces]:\n");
String input = scan.nextLine();
boolean numeric = numberOrWord(input);
if (numeric) {
sum = digitSum(Integer.parseInt(input));
System.out.println("Result= " + sum);
} else {
char letter = calculateLetter(input);
System.out.println("Result= " + letter);
}
System.out.println("<<PRESS s/S TO CONTINUE>>");
keepGoing = scan.next().charAt(0);
} while (keepGoing == 's'||keepGoing == 'S');
}
public static boolean numberOrWord (String str){
try{
Double.parseDouble(str);
return true;
}catch(NumberFormatException e){
return false;
}
}
public static int digitSum(int number){
int sum = 0;
while(number > 0){
sum = sum + number % 10;
number /= 10;
}
return sum;
}
public static char calculateLetter(String word){
int sumPV = 0;
for(char digit : word.toCharArray()){
sumPV += alphabet.get(Character.toLowerCase(digit));
}
/* for(int i = 0, l = word.length(); i < l; i++){
char c = word.toLowerCase().charAt(i);
sumPV = sumPV + alphabet.get(c);
}*/
while(sumPV > 27){
sumPV =digitSum(sumPV);
}
char letter = (char) 0;
for(char key : alphabet.keySet()){
if(alphabet.get(key).equals(sumPV)) {
letter = key;
}
}
return letter;
}
}
Every char is just a mapping to integer value and you can print their intger values to see that A -> 65 by doing simple operations like
char d = 'D'; // D is just 68
so doing
d - 65 Or d - 'A'
Both result in 3
You can use the integer value representations for char and do integer operations like so
public class Main
{
public static void main(String[] args) {
System.out.println("Hello World");
char a = 'A';
System.out.println(a - 'A' + 1); //Returns 1
a = 'B';
System.out.println(a - 'A' + 1); //Returns 2
}
}
[Update] For your specific case you just need to replace ñ with O before replacing
You can use Character.getNumericValue().
This will give you the ASCII code for the letter.
As that code is not 1 for 'a' you need to compensate that by e.g.
int n = Caracter.getNumericValue(c) - Character.getNumericValue('a') + 1;
Note, that this will only work if you have only lower case characters.
A different approach, closer to your code, would be to simply use a string containing 'a' ... 'z' and use indexOf(c).
String alphabet = "abcdefg...z";
int n = alphabet.indexOf("a") + 1;
The +1 is necessary because index starts with 0.

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

generating list of small ascii strings

I want to create a generator class, in java, to produce the next smallest unique ascii string, where the resultant string must begin with an a-Z, and each subsequent letter can be a-Z or 0-9.
The strings are being used to minify variables in a javascript file.
Any suggestions on a tool out there which does this or some rough code as to how you'd implement?
If you need to use a different selection of characters you can use
public static void main(String... ignored) {
String prev = "";
for (int i = 0; i < 40000000; i++) {
String s = asId(i);
if (s.length() > prev.length())
System.out.print(prev + "\n" + s + " to ");
prev = s;
}
}
static char[] CHARS = "0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ".toCharArray();
public static String asId(long number) {
StringBuilder sb = new StringBuilder();
long div = number < 52 ? 1 : 52;
while (div <= number / 62) div *= 62;
while (div > 0) {
sb.append(CHARS[((int) (number / div % 62))]);
div /= 62;
}
return sb.toString();
}
prints
0 to Z
10 to ZZ
100 to ZZZ
1000 to
You can use the following in Java.
public static String asId(long number) {
return (char) ('a' + number % 26)
+ (number >= 26 ? Long.toString(number / 26, 36) : "");
}
If you concerned about negative numbers you can use.
public static String asId(long number) {
long lowBit = number & 1;
long highBits = number >>> 1;
return (char) ('a' + highBits % 13 + lowBit)
+ (number >= 26 ? Long.toString(highBits / 13, 36) : "");
}
Here are some ideas (not fully tested!!!)
import java.lang.*;
import java.util.*;
public class Test
{
static class VariablesIterator implements Iterator<String>
{
private List<Character> characters = new ArrayList<Character>();
private List<Integer> indices = new ArrayList<Integer>();
public VariablesIterator(String start)
{
char[] cs = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789".toCharArray();
for (char c : cs)
{
characters.add(c);
}
for (int i = 0; i < start.length(); ++i)
{
indices.add(characters.indexOf(start.charAt(i)));
}
}
public boolean hasNext()
{
return true;
}
public String next()
{
String current = "";
for (int i = 0; i < indices.size(); ++i)
{
current += characters.get(indices.get(i));
}
Integer last = indices.get(indices.size() - 1);
if (indices.size() != 1 && last != 2*26 + 10 - 1 || indices.size() == 1 && last != 2*26 - 1)
{
indices.set(indices.size() - 1, last + 1);
}
else
{
indices.add(0);
}
return current;
}
public void remove()
{
throw new UnsupportedOperationException();
}
}
static class Variables implements Iterable<String>
{
public Iterator<String> iterator()
{
return new VariablesIterator("a");
}
}
public static void main(String[] args)
{
Scanner in = new Scanner(System.in);
for (String variable : new Variables())
{
System.out.println(variable);
in.nextLine();
}
}
}

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