I get two java bytes as input that together symbolizes a 16-bit signed integer. I need to convert it to one single java integer (signed, of course). I have come up with a "ugly" solution, that includes converting to an int, then to a short and then back to an int. Is there a shorter and more elegant way? My code is as following:
------=_Part_19_678369072.1513344309074
Content-Type: application/xop+xml; charset=UTF-8; type="text/xml"
Content-Transfer-Encoding: 8bit
Content-ID: <rootpart@soapui.org>
<S:Envelope xmlns:S="http://schemas.xmlsoap.org/soap/envelope/">
<S:Body>
<ns2:getFileResponse xmlns:ns3="urn:eu:europa:ec:etrustex:integration:service:notification:v2.0" xmlns:ns2="urn:eu:europa:ec:etrustex:integration:service:filerepository:v2.0" xmlns="urn:eu:europa:ec:etrustex:integration:model:common:v2.0">
<ns2:fileWrapper>
<Content><inc:Include href="cid:test.txt" xmlns:inc="http://www.w3.org/2004/08/xop/include"/></Content>
</ns2:fileWrapper>
</ns2:getFileResponse>
</S:Body>
</S:Envelope>
------=_Part_19_678369072.1513344309074
Content-Type: text/plain; charset=us-ascii
Content-Transfer-Encoding: 7bit
Content-ID: <test.txt>
Content-Disposition: attachment; name="test.txt"
TEST
------=_Part_19_678369072.1513344309074--
答案 0 :(得分:1)
This seems to match your converter - not certain it is simpler but it is certainly less verbose.
public function username()
{
return 'member_id';
}
答案 1 :(得分:1)
以下是等效的:
return (short) ((b2 << 8) | (b1 & 0xFF));
byte
的范围足够小,可以测试b1
和b2
的所有可能值的等效性:
byte b1 = Byte.MIN_VALUE;
do {
byte b2 = Byte.MIN_VALUE;
do {
assertEquals(convert(b1, b2), convertEquivalent(b1, b2));
} while (b2++ != Byte.MAX_VALUE);
} while (b1++ != Byte.MAX_VALUE);
答案 2 :(得分:0)
@AndTurner可能是您寻求的解决方案。
但是如果涉及输入流的字节数组或某个文件通道(内存映射文件),可以使用ByteBuffer。
byte[] bytes = ...
ByteBuffer buf = ByteBuffer.wrap(bytes).order(ByteOrder.LITTLE_ENDIAN);
...
short n = buf.readShort(); // Sequential
short m = buf.readShort(354L); // Direct access