How to combine 2 values from the same char array?

Omnos :

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());
    }
}
}
Joop Eggen :

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

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=324504&siteId=1