Hi everyone,
I'm stuck with something that most probably is stupid and obvious, but I've dealt with it some long nights and could not figure it out.
Sorry in advance for the stupid-ness of the question and the trivial of the response.
Another thing I should probably prepare for, is the flame I'll be getting for being contentious and using the String class. I've been meaning to have the thing up and running the easy way and then tuning the code.
I'm trying to get an SMS PDU decode function and had trouble populating a String object. The code goes like:
String decodeMessage (String eMessage, int messageLenght) { // Gets the PDU and the length of the represented message, returns it.
Serial.println();
Serial.println("Entering decode function");
String dMessage;
dMessage.reserve(messageLenght+1); // First try
// char tempoString [25]; // Another try
byte newByte = 0;
byte originalByte = 0;
int counter = 1;
int currentPosition = messageLenght;
// Convert the received string Hex representation to actual bytes starting from the last 2 hex chars of the string.
unsigned int ln, hn;
Serial.print("Received pdu: ");
Serial.println(eMessage);
Serial.print("Size:");
Serial.println(eMessage.length());
for (int j = eMessage.length()-1; j >=0 ; j=j-2) {
if (eMessage.charAt(j) > '9') {
ln = eMessage.charAt(j) - 'A' + 10;
} else {
ln = eMessage.charAt(j) - '0';
}
if (eMessage.charAt(j-1) > '9') {
hn = eMessage.charAt(j-1) - 'A' + 10;
} else {
hn = eMessage.charAt(j-1) - '0';
}
originalByte = (hn << 4) | ln;
Serial.println(originalByte,HEX); //Got the original byte.
for (int i = 7; i>=0; i--) { //Start the newByte construction. Add 7 bits of each original byte.
//newByte = newByte | (bitRead(originalByte, i) >> counter); //This did not work, never figured out why....
bitWrite(newByte, 7-counter, bitRead(originalByte, i));
counter++;
if (counter == 8) { // We have filled up the byte. Lets close it, add it to the string and start with a new newByte.
Serial.println(newByte,BIN); // Seems all right. the Values I get for all the new bytes created is the message I'm expecting.
dMessage.setCharAt(currentPosition, (char)newByte); //First try
// dMessage += newByte; //another try
strcat(tempoString, (char)newByte); //yet another try
delay(100);
currentPosition--; // Go one char back on the dMessage.
newByte = 0; counter = 1;
}
}
}
// Serial.println(tempoString);
Serial.println(dMessage);
delay (300);
return dMessage;
}
The decode process starts from the last byte of the eMessage (encoded message).
I'm taking the 7 MSB of the last byte and copying them to the a new byte, having preserved the initial 0 as MSB .
I then want this new byte to be the last character of the String I'll be returning.
While the Serial.print(newByte, BIN) shows that the newBytes do get correctly created, there is nothing inside the String.
I have also tried using a char array (ignore the fact that appending is not done from the end, I just wanted to see if anything goes in there) but no go. (see also different wording in the comments...)
Just as an example I have the PDU:
D4F29C0E6AA3DDF976181486BF416928FAED2EBB00
Which represents the message:
Test mhnyma apo iPhone.
(thats Greek language written with latin characters, sorry about that) which is 23 chars long.
Any ideas what I'm missing?