Omnos
Omnos

Reputation: 25

How to combine 2 values from the same char array?

I'm sending this message: 53010422230205222345 (hex), i want to parse it like so: 53,01,04,... and assign those values to a char array for further manipulation.

Currently I'm trying to convert the whole message to char array with the method ".toCharArray" which puts every character into its own element, thus the next step has to combine e.g. 5 and 3 to 53, this is where i'm stuck.

If i try:

for (int count = 0; count < chMsg.length; count = count + 2){
sb.append(chMsg[count]);
sb.append(chMsg[count+2]);

char results[] = sb.toString().toCharArray();
msg[count1].Msg = results[];
}

I get errors for "required: char, found: char []".

And for:

for (int count = 0; count < chMsg.length; count = count + 2){
msg[count1].Msg = chMsg[count] + chMsg[count + 1];
}

I get "required: char, found: int".

There has to be some more elegant/simple solution than what i'm trying? If what i'm trying is OK what do i have to alter for it to work?

Below is the whole code with both methods I tried.

public class MainActivity extends AppCompatActivity {

public String sendMsg;
public StringBuilder builder = new StringBuilder();
public StringBuilder sb = new StringBuilder();

int i;
int count1;

MsgArray msg[] = new MsgArray[sendMsg.length()];
DataClass[] data = new DataClass[arraySize];

@Override
protected void onCreate(Bundle savedInstanceState)
{
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    CreateArray();

    if (sendMsg != ""){
        sendMsg = "";
    }

    sendBtn.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            sendMsg = sendText.getText().toString();
            Conversion();
        }
    });
}

public void CreateArray(){
    for (i = 0; i < arraySize; i++) {
        data[i] = new DataClass();
    }

    for (i=0; i < sendMsg.length(); i++){
        msg[i] = new MsgArray();
    }
}

public void Conversion()
{
    count1 = 0;

    char[] chMsg = sendMsg.toCharArray();
    for (int count = 0; count < chMsg.length; count = count + 2){
        sb.append(chMsg[count]);
        sb.append(chMsg[count+2]);

        char results[] = sb.toString().toCharArray();
        msg[count1].Msg = results[];

        msg[count1].Msg = chMsg[count] + chMsg[count + 1];
    }

    if(sendMsg != "")
    {
        for (int a = 0; a < sendMsg.length(); a = a + 2){
            String s = sendMsg.substring(a, a + 2);
            int n = Integer.valueOf(s, 16);
            builder.append((char)n);
        }
        recieveText.setText("ASCII = " + builder.toString());
    }
}
}

Upvotes: 1

Views: 413

Answers (3)

Joop Eggen
Joop Eggen

Reputation: 109557

String holds Unicode, a char array where every char is 2 bytes, UTF-16 encoded. So for arbitrary byte arrays the memory doubles, there is a nonsensical conversion between some encoding and the bytes. And this conversion will go wrong for UTF-8 which will not allow any byte sequence.

The bytes you can get via:

byte[] bytes = new byte[chMsg.length / 2];
int byteI = 0;
for (int count = 0; count < chMsg.length; count = count + 2) {
    int b = Integer.parseInt(new String(chMsg, count, 2), 16); // 16 for hex
    bytes[byteI++] = (byte) b;
}

Should nevertheless a String be needed, one could use:

String s = new String(bytes, "ISO-8859-1");
byte[] bytes = s.getBytes("ISO-8859-1");

The syntax char xxx[] was introduced in java to be compatible with C/C++; in the early beginning. More conventional (and logical) is char[] xxx.

= results[] should simply be = results

Upvotes: 3

Sotiris S. Magionas
Sotiris S. Magionas

Reputation: 799

An alternate approach would be :

public void fromMessageToArray(String theMessage){

        //  let's suppose the length of the message is always EVEN

        String tempMessage = theMessage;
        String[] msg = new String[tempMessage.length()/2];

        for(int i=0; i<msg.length; i++){
            msg[i] = tempMessage.substring(0,2);    //  first two (2) digits go to our array
            tempMessage = tempMessage.substring(2);  //  omit first two (2) digits
        }
    }

You should use stringbuilders which i have not, and be careful thath the message length is even. This method simply takes the first two digits from your string and places them in a string array until your string is exhausted.

Upvotes: 0

Brother
Brother

Reputation: 2210

You can use regex for this, each 2 character:

String hex = "53010422230205222345";
String[] split = hex.split("(?<=\\G..)");

split[0]
split[1]

Prints:

53
01

Upvotes: 0

Related Questions