0

I need for my app this int[] format as I wrote in the title.

I'm fetching the value out of a HashMap which is filled out of an XML file with this format [00, 00, 00, 00], but I need it for every int with 0xin front.

I tried to "convert" it via a for-loop:

for(int i = 0; i < value.length; i++){
        value[i] = "0x"  + value[i];
    }

but I cant convert from String to int this way.

Then I tried to change it directly in the XML file, but then my app crashes with an NPE.

Now I want to know if there is a solution to my problem.

EDIT:

For further explanation:

I tried this before, but it did not work:

public int[] getValue(Map map, String key) {
    Map keyMap = map;
    int[] value = {};

    @SuppressWarnings("rawtypes")
    Iterator iter = keyMap.entrySet().iterator();

    while (iter.hasNext()) {
        @SuppressWarnings("rawtypes")
        Map.Entry mEntry = (Map.Entry) iter.next();
        if (mEntry.getKey().equals(key)) {
            value = (int[]) mEntry.getValue();
        }
    }
    return value;
}

Then I tried one of the answers and added

for(int i = 0; i < value.length; i++){
        value[i] = Integer.valueOf(String.valueOf(value[i]), 16);
    }

before the return. Now it works... but I dont know why it works :(

10
  • 1
    Have you tried searching the internet for "java converting integer to string"? Commented Feb 17, 2014 at 10:40
  • I dont want a string, I want it like this: int[] int = {0x00, 0x00, 0x00, 0x00} Commented Feb 17, 2014 at 10:43
  • 5
    That's not possible, because int is int - hexadecimal prefixes are only for interpretation and not for calculation. Commented Feb 17, 2014 at 10:44
  • 4
    There is no difference between int[] a = {0x00, 0x00, 0x00, 0x00} and int[] a = {0,0,0,0} Commented Feb 17, 2014 at 10:47
  • 1
    I doubt that value[i] = Integer.valueOf(String.valueOf(value[i]), 16); works in general. The problem is that you already have a wrong value when you read the XML, sometimes even a NumberFormatException. You cannot repair that after the fact. Commented Feb 17, 2014 at 16:57

2 Answers 2

2

In any typed language int/number values have no format. The way Java stores int values is as 4 byte number using Two's complement, so 0 or 0x0 or 00 (octal) or 0b is the same value: 32 zeros in a binary word.

SECOND EDIT:

I think you are over-engineering. What you should use is a

Map<String,int[]> map;

then, if you write map.get(key), you'll obtain what you're looking for.

NEW EXPLANATION: The previous solution worked because when you read the XML stored the values as decimal ones:

String s="80";
int i= Integer.valueOf(s); //it stores 80 (decimal value)
int value= Integer.valueOf(String.valueOf(i),16); //It stores 128, or 0x80 (hex value)
Sign up to request clarification or add additional context in comments.

7 Comments

the key is just a String to better identify the value stored to it (for better reading purposes)
okay, the String.valueOf() was part of an answer which is now deleted (dont know why he deleted it). hm... as I now tried to delete the String.valueOf part, it does say The method valueOf(int) in the type Integer is not applicable for the arguments (int, int)
Yes, I was messing myself... let me try to understand again what you need :P
The problem I had was that converted a String out of an XML. String is 80 0 0 0 0 0 0 0. So through a XmlPullparser I fetched the data and stored it in a HashMap. Now I wanted to take via a key the value stored to it. So I fetched it and saved it to the value int array. The Problem why it did not work was, that in my pullparser I didnt convert it correctly to an hex int. But at the moment it works with the answer that was posted, but which is now deleted
@Fraggles Correct, the parsing must be done when reading the XML, using Integer.valueOf(mystring,16) to store the correct integer. You were storing decimal values, that's why you needed to re-evaluate them
|
1
for(int i = 0; i < value.length; i++){
    value[i] = Integer.valueOf(String.valueOf(value[i]), 16);
}

Try this. If you want the hex value. Or in case if you want hex string you can use below code.

Integer.toHexString(integerValue)

2 Comments

did not get that can you explain further ?
See my answer for more details

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.