Combining 4 Bytes To Integer

BullyWiiPlaza

I have an array of bytes called contents from which I want to read bytes, shorts and integers from.

To do this, I read bytes individually and combine them by shifting.

public byte getByteValue(int fileOffset)
{
    return contents[fileOffset];
}

public short getShortValue(int fileOffset)
{
    short shortValue;

    shortValue = getByteValue(fileOffset);
    shortValue = (short) (shortValue * 256 + getByteValue(fileOffset + 1));

    return shortValue;
}

public int getIntegerValue(int fileOffset)
{
    int integerValue;

    integerValue = getShortValue(fileOffset);
    integerValue = integerValue * 256 + getByteValue(fileOffset + 2);
    integerValue = integerValue * 256 + getByteValue(fileOffset + 3);

    return integerValue;
}

When this is used on an integer value which shows up as 0x4455FF00 in a Hex Editor like HxD, the Java method however returns 0x4454FF00.

Oddly enough, getShortValue() is correct with 0x4455 and getByteValue() obviously as well.

Why does it differ? What went wrong? How do I fix it?

aioobe

Keep in mind that bytes are signed in Java (think about what happens if getByteValue(fileOffset) returns -100).

I suggest you use ByteBuffer and its getInt, getShort methods.

Your approach can be fixed by changing

public byte getByteValue(int fileOffset)
{
    return contents[fileOffset];
}

to

public int getByteValue(int fileOffset)
{
    return contents[fileOffset] & 0xFF;
}

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

Combining 4 Bytes To Integer

From Dev

Hex to integer (4 bytes)

From Dev

Hex to integer (4 bytes)

From Dev

Split random integer into 4 random bytes

From Dev

Java: 4 bytes to 32 bit integer

From Dev

How to reverse the 4 bytes of an unsigned integer?

From Dev

UNPACK() 4 bytes into integer PHP sockets

From Dev

How can I print 4 bytes as an integer

From Dev

How can I print 4 bytes as an integer

From Dev

Split random integer into 4 random bytes

From Dev

Java: 4 bytes to 32 bit integer

From Dev

memset bytewise instead of integer ( 4 bytes )

From Dev

Convert 4 bytes to integer using struct

From Dev

Combining two unsigned bytes to a single integer value using left-shift and bitwise-or

From Dev

Combining two unsigned bytes to a single integer value using left-shift and bitwise-or

From Dev

Why IPv4 address is in bytes and can not be in integer range?

From Dev

Read 2 bytes as integer

From Dev

Why is the sum of bytes integer?

From Dev

Convert bytes to integer

From Dev

Swapping 2 Bytes of Integer

From Dev

Converting bytes array to integer

From Dev

Swapping 2 Bytes of Integer

From Dev

Prolog convert integer to bytes

From Dev

Swap alternate bytes in a integer

From Dev

Combining integer and string in array value

From Dev

While doing fread() into an integer, what happens if file size is not a multiple of 4 bytes?

From Dev

How do I read out the first 4 bytes in javascript, turn it into an integer and remove the rest?

From Dev

Convert 4 bytes to integer using little-endian format (least signficant byte first)

From Dev

While doing fread() into an integer, what happens if file size is not a multiple of 4 bytes?

Related Related

HotTag

Archive