Solved

Inserting byte values into byte array in Android Java

Posted on 2014-04-11
4
3,173 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 79

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

Live: Real-Time Solutions, Start Here

Receive instant 1:1 support from technology experts, using our real-time conversation and whiteboard interface. Your first 5 minutes are always free.

Question has a verified solution.

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

Suggested Solutions

In a recent article here at Experts Exchange (http://www.experts-exchange.com/articles/18880/PaperPort-14-in-Windows-10-A-First-Look.html), I discussed my nine-month sandbox testing of the Windows 10 Technical Preview, specifically with respect to r…
In this post we will learn how to connect and configure Android Device (Smartphone etc.) with Android Studio. After that we will run a simple Hello World Program.
Viewers will learn about basic arrays, how to declare them, and how to use them. Introduction and definition: Declare an array and cover the syntax of declaring them: Initialize every index in the created array: Example/Features of a basic arr…
This tutorial will introduce the viewer to VisualVM for the Java platform application. This video explains an example program and covers the Overview, Monitor, and Heap Dump tabs.

776 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