-3

I am getting mad, i need to create a token using this operator

PHP

a = "hello";
b = "world";
token = a | b;

well i need create the same in java [android] but i ever get error.
I alredy try to:
- cast the 2 strings to long but obvisuly i get an error on casting
- cast the 2 string in BitSet
- cast the 2 strings in Bit Array
but the final result was ever an error.
Someone can suggest me some tips please ?

BuBy
  • 59
  • 5

2 Answers2

1

I assume that you want to perform a bitwise operation on strings(which performs a bitwise operation on ASCII values of characters of these string in PHP). There is no such operator for String in Java, but you can do it using BitSet:

public String or(String a, String b) throws UnsupportedEncodingException {
    final String charsetName = "US-ASCII";
    BitSet aBitSet = BitSet.valueOf(a.getBytes(charsetName));
    aBitSet.or(BitSet.valueOf(b.getBytes(charsetName)));
    return new String(aBitSet.toByteArray(), Charset.forName(charsetName));
}
kraskevich
  • 18,368
  • 4
  • 33
  • 45
0

As Sotirios said, you are probably(?) trying to concatenate strings. If that's the case, then String token=a+b; should do the job. Again, it's unclear what you ask.. You might want to take a look at the String documentation.

Menelaos Kotsollaris
  • 5,776
  • 9
  • 54
  • 68