Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
Solved

# Inserting byte values into byte array in Android Java

Posted on 2014-04-11
Medium Priority
3,631 Views
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
Question by:Wayne88
[X]
###### Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

• Help others & share knowledge
• Earn cash & points
• 2

LVL 83

Expert Comment

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

CEHJ earned 800 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;
}
``````
0

LVL 18

Author Closing Comment

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

LVL 86

Expert Comment

ID: 40035386
:)
0

## Featured Post

Question has a verified solution.

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

Introduction This article is the first of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article explains our test automation goals. Then rationale is given for the tools we use to a…
In this post we will learn how to make Android Gesture Tutorial and give different functionality whenever a user Touch or Scroll android screen.
Viewers learn how to read error messages and identify possible mistakes that could cause hours of frustration. Coding is as much about debugging your code as it is about writing it. Define Error Message: Line Numbers: Type of Error: Break Down…
Viewers will learn about the different types of variables in Java and how to declare them. Decide the type of variable desired: Put the keyword corresponding to the type of variable in front of the variable name: Use the equal sign to assign a v…
###### Suggested Courses
Course of the Month7 days, 8 hours left to enroll