-->
Page 1 of 1

Convert String to HEX

PostPosted: Sat Jun 06, 2015 8:41 pm
by Stephen
I'm having trouble with some strings when posting a MAC Address.
Code: Select all  WiFi.macAddress(mac);
  String PostData="submission={\"DeviceID\":\"";
    PostData=String(PostData+mac[5]);PostData=PostData+":";
         PostData=String(PostData+mac[4]);PostData=PostData+":";
         PostData=String(PostData+mac[3]);PostData=PostData+":";
         PostData=String(PostData+mac[2]);PostData=PostData+":";
         PostData=String(PostData+mac[1]);PostData=PostData+":";
         PostData=String(PostData+mac[0]);
    PostData=PostData+"\",";
    PostData=PostData+"\"RSSI\":\"";
    PostData=String(PostData+rssi);
    PostData=PostData+"\"}";
  Serial.println(PostData);


This returns as a series of numbers:
Code: Select allsubmission={"DeviceID":"217:126:163:52:254:24","RSSI":"-44"}


However, I believe it should be something much more akin to this:
Code: Select all00:0a:95:9d:68:16 (example only)


I tried including ",HEX" in the String(), unfortunately it fails to compile. I'm not getting an error, it simply hangs on compilation, as illustrated in the example code below:
Code: Select all PostData=String(PostData+mac[2],HEX);PostData=PostData+":";


I realize this is more of a Arduino question, less of a ESP8266 question -- but I was hoping someone could offer a simple fix. If anyone has experience combining strings and sees any improvements I could make, even that would be greatly appreciated.

Re: Convert String to HEX

PostPosted: Sun Jun 07, 2015 3:42 am
by tytower
Use += like this
PostData += mac[5]+":"+mac[4]+":"+mac[3]+":"+mac[2]+":"+mac[1]+":"+mac[0]+"\,";

See examples strings

byte mac[] = {
0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED}

Re: Convert String to HEX

PostPosted: Sun Jun 07, 2015 6:27 pm
by tytower
This might be useful
Code: Select all
void setup() {
  Serial.begin(9600);

}
void loop() {
  byte firstarray[] = {0x15, 0x10, 0x15};
  int secondarray[] = {2, 4, 6, 8, 10};
  char thirdarray[]  {'a', 'b', 'c', 'd', 'e'};
  double fourtharray[] {12.52 ,45.3 ,260.778 ,104 ,};
  printarray (firstarray,3);
  printarray (secondarray,5);
  printarray (thirdarray,5);
  printarray (fourtharray,4);
  delay(5000);
}
// arrays as parameters
void printarray (int arg[], int length) {
  for (int n=0; n<length; ++n){
    Serial.print(arg[n] ,' ');
    Serial.print(" ");  }   
Serial.println('\n');}

void printarray (byte arg[], int length) {
  for (int n=0; n<length; ++n){
    Serial.print(arg[n],' ');
    Serial.print(" "); }
  Serial.println('\n');}

void printarray (char arg[], int length) {
  for (int n=0; n<length; ++n){
    Serial.print(arg[n],' ');
    Serial.print(" ");}
  Serial.println('\n');}

void printarray (double arg[], int length) {
  for (int n=0; n<length; ++n){
    Serial.print(arg[n],4);
    Serial.print(" ");}
  Serial.println('\n');}




If you post your code above with the array definitions mainly I can probably help more specifically

Re: Convert String to HEX

PostPosted: Mon Jun 08, 2015 1:32 am
by tytower
This is as you have it
Code: Select allvoid setup() {
 
Serial.begin(9600);
}

void loop() {

  int rssi=56; 
byte mac[6]{0xDE,0xAD,0xBE,0xEF,0xFE,0xED};
 
 String PostData="submission={\"DeviceID\":\"";
         PostData += String(mac[0],HEX) += ":";
         PostData += String(mac[1],HEX) += ":";
         PostData += String(mac[2],HEX) += ":";
         PostData += String(mac[3],HEX) += ":";
         PostData += String(mac[4],HEX) += ":";
         PostData += String(mac[5],HEX);
    PostData += "\",";
    PostData += "\"RSSI\":\"";
    PostData += rssi;
    PostData += "\"}";
  Serial.println(PostData);

}