2

I would like to convert a long value to a char sequence like in this caluculator here.

I don't really know how that value is converted into the ASCII sequence (or if its even correct). I thought that an ASCII value is 8 bit long so that would mean that I have to convert the long value to binary and then split it into 8 bit blocks, is that correct?

live-love
  • 48,840
  • 22
  • 240
  • 204
wasp256
  • 5,943
  • 12
  • 72
  • 119

3 Answers3

4

Strictly, ASCII characters are 7 bit long and we usually just add an extra 0 to the beginning to get 8 bits. Extensions to ASCII (such as ISO 8859) have 8-bit long characters. The calculator you linked seems to be using one of those extensions.

In Java, longs have 64-bits (and one of those bits is used for the sign), so you can indeed have 8 chunks of 8-bit long characters.

First, you'll have to convert your long to a byte array (not all of that question is relevant to this case, but some of it is -- particularly the part that mentions ByteBuffer).

byte[] bytes = ByteBuffer.allocate(8).putLong(someLong).array();

Once you have the array, convert each byte to a char, using a simple cast.

EDIT: Instead of manually converting each character, you may use the java.lang.String(byte[]) constructor.

String str = new String(bytes);

Note that this will use the platform's default charset. If this is not desirable, you can use one of the constructors that also take a charset.

Community
  • 1
  • 1
luiscubal
  • 24,773
  • 9
  • 57
  • 83
  • 1
    You can simply use the `String` constructor that accepts `byte[]`. – Sotirios Delimanolis Oct 07 '13 at 19:06
  • @SotiriosDelimanolis Oh yeah, I forgot about that one. That's easier to use. – luiscubal Oct 07 '13 at 19:06
  • I've tried your method and it works fine so far unfortunately I still receive some unresolved characters (displayed with a question mark in a diamond)... what do you mean with `you can use one of the constructors`? Is there a way to extend the charset? – wasp256 Oct 07 '13 at 19:17
  • @wasp256 You can implement your own charsets, but it's overkill here. You get ? diamonds if the character is not recognized by the charset you're using. Try ISO 8859-1-15 and, if that doesn't give you the results you want, you can go back to using a loop and manually creating the characters. – luiscubal Oct 07 '13 at 19:18
  • Sry but how do I set the new ISO? – wasp256 Oct 07 '13 at 19:24
  • 1
    `new String(bytes, "ISO-8859-15")` or `new String(bytes, Charset.forName("ISO-8859-15"))` – luiscubal Oct 07 '13 at 19:25
0

Nope. There are several ways to do this. One way is using java.math.BigInteger which has a method called toByteArray(). Try it if it fits your problem.

Martin Kersten
  • 5,127
  • 8
  • 46
  • 77
0

Try this code to convert a long value to a 4-char array.

//convert long to char array

long longValIn = 229902744986400000L;

ByteBuffer bb1 = ByteBuffer.allocate(8);
bb1.putLong(longValIn); 

char [] charArr = new char[4];
charArr[0] = bb1.getChar(0);
charArr[1] = bb1.getChar(2);
charArr[2] = bb1.getChar(4);
charArr[3] = bb1.getChar(6);


//convert char array to long

ByteBuffer bb2 = ByteBuffer.allocate(8);
bb2.putChar(0,charArr[0]);
bb2.putChar(2,charArr[1]);
bb2.putChar(4,charArr[2]);
bb2.putChar(6,charArr[3]);

long longValOut = bb2.getLong(0); 
live-love
  • 48,840
  • 22
  • 240
  • 204