Solved

Inserting byte values into byte array in Android Java

Posted on 2014-04-11
4
3,093 Views
Last Modified: 2014-05-01
Example: I have a decimal value 43799883 which is 29C554B in hex.  How can I insert the hex correctly into byte array and starting at a specific byte array location?

Example:

int test = 43799883;
byte[ ] testArray = new byte[20];
testArray[10] = (byte) test;

Here, I want to start inserting the test value in byte starting at testArray location 10 and onward.  Since 29C554B is 4 bytes long, I was expecting to see:

testArray[10] = 29
testArray[11] = C5
testArray[12] = 54
testArray[14] = B

But the result was that the last two digits of the hexadecimal value (4B) was entered into testArray[0].

Please provide help with the correct code.

Thank you
0
Comment
Question by:Wayne88
  • 2
4 Comments
 
LVL 78

Expert Comment

by:David Johnson, CD, MVP
ID: 39995756
different cpu's have different byte ordering you were expecting most significant to least significant and you are getting least to most. This is referred to as Big or Little-Endian
Intel CPU you will get
0x02, 0x9c. 0x55, 0x4b
0
 
LVL 86

Accepted Solution

by:
CEHJ earned 200 total points
ID: 39995794
I was expecting to see:

testArray[10] = 29
testArray[11] = C5
testArray[12] = 54
testArray[14] = B

No - a hex value depicted as individual bytes needs to be an even number of digits. You missed the leading zero on 2. The last value is 4B, which is what you'll get when you cast an int to a byte.

What you're doing is easiest to do with java.nio.ByteBuffer, but i'm not sure if that exists in Android, but here's the harder way

    public static byte[] placeInt(byte[] bytes, int value, int index) {
        index += 3;
        for (int i = 0; i < 4; i++) {
            bytes[index - i] = (byte) (0xFF & (value >> (8 * i)));
        }
        return bytes;
    }

Open in new window

0
 
LVL 14

Author Closing Comment

by:Wayne88
ID: 40035377
Sorry for the late reply as my membership was expired.  Thanks for the help!
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 40035386
:)
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
Issue to mail 11 72
ArrayIndexOutOfBoundException 9 68
Basic Java Case or If-Else statement... 3 43
WORM compliant device  what is it? 12 56
Go is an acronym of golang, is a programming language developed Google in 2007. Go is a new language that is mostly in the C family, with significant input from Pascal/Modula/Oberon family. Hence Go arisen as low-level language with fast compilation…
Java functions are among the best things for programmers to work with as Java sites can be very easy to read and prepare. Java especially simplifies many processes in the coding industry as it helps integrate many forms of technology and different d…
Video by: Michael
Viewers learn about how to reduce the potential repetitiveness of coding in main by developing methods to perform specific tasks for their program. Additionally, objects are introduced for the purpose of learning how to call methods in Java. Define …
Hi friends,  in this video  I'll show you how new windows 10 user can learn the using of windows 10. Thank you.

920 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

16 Experts available now in Live!

Get 1:1 Help Now