1

I am working on a project where efficiency is key, and need to write unsigned ints over the network. My code is like this:

DataOutputStream out = ...;
int value = ...;
out.writeUnsignedInt(value);

For reading:

DataInputStream in = ...;
int value = in.readUnsignedInt();

So to be clear: I want to write 4 bytes, and my value is not larger than 2^31-1 (that's why my input/output variables are ints. The values are larger than 2^15-1 however)

I am using Java 8 if it matters. Thank you for your time, and sorry in advance if I've done something stupid

atoms118
  • 105
  • 2
  • 11
  • 3
    Possible duplicate of [Writing unsigned int of 4 bytes over network](https://stackoverflow.com/questions/7830175/writing-unsigned-int-of-4-bytes-over-network) – Mitchell Skaggs Dec 27 '17 at 22:16
  • @MitchellSkaggs I saw that question, but the answer didn't really have much of an explanation and it's unclear what casts are significant (Is the initial cast from long to int important? Does the bitwise stuff still work if I'm working with an int instead of a long?) – atoms118 Dec 27 '17 at 22:19
  • @gontrollez It's just for a hobby project, but I'm not sure that's relevant to the question – atoms118 Dec 27 '17 at 22:23
  • 3
    You can still use the functions provided in that answer, just remove the conversions from int->long and long->int since you're already working in int – IsThisJavascript Dec 27 '17 at 22:27
  • 1
    If you're just reading and writing, then the difference between signed and unsigned doesn't matter. It's the same thing. The only operations for which there's a difference are comparison, division, and remainder (and, as a corollary, conversion to strings, which uses all three of the above). – Louis Wasserman Dec 27 '17 at 22:28
  • @IsThisJavascript So do I not need the bitwise code on read? Also, if it just me or does the docs not have anything about unsigned values in DataOutputStream in them? – atoms118 Dec 27 '17 at 22:30
  • @LouisWasserman It does matter of I have a program in a different language reading the values and expecting them to be unsigned, or if I'm writing a lot of values and memory conservation is important – atoms118 Dec 27 '17 at 22:31
  • 1
    @atoms118 You can remove the bitshift as that's only used for int->long conversion:https://stackoverflow.com/questions/9578639/best-way-to-convert-a-signed-integer-to-an-unsigned-long – IsThisJavascript Dec 27 '17 at 22:33
  • @IsThisJavascript Ok, this technically works, but it's still using a signed value internally, so if I read the value in another language like C, it wouldn't match up. I've actually tested this like you suggested, and confirmed my suspicions. – atoms118 Dec 27 '17 at 22:48
  • @atoms118 neither of those should be true. If you have a four-byte value in Java, signed or unsigned, you cast it to an `int`, and you write it -- whichever way you want -- and read it to an unsigned four-byte number in another language, it should work correctly as long as both sides are using the same endianness (which is unrelated to signedness). Memory consumption isn't different between signed and unsigned values, either. – Louis Wasserman Dec 27 '17 at 22:51
  • @LouisWasserman I'm not sure I understand. If Java writes a signed 4 byte value, and my C program tries to read an unsigned 4 byte value, the result will not be the same...? I could be mistaken, you probably know more about this then I do, but that's my understanding of it anyways – atoms118 Dec 27 '17 at 22:55
  • The result will be the same as if you were treating the Java `int` as unsigned. E.g. if you take the signed Java int and print it with `Integer.toUnsignedString`, write the signed 4 byte value to C, read it as a 4 byte unsigned value in C, and print it, you will get the same result. – Louis Wasserman Dec 27 '17 at 23:00
  • @LouisWasserman I see, thank you! – atoms118 Dec 27 '17 at 23:01

0 Answers0