Problem in adjusting the range of long value

Hi all

I have problem

I have application running in C language which sends data to other application which is running in java .. Its through socket communication

My doubt is in C the size of long is 4bytes and in java 8bytes

my C application is sending a negative long value

so with C long has 32 bit in which the last bit is for signed/unsigned

but the value when i convert in java will i get postive value or negative value

In java long is 64 bits and last is for signed/unsigned

So when i convert that C long value which is a negative value to Java long value it come under the range of postive java long value

How to solve this

[696 byte] By [Prakash_Halana] at [2007-11-26 18:41:04]
# 1
What am I missing? Why not just map the C long to a Java int ?
sabre150a at 2007-7-9 6:15:06 > top of Java-index,Core,Core APIs...
# 2
thank you friend I used int before in java actullay when the C long value becomes it maximum i get negative values but C is sending only positive long value ...I think there might be some problem in my code i'll see that Thank you once again
Prakash_Halana at 2007-7-9 6:15:06 > top of Java-index,Core,Core APIs...
# 3
Are you using DataInputStream.readInt()?
ejpa at 2007-7-9 6:15:06 > top of Java-index,Core,Core APIs...
# 4
No mate Actullay we r communcatong using some protocol convention the C applcation send a packet in which the values are dumped we decode the packet and read .. we use in.read(output, 0, length);
Prakash_Halana at 2007-7-9 6:15:06 > top of Java-index,Core,Core APIs...
# 5

Well I think you need to make up your mind what the problem is. In your OP you said this:

> my C application is sending a negative long value

and now you're saying this:

> but C is sending only positive long value.

In any case all you have to do is convert the 4 bytes to a Java integer correctly, which is just a few shifts and masks.

But it would be both easier and clearer to use the methods of DataInputStream directly on the input stream to give you the appropriate datatypes as they arrive, without using the byte[] array at all.

ejpa at 2007-7-9 6:15:06 > top of Java-index,Core,Core APIs...
# 6

mate

The variable in which the C stores its value is a signed long value

k...so it send only positive value. But when the value overflows it start sending negative values.

So till C is sending postive values no problem ... and when its start sending negative value and if my java also get negative value no problem

but wat happening is while receiving i get positive value

But i don't understand where the problem is in java or c

anyhow thanks for ur comment

take care

bye prakash

Prakash_Halana at 2007-7-9 6:15:06 > top of Java-index,Core,Core APIs...
# 7
You would have to show us your sending and receiving code for anyone to be able to answer that, wouldn't you? or is this just a guessing game?
ejpa at 2007-7-9 6:15:06 > top of Java-index,Core,Core APIs...
# 8
No mate its not a guessing game . I think i am not able to make you understand. The project i m working for is networking project where i have to read the traffic coming .... its k anyhow thank you bye take care
Prakash_Halana at 2007-7-9 6:15:06 > top of Java-index,Core,Core APIs...
# 9

Okay, strip it down:

What is the range of values being sent? What's the minimum and maximum numerical values that you want to communicate?

How many bytes are those values being sent in?

How is it being encoded?

Give some examples of values you're sending that are being interpreted incorrectly: What is the value? What is the byte pattern? What value is Java interpreting it as?

jverda at 2007-7-9 6:15:06 > top of Java-index,Core,Core APIs...