How to convert a long into printable bytes - java

I have a string which represent a long. Like "12345678901" (11 chars long).
I convert it into a long using Long.parse(), that's fine.
Now, I want to send this long as a short string, like "eR%s" over the wire.
The goal is to have this final string as short as possible. Any idea what's the best way to do that? I can use more characters as the URL encoding (Like I can use /, %, :, etc.)

Java can handle a radix as high as 36 using the digits 0 - 9 and lower case letters a - z.
> Long.toString(12345678901L, 36)
"5o6aqt1"
> Long.parseLong("5o6aqt1", 36)
12345678901
You could create your own encoding using 65 of the 66 unreserved URI Characters (so your URI would not need escaping). The '-' sign needs to be used for negative numbers:
> Long65.toString(12345678901L)
"aFDIbA"
> Long65.parseLong65("aFDIbA")
12345678901
Here is the code for Long65()
import java.math.BigInteger;
public class Long65 {
private static int base = 65;
private static String URIchars = "0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ_.~";
public static String toString(Long aNumber) {
StringBuilder result = new StringBuilder();
if (aNumber < 0) {
result.append('-');
aNumber = -aNumber;
}
int r = (int)(aNumber % base);
if (aNumber - r == 0)
result.append(URIchars.charAt(r));
else
result.append(Long65.toString((aNumber - r) / base) + URIchars.charAt(r));
return result.toString();
}
public static long parseLong65(String aNumber) {
char[] digits;
int sign = 1;
if (aNumber.charAt(0) == '-') {
sign = -1;
digits = aNumber.substring(1).toCharArray();
} else {
digits = aNumber.toCharArray();
}
BigInteger bigBase = BigInteger.valueOf(base);
BigInteger power = bigBase.pow(digits.length);
BigInteger total = BigInteger.valueOf(0);
for (char digit : digits){
power = power.divide(bigBase);
total = total.add(power.multiply(BigInteger.valueOf(URIchars.indexOf(digit))));
}
return sign * total.longValue();
}
}

Use a different base for your number if you want a shorter representation.

The bigger the numeric base you use, the smaller the representation. You can try base 16 for example:
Long.toString(num, 16)
This should return a string of at most 16 characters.
If that's not small enough, you can build a representation in a bigger base. However, if the resulting string needs to be URL escaped, this may not be useful. In base 256 for example, 8 chars would be enough for any number, but many of the 256 chars need to be escaped, making the resulting text longer. So you have to choose your alphabet carefully, if you choose to implement such an encoding/decoding scheme yourself.
Take a look at http://en.wikipedia.org/wiki/Base64 for example. You can use this Java implementation. You may also be interested in Base85 and its implementations.

To reply to the comments Base64 vs other options, I will say, it all depends on the constraints you have on the character set. I'm not talking about transmitting over an URL, but over a char stream which need to be a text stream. I can not simply send a byte array since non-printable chars might cause certain issues.
So I built something like that (see below) which transforms a long in base92 (almost). It uses all the printable chars excluding minus and pipe which I use for delimiters.
It's almost a cut&past of Base65 where I simply build the list of valid digits dynamically. Can be reuse for any base or any list of valid digits.
<!-- language: java -->
public class LongConverter {
private static String URIchars;
static {
StringBuilder result = new StringBuilder();
for (int i = 32; i < 255; i++) {
if ((i != 45) && (i != 124))
result.append((char)i);
}
URIchars = result.toString();
}
public static String toString(Long aNumber) {
int base = URIchars.length();
StringBuilder result = new StringBuilder();
if (aNumber < 0) {
result.append('-');
aNumber = -aNumber;
}
int r = (int) (aNumber % base);
if (aNumber - r == 0) result.append(URIchars.charAt(r));
else result.append(Long65.toString((aNumber - r) / base) + URIchars.charAt(r));
return result.toString();
}
public static long parseLong(String aNumber) {
int base = URIchars.length();
char[] digits;
int sign = 1;
if (aNumber.charAt(0) == '-') {
sign = -1;
digits = aNumber.substring(1).toCharArray();
} else {
digits = aNumber.toCharArray();
}
long total = 0;
long power = 1;
for (int i = 0; i < digits.length; i++)
power *= base;
for (char digit : digits) {
power /= base;
total += URIchars.indexOf(digit) * power;
}
return sign * total;
}
}

