-1

I got some important question:

I need to send a Hexadecimal value stored in a bye[] (max. 4 byte) witch i have typed in in my textview of my android phone application.

    mSendButton.setOnClickListener(new OnClickListener() { // clickonbutton to send data
        public void onClick(View v) {
            // Send a message using content of the edit text widget
            TextView view = (TextView) findViewById(R.id.edit_text_out); 
            byte[] message = view.getText().toString().getBytes();
            sendMessage(message); // message needs to be a byte []
        }
    });

for example when i type in 0x1020 and press send button i want to have a byte [] = {0x1020}.

The toString function (line 5) converts the raw incoming bytes into other values. A legal replacement would be:

     CharSequence values= view.getText();

It's imortant that the first 2 value's are 0x and after these there are 2 or 4 bytes (hex presentation) of data.

Thank you verry much for taking some time to help me !

A.student
  • 11
  • 1
  • 2

2 Answers2

0

Find a library that does this (in my classpath alone there are 10. Surely you can find one appropriate for your project):

org.springframework.security.crypto.codec.Hex.decode(someString);

public static byte[] decode(CharSequence s) { int nChars = s.length();

if (nChars % 2 != 0) {
    throw new IllegalArgumentException("Hex-encoded string must have an even number of characters");
}

byte[] result = new byte[nChars / 2];

for (int i = 0; i < nChars; i += 2) {
    int msb = Character.digit(s.charAt(i), 16);
    int lsb = Character.digit(s.charAt(i+1), 16);

    if (msb < 0 || lsb < 0) {
        throw new IllegalArgumentException("Non-hex character in input: " + s);
    }
    result[i / 2] = (byte) ((msb << 4) | lsb);
}
return result;
}
Christian Bongiorno
  • 5,150
  • 3
  • 38
  • 76
  • Hi Christian, is it possible to give me some references to download and add these library's ? I'm pretty new to java so i'm sorry. The only hit i found was this: http://static.springsource.org/spring-security/site/docs/3.1.x/apidocs/org/springframework/security/crypto/codec/Hex.html – A.student May 18 '13 at 01:05
  • The thing is, you're doing this on android and you don't want to pull in some heavy dependencies. If you're following the apache 2.0 license (or you can) then you can consider simply copying this code in. It's 2 functions. BTW: If you like my answer please upvote it – Christian Bongiorno May 20 '13 at 16:10
-1
  • Check whether the string starts with "0x" using message.startsWith("0x")
  • Remove the first two characters from the string
  • Use Integer.parseInt(message, 16): the 16 implies it will attempt to parse a hexadecimal number from the string
  • Store the result in a byte[]. Keep in mind that a single byte can only hold up to 127.

I hope this helps.

Edit: The answer to this question might help you storing the result in a byte array.

Community
  • 1
  • 1
Patrickdev
  • 2,341
  • 1
  • 21
  • 29
  • There is no indication that the String necessarily can be represented using a 32 bit value (what if it's a hash or input of an encrypted value?). In addition, you will also have to code for step 4 "Store the result..." int doesn't natively "store" in a byte[] – Christian Bongiorno May 18 '13 at 00:46
  • True Christian Bongiorno, and it did not worked for me: with input of 1234 i got value 4660 :( – A.student May 18 '13 at 00:56
  • I never implied that it can be natively stored in a byte array. I provided instructions on how to get started on solving the OP's issue. @A.student, 4660 is the representation of the number 0x1234 in the decimal numeral system. If it parsed 4660, it means it was successful. Did I not understand your question correctly? – Patrickdev May 18 '13 at 08:15