0

I have a .const file, lets say abc.const (a cpp file). the contents of that file is,

xyz :ullong:0x1000000000000000ULL yub :ullong:0x0100000000000000ULL .... ....

now i ve a program to convert this file to .java class.

But when i try above,

i got the following error

abc.java:255: ';' expected public static final long xyz = 0x1000000000000000ULL;

how should i resolve this. thanks in advance??

( i need to generate a .java class from this .const file by solving this )

2 Answers2

2
  • There are no unsigned types in Java, so there's no "U" numeric literal specifier
  • You only use a single L for a long in Java, which is always 64 bits
  • You appear to have two leading 0s before the x.

So the Java would be:

public static final long xyz = 0x1000000000000000L;

Note that this won't be an exact equivalent of the C++, due to Java not having unsigned types as mentioned before. If you need to represent values larger than Long.MAX_VALUE, you should use the BigInteger class. If you're find with the restricted range of long, you're better off sticking with the primitive type.

Jon Skeet
  • 1,421,763
  • 867
  • 9,128
  • 9,194
  • oh yeah thanks Jon, so i have to make use of this class in the program that generates .java class right? – Logeshwari Ram Apr 19 '11 at 05:41
  • @ Jon Skeet - thanks for the info. but i can modify in .const file only. .java class is generated when i build .const file. so .const file as in c++ not accepting 0x100000000000000L and throwing "warning: integer constant is too large for 'long' type" error. so what can we do now? – Logeshwari Ram Apr 19 '11 at 05:48
  • @Logeshwari: It sounds like you need to change whatever tool is creating the Java file to be smarter. – Jon Skeet Apr 19 '11 at 05:55
  • @ Jon Skeet: oh any idea on what can be done?? like should we write a function that converts ULL (from .const) to L(and suffix in .java file) like such or anyother?? – Logeshwari Ram Apr 19 '11 at 05:59
  • @Logeshwari: It's not clear to me what the format of the .const file is, or what you're using to convert it to Java, but it sounds like that tool needs more of an understanding of both C++ and Java constants. You can't just use the text directly and expect it to work in both languages. – Jon Skeet Apr 19 '11 at 06:11
1

There is no Java equivalent to an unsigned long long

If you need a number that large, you're going to have to use the BigInteger class:

http://download.oracle.com/javase/6/docs/api/java/math/BigInteger.html

That being said, the max value for a java long is 2^63-1. If that's big enough for you just change

0x100000000000000ULL

to

0x100000000000000L
Brian Roach
  • 76,169
  • 12
  • 136
  • 161
  • You've removed the hex specifier, which completely changes the value. – Jon Skeet Apr 19 '11 at 05:30
  • @ Jon Skeet - thanks for the info. but i can modify in .const file only. .java class is generated when i build .const file. so .const file as in c++ not accepting 0x100000000000000L and throwing "warning: integer constant is too large for 'long' type" error. so what can we do now? – Logeshwari Ram Apr 19 '11 at 05:40
  • @ Brian Roach - thanks for the info. but i can modify in .const file only. .java class is generated when i build .const file. so .const file as in c++ not accepting 0x100000000000000L and throwing "warning: integer constant is too large for 'long' type" error. so what can we do now? – Logeshwari Ram Apr 19 '11 at 05:48
  • Punt? Sorry ... I don't have an answer for that one, I've never done what you're trying to do. What is generating the Java? – Brian Roach Apr 19 '11 at 06:03
  • @logeshwari-ram, If I do `public static final long L = 0x100000000000000L;` it compiles fine. Can you give the exact line of code and it error message? – Peter Lawrey Apr 19 '11 at 08:39