Related

How can I loop through all the characters that are 0 in a given string?

I'm trying to remove trailing zeroes from an integer and here is my code so far.
import java.math.BigInteger;
public class newuhu {
public static int numTrailingZeros(int s) {
BigInteger J = BigInteger.valueOf(s);
String sb = J.toString();
String Y = "";
while (sb.length() > 0 && sb.charAt(sb.length() - 1) == '0') {
sb.replaceAll("0"," ");
}
return Integer.parseInt(Y);
}
Note: I turned my int into a Biginteger because I've been warned that some inputs may look like 20!, which is 2.432902e+18
However, my IntelliJ debugging tool tells me that variable sb isn't in the loop. So, I'm trying to understand what must be done to make sure sb is in the loop.
Please understand that I'm a beginner in Java so, I'm trying to learn something new.
replaceAll replaces all occurrences of string with character that you want (ie space) so you don't need loop at all, also you're concerned about overflow so you should actually use BigInteger as a parameter, not int (int wont fit anything close to 20!) but there's another issue with your code, you said you want to replace trailing zeros but right now you will replace every 0 with blank character, you should try to use something like https://commons.apache.org/proper/commons-lang/apidocs/org/apache/commons/lang3/StringUtils.html
public class newuhu {
public static int numTrailingZeros(BigInteger s) {
String sb = s.toString();
return Integer.parseInt(sb.replaceAll("0", "")); // consider returning something else if you're working with BigInteger
}
Keep in mind that when doing BigInteger.valueOf(int) does not have an effect as a number to big for int will never be stored in an int. Also 20! is fine for int.
public static String trimTrailingZeros(String source) {
for (int i = source.length() - 1; i > 0; ++i) {
char c = source.charAt(i);
if (c != '0') {
return source.substring(0, i + 1);
}
}
return ""; // or return "0";
}
Or if you prever BigInteger.
public static BigInteger trimTrailingZeros(BigInteger num) {
while (num.remainder(BigInteger.TEN).signum() == 0) {
num = num.divide(BigInteger.TEN);
}
return num;
}
This should be fast as you only create one string (via substring).
(First: variables and fields should start with a small letter - when no constants.)
It should be
sb = sb.replaceAll(...)
as sb is not changed by replaceAll, only the replaced value is returned. The class String gives immutable values, that always remain the same, so you can assign a variable to a variable and changing values of either variable will never influence the other - no further sharing.
Then it should be:
sb = sb.replaceFirst("0$", "");
replaceAll would replace every 0, like "23043500" to "23435".
replaceFirst replaces the _regular expression: char '0' at the end $.
Overflow on the input number is not possible, as you are already passing an int.
public static int numTrailingZeros(int n) {
while (n != 0 && n % 10 == 0) {
n /= 10;
}
return n;
}
public static int numTrailingZeros(String n) {
n = n.replaceAll("0+", "");
if (n.isEmpty() || n.equals("-")) { // "-0" for pessimists?
n = "0";
}
return Integer.parseInt(n);
}
% is the modulo operator, the remainder of an integer division 147 % 10 == 7.
The name is misleading, or you are calculating something different.
public static int numTrailingZeros(int n) {
int trailingZeros = 0;
while (n != 0 && n % 10 == 0) {
n /= 10;
++trailingZeros ;
}
return trailingZeros ;
}
The problem here is that sb.replaceAll("0","") won't do anything. You're throwing away the return value that contains your replaced string. See here.
What you probably want is something like this:
while (sb.length() > 0 && sb.charAt(sb.length() - 1) == '0') {
sb = sb.replaceAll("0"," ");
I'm not sure you need a while loop, though. ReplaceAll will... replace all of the zeros with spaces.

Take in String that contains number characters and add commas to each thousands place WITHOUT converting string into int or long

I want to take in a string for example "1234567890" and add commas to each thousands place in the number. However, I don't want to parse this string into an int or long. I think I might need to use recursion but I dont know how.
String number1 = "1234567890";
System.out.println(stringNumberAddCommas(number1));
//output == 1,234,567,890
public static String stringNumberAddCommas(String number1){ }
we can achieve this as below as well-
public static String stringNumberAddCommas(String number) {
for (int i = number.length() - 1; i >= 0; ) {
if (i >= 3) {
number = number.substring(0, i - 2) + "," + number.substring(i - 2);
}
i = i - 3;
}
System.out.println("number=" + number);
return number;
}
There's no need to mess with recursion; all you need is to realize that you need to either work backwards or do some very basic math based on length.
Given a pile o digits that is, say, 8 characters long, that's all the information you need to know that the first comma is after 2 digits, and then every further comma at +3 digits from that position until you reach the end.
Tools needed:
"12345678".length() (= 8)
"12345678".substring(2, 5) (= "345")
a for (int i = ...; i < in.length(); i+= 3) loop.
a new StringBuilder() along with its .append() method, both for appending the comma as well as those substrings.
Some exceedingly basic math on that length, involving in particular the % operator. a % b divides a by b, tosses the result in the garbage, and returns the leftovers. In other words, 5 % 3 returns 2 (because 5 divides into 3 one time, and that leaves a remainder of 2).
String test = "1234567890";
String reversed = new StringBuffer(test).reverse().toString();
int i = 0;
StringBuilder ans = new StringBuilder();
while(i+3<reversed.length()){
ans.append(reversed, i, i + 3).append(",");
i+=3;
}
ans.append(reversed, i, reversed.length());
String solution = ans.reverse().toString();
I'd define an offset to find the length of the first substring, then iterate over the string in 3 character substrings.
StringJoiner would also be helpful in adding the needed commas.
public static String stringNumberAddCommas(String str){
int offset = str.length() % 3 != 0 ? str.length() % 3 : 3;
StringJoiner sj = new StringJoiner(",");
for (int s = 0, i = offset; i <= str.length(); s = i, i += 3) {
sj.add(str.substring(s, i));
}
return sj.toString();
}

How to put numbers to array in while loop in java?

I am trying to add two binary numbers and then get their sum in binary system. I got their sum in decimal and now I am trying to turn it into binary. But there is problem that when I take their sum (in decimal) and divide by 2 and find remainders(in while loop), I need to put remainders into array in order print its reverse. However, there is an error in array part. Do you have any suggestions with my code? Thanks in advance.
Here is my code:
import java.util.Scanner;
public class ex1 {
public static void main(String[] args) {
Scanner scan = new Scanner(System.in);
int n = scan.nextInt();
int m = scan.nextInt();
int k = dec1(n)+dec2(m);
int i=0,c;
int[] arr= {};
while(k>0) {
c = k % 2;
k = k / 2;
arr[i++]=c; //The problem is here. It shows some //error
}
while (i >= 0) {
System.out.print(arr[i--]);
}
}
public static int dec1(int n) {
int a,i=0;
int dec1 = 0;
while(n>0) {
a=n%10;
n=n/10;
dec1= dec1 + (int) (a * Math.pow(2, i));
i++;
}
return dec1;
}
public static int dec2(int m) {
int b,j=0;
int dec2 = 0;
while(m>0) {
b=m%10;
m=m/10;
dec2= dec2 + (int) (b * Math.pow(2, j));
j++;
}
return dec2;
}
}
Here:
int[] arr= {};
creates an empty array. Arrays don't grow dynamically in Java. So any attempt to access any index of arr will result in an ArrayIndexOutOfBounds exception. Because empty arrays have no "index in bounds" at all.
So:
first ask the user for the count of numbers he wants to enter
then go like: int[] arr = new int[targetCountProvidedByUser];
The "more" real answer would be to use List<Integer> numbersFromUsers = new ArrayList<>(); as such Collection classes allow for dynamic adding/removing of elements. But for a Java newbie, you better learn how to deal with arrays first.
Why are you using two different methods to do the same conversion? All you need is one.
You could have done this in the main method.
int k = dec1(n)+dec1(m);
Instead of using Math.pow which returns a double and needs to be cast, another alternative is the following:
int dec = 0;
int mult = 1;
int bin = 10110110; // 128 + 48 + 6 = 182.
while (bin > 0) {
// get the right most bit
int bit = (bin % 10);
// validate
if (bit < 0 || bit > 1) {
throw new IllegalArgumentException("Not a binary number");
}
// Sum up each product, multiplied by a running power of 2.
// this is required since bits are taken from the right.
dec = dec + mult * bit;
bin /= 10;
mult *= 2; // next power of 2
}
System.out.println(dec); // prints 182
An alternative to that is to use a String to represent the binary number and take the bits from the left (high order position).
String bin1 = "10110110";
int dec1 = 0;
// Iterate over the characters, left to right (high to low)
for (char b : bin1.toCharArray()) {
// convert to a integer by subtracting off character '0'.
int bit = b - '0';
// validate
if (bit < 0 || bit > 1) {
throw new IllegalArgumentException("Not a binary number");
}
// going left to right, first multiply by 2 and then add the bit
// Each time thru, the sum will be multiplied by 2 which shifts everything left
// one bit.
dec1 = dec1 * 2 + bit;
}
System.out.println(dec1); // prints 182
One possible way to display the result in binary is to use a StringBuilder and simply insert the converted bits to characters.
public static String toBin(int dec) {
StringBuilder sb = new StringBuilder();
while (dec > 0) {
// by inserting at 0, the bits end up in
// correct order. Adding '0' to the low order
// bit of dec converts to a character.
sb.insert(0, (char) ((dec & 1) + '0'));
// shift right for next bit to convert.
dec >>= 1;
}
return sb.toString();
}

Datatype for getting input for over 30 digits

I have a serious problem. I need to get a number say
123454466666666666666665454545454454544598989899545455454222222222222222
and give the total of that number. I was trying for a long time. I couldn't get the answer. The problem is I didn't know which data type to use. I have tried it long. It accepts only 18 digits. I have gone through BigInteger. But I couldn't make arithmetic operations with it. so help me out with this problem..
1.Get it as a string
2.get length of it.
3.Loop through each character of it.
4.check if the character is a number.
5.If yes parse it to int.
6.Add all numbers together in the loop
OR
Use BigDecimal
You can get the result from the below code.
String string = "123454466666666666666665454545454454544598989899545455454222222222222222";
int count = 0;
for (int i = 0; i < string.length(); i++) {
count += Integer.parseInt(String.valueOf(string.charAt(i)));
}
System.out.println(count);
Just use it as a String. That's the easiest way to go for the task at hand.
public class Test022 {
public static void main(String[] args) {
String s = "123454466666666666666665454545454454544598989899545455454222222222222222";
int sum = 0;
for (int i=0; i<s.length(); i++){
sum += s.charAt(i) - '0';
}
System.out.println(sum);
}
}
i can suggest using this code and the numbers as String
/**
* Adds two non-negative integers represented as string of digits.
*
* #exception NumberFormatException if either argument contains anything other
* than base-10 digits.
*/
public static String add(String addend1, String addend2) {
StringBuilder buf = new StringBuilder();
for ( int i1 = addend1.length() - 1, i2 = addend2.length() - 1, carry = 0;
(i1 >= 0 && i2 >= 0) || carry != 0;
i1--, i2-- ) {
int digit1 = i1 < 0 ? 0 :
Integer.parseInt(Character.toString(addend1.charAt(i1)));
int digit2 = i2 < 0 ? 0 :
Integer.parseInt(Character.toString(addend2.charAt(i2)));
int digit = digit1 + digit2 + carry;
if (digit > 9) {
carry = 1;
digit -= 10;
} else {
carry = 0;
}
buf.append(digit);
}
return buf.reverse().toString();
}
BigInteger does support methods like add/multiply etc. See this for details.
BigInteger operand1 = new BigInteger("123454466666666666666665454545454454544598989899545455454222222222222222");
BigInteger operand2 = new BigInteger("123454466666666666666665454545454454544598989899545455454222222222222222");
System.out.println(operand1.add(operand2));
System.out.println(operand1.subtract(operand2));
System.out.println(operand1.multiply(operand2));
System.out.println(operand1.divide(operand2));

How to get alphabates letter number and operate on it [closed]

This question is unlikely to help any future visitors; it is only relevant to a small geographic area, a specific moment in time, or an extraordinarily narrow situation that is not generally applicable to the worldwide audience of the internet. For help making this question more broadly applicable, visit the help center.
Closed 10 years ago.
I have a requirement where I need to return the alphabet when given an alphabet and a number.
Example if given, C and 4 I will return C+4 = G
Also if given C and -2 I will return C + (-2) = A
If I have AA then, AA + 4 = AD, So I will always want to take the last character from the string.
I was thinking of using string array to store alphabets, but it seems kind of bad solution. Is there any way by which I can get it done better ?
Alphabet characters are all already in order, all you need to do is add a number to one to get another.
I presume you want something like this:
addToChar('A', 4);
char addToChar(char inChar, int inNum)
{
return (char)(inChar + inNum);
}
You may want to check whether it is less than 'A' or greater than 'Z' as well.
In response to your edit:
void addToChar(char[] inChars, int inNum)
{
for (int i = inChars.length-1; inNum != 0 && i >= 0; i--)
{
int result = inChars[i]-'A'+inNum;
if (result >= 0)
{
inNum = result / 26;
result %= 26;
}
else
{
inNum = 0;
while (result < 0) // there may be some room for optimization here
{
result += 26;
inNum--;
}
}
inChars[i] = (char)('A'+result);
}
}
To deal with overflow: (somewhat less efficient) ('Z' + 1 outputs 'AA')
static String addToChar(String inChars, int inNum)
{
String output = "";
for (int i = inChars.length()-1; inNum != 0 || i >= 0; i--)
{
if (i < 0 && inNum < 0)
return "Invalid input";
int result = i >= 0 ? inChars.charAt(i)-'A'+inNum
: -1+inNum;
if (result > 0)
{
inNum = result / 26;
result %= 26;
}
else
{
inNum = 0;
while (result < 0)
{
result += 26;
inNum--;
}
}
output = (char)('A'+result) + output;
}
return output;
}
try this for example :
public class example {
public static void main(String[] args) {
int number = 2;
char example = 'c';
System.out.println((char)(example+number));
}
}
this is an example for the updated question :
still need to verify the input number and input String (lets say what happens if the number is 124 ?)
public class example {
public static void main(String[] args) {
int number = 1;
String example = "nicd";
//get the last letter from the string
char lastChar = example.charAt(example.length()-1);
//add the number to the last char and save it
lastChar = (char) (lastChar+number);
//remove the last letter from the string
example = example.substring(0, example.length()-1);
//add the new letter to the end of the string
example = example.concat(String.valueOf(lastChar));
//will print nice
System.out.println(example);
}
}
Have you ever google about character set? Like ASCII, a character is already represented by a number.
You don't need to store alphabet letters in an array; this is one reason why ASCII has all letters in consecutive order.
Perform the math operation, which implicitly converts the char to an int, then cast the result to a char. You'll have to check that you don't go before 'A' or after 'Z'.
Here's an ASCII table reference.
First, convert your character to an int with a cast, then add your int, and convert it back to a char. For instance:
char c = 'c';
int cInt = (int)c;
int gInt = cInt + 4;
char g = (char)gInt; // 'G'

Categories