data type conversion problem ?

Thread Starter

Mathematics!

Joined Jul 21, 2008
1,036
Ok , I am using a bunch of different languages but my major problem lies between java , c/c++

Two problems come into play
1) unsigned / signed issues
2) data type size issues

Problem 1
I know that weather the data type is signed or unsigned it doesn't matter since it is going to hold the same value in both data types provided the data types sizes are the same. But for displaying the value problems occur. How can I solve this without always have to create a BigInteger Object in java.
For example
in java
int var = (int)4294967295 ; //still has the same physical value 0xFF 0xFF 0xFF 0xFF it is just displaying this value differently then I want it to display (i.e wanted to print 4294967295 )

Stipulations are that you cann't increase the size of the datatype to make it display correctly.
Since this is legacy code that calls the jni (so I can not manipulate the sizes of the structures I am giving or getting back.

Is their a standard function I can call in java that allows me to display the unsigned value of a variable.
Note this is not only for integers but for shorts , longs , bytes as well?

Don't tell me I have to write my own function that parses thru bits ,..etc and uses the next biggest data structure
(i.e 2^32+...+ 2^3 * bit3 + 2^2*bit2+2*bit1+bit0)
That is my last resort I could do this but I would like to find a function I could call to do this for any variable type.

This problem is just in displaying the value. Note no loss of data occurs when casting from signed to unsigned of the same data type/size I know this at least.
 
Last edited:

kubeek

Joined Sep 20, 2005
5,795
Java doesn´t have unsigned integers, so you have to use a next larger type and convert the number by adding 2^n when java thinks it's negative. That is when you got bytes in and need to interpret them as unsigned integers.
 

Thread Starter

Mathematics!

Joined Jul 21, 2008
1,036
Ok , I guess if that is the only way then I will have to use it.

But I am wondering what I do when I exceed the biggest builtin signed non-floating point data type in java?

For instance say I am working with long and I exceed the limit of
9223372036854775807 say with 9223372036854775810 what structure Should I use? Must I go to the arbitary precison non-built in types of BigInteger or BigDecimal or is their some bigger non-floating point builtin data type in java that I do not know about?

This problem comes in when converting from an unsigned long in c++ and returning it to java signed long ?

question 2
Are float and double backwards compatible ?
Meaning if I take a float and cast it or assign it to a double will it always retain the same value that the float had or are these types not compatible when converting/assigning the smaller to the larger?

Basically if I had a whole bunch of legacy code to convert and it all used floats but now the values/calculations are exceeding the floats 32bit compacity is their any easy way to convert it all to doubles the floating data types where the calculations are going to exceed? (not bigdeciemal is over kill )
 
Last edited:

kubeek

Joined Sep 20, 2005
5,795
For instance say I am working with long and I exceed the limit of
9223372036854775807 say with 9223372036854775810 what structure Should I use? Must I go to the arbitary precison non-built in types of BigInteger or BigDecimal or is their some bigger non-floating point builtin data type in java that I do not know about?
depening by how much you exceed it, you could use for example a byte to store the upper bits, thus having 64+8 bit numbers, or just use the arbitrary precision libraries.

question 2
Are float and double backwards compatible ?
Meaning if I take a float and cast it or assign it to a double will it always retain the same value that the float had or are these types not compatible when converting/assigning the smaller to the larger?
Double is an "enhancement" of float, so the number converted into double should be as imprecise as the original float was an not more. But remeber that floating point numbers are not entirely accurate and you have allways some error in the lower orders.

If I were you, I would double-think about converting any computational program to Java, because all these conversions of data will have its toll. Not only in memory usage but also in processing speed because you need to process larger numbers even if the high orders contain basically zeroes.

If you need to process large amounts of data, calculate it in a C program first and then maybe pass it through a file to a Java application for presentation.

When I wrote my bachelors thesis I downloaded megabytes of data from a microcontroller and then did some simple calculations with it. The application was in Java and it was horribly slow, as in seconds to gather and recalculate data from a 200kB binary file full of integers and floats (mind you on a 2.8ghz dual core with plenty of ram). I chose Java because of the ease of making UI, and I would never do the same mistake again. Next time C core and maybe Java UI.
 
Last edited:
I know that weather the data type is signed or unsigned it doesn't matter since it is going to hold the same value in both data types provided the data types sizes are the same..
No this isnt true, the signed value if INT can only hold 15 bits where as UNSIGNED INT can hold 16 bits. I think this is the bit that is confusing you.
So FFFF is -1 in signed and 65535 in unsigned.
 

djsfantasi

Joined Apr 11, 2010
9,163
Converting float to double may have unintended questions. In the mathematics department at Georgia Tech many, many years ago, I was their computer technical consultant. We had converted to a 64 bit machine and I had a professor enter my office very upset. Seems he had a set of programs that "suddenly started working." They were an example of how numerical analysis may not converge to a solution. Since the default precision changed with the new system, we had to force them to use single precision in order to make them "fail" once asgain.
 
Top