Solved

Inserting byte values into byte array in Android Java

Posted on 2014-04-11
4
2,969 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
Comment Utility
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
Comment Utility
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 13

Author Closing Comment

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

Expert Comment

by:CEHJ
Comment Utility
:)
0

Featured Post

Top 6 Sources for Identifying Threat Actor TTPs

Understanding your enemy is essential. These six sources will help you identify the most popular threat actor tactics, techniques, and procedures (TTPs).

Join & Write a Comment

I don't know if many of you have made the great mistake of using the Cisco Thin Client model with the management software VXC. If you have then you are probably more then familiar with the incredibly clunky interface, the numerous work arounds, and …
Windows 7 does not have the best desktop search built in. This is something Windows 7 users have struggled with. You type something in, and your search results don’t always match what you are looking for, or it doesn’t actually work at all. There ar…
Viewers learn about the “for” loop and how it works in Java. By comparing it to the while loop learned before, viewers can make the transition easily. You will learn about the formatting of the for loop as we write a program that prints even numbers…
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 …

743 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

17 Experts available now in Live!

Get 1:1 Help Now