Solved

Inserting byte values into byte array in Android Java

Posted on 2014-04-11
4
3,361 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
[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
  • Learn & ask questions
  • 2
4 Comments
 
LVL 81

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 16

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

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
add projects t working set in maven 2 71
IBM TS2900 (3572) Tape Autoloader Java? 12 109
Coding for the first time 9 103
Selenium WebElement Login button findElement 9 22
In this article we will discuss all things related to StageFright bug, the most vulnerable bug of android devices.
Introduction This article is the last of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article covers our test design approach and then goes through a simple test case example, how …
Viewers will learn one way to get user input in Java. Introduce the Scanner object: Declare the variable that stores the user input: An example prompting the user for input: Methods you need to invoke in order to properly get  user input:
This tutorial explains how to use the VisualVM tool for the Java platform application. This video goes into detail on the Threads, Sampler, and Profiler tabs.

739 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