Trouble converting values to string

I print the value of my integer

 Serial.print(code[i], HEX);

It looks like this: 36 00 78 2B 8D

I can't seem to convert it to a character value.

I tried doing:

char mqttCode[16];

mqttCode[16]=(code[i], HEX);

But this does not work. Help!?

I should note that it's actually 14 characters in length (10 numbers, 4 spaces), I think. I have been changing the values around to see if that's why I get nothing when I try to serial print the new variable.

 Serial.print(code[i],[glow] BYTE[/glow]);

?

Negative.

I think what I meant is, I need to convert and integer array to a character array.

char mqttCode[5];

 for (i=0; i<5; i++) {
          if (code[i] < 16) 
          Serial.print("0");
          Serial.print(code[i], HEX);
          Serial.print(" ");
          mqttCode[i]=(code[i], HEX);
}

For those of us who aren't very good at guessing: Can you give us an specific example with the following information?

  • What, exactly, is in the five bytes of the code array when you enter the loop?

  • What do those bytes represent to you?

  • What, exactly, do you want to print out for this example?

  • What, exactly, do you want to store in the five bytes of the mqttCode array

Regards,

Dave

You make a good point.

The code *array represents an RFID tag, 5 sets of 2 digits. * *I need to pass an ascii string to an mqtt publish command, so I need to convert the RFID number to a string variable, such as mqttCode. *

You make a good point

Apparently not, so here's my guess:

What, exactly are in the five bytes of the input array and what do they represent to you:

Example of my guess:


{0x01, 0x23, 0x45, 0x67, 0xab}

These are the bytes of an arbitrary 40-bit binary number, with most significant byte appearing first.

What do you want to print out


01 23 45 67 ab

What do you want to store in the output array so that you can print it as a string?


{'0','1','2','3,'4','5','6','7','a','b','\0'}

Note that it takes 11 bytes in the array so that we can print the ten chars.

Now, if I have guessed wrong, can you please tell me exactly what it is that you want to do?

Regards,

Dave

I don’t want to print out anything. I’m already doing that. I want to do as I said, “I need to pass an ascii string to an mqtt publish command”, so I need to convert code*, an integer array, to an ascii string representing the same digits.*
```
*#include <Ethernet.h>
#include <PubSubClient.h>

//generic mac address for arduino.  dead beaf-eater?
byte mac = {  
 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED };
 
//static ip needs to be assigned
byte ip = {
 192, 168, 10, XXX };
 
//broker ip
byte mqserver = {
 XXX, XXX, XXX, XXX };

//mqtt
void callback(char* topic, byte* payload,int length) {
}

PubSubClient mqclient(mqserver, 1883, callback);

//initialize leds
int greenPin = 3;
int redPin = 2;

//char variable to convert
char mqttString[15];

void setup() {
 Ethernet.begin(mac, ip);
 pinMode(greenPin, OUTPUT);
 Serial.begin(9600);                          
}

void loop () {
 byte i = 0;
 byte val = 0;
 byte code[6];
 byte checksum = 0;
 byte bytesread = 0;
 byte tempbyte = 0;

if(Serial.available() > 0) {
   //check for header
   if((val = Serial.read()) == 2) {                  
     bytesread = 0;
     //read 10 digit code + 2 digit checksum
     while (bytesread < 12) {                        
       if( Serial.available() > 0) {
         val = Serial.read();
         //if header or stop bytes before the 10 digit reading
         if((val == 0x0D)||(val == 0x0A)||(val == 0x03)||(val == 0x02)) {
           //stop reading
           break;        
           digitalWrite(redPin, HIGH);  
           delay(500);
           digitalWrite(redPin, LOW);              
         }

//do ascii/hex conversion:
         if ((val >= ‘0’) && (val <= ‘9’)) {
           val = val - ‘0’;
     
         }
         else if ((val >= ‘A’) && (val <= ‘F’)) {
           val = 10 + val - ‘A’;
           
         }

//every two hex-digits, add byte to code:
         if (bytesread & 1 == 1) {
           //make some space for this hex-digit by
           //shifting the previous hex-digit with 4 bits to the left:
           code[bytesread >> 1] = (val | (tempbyte << 4));
           //if we’re at the checksum byte
           if (bytesread >> 1 != 5) {            
             //calculate the checksum… (xor)    
             checksum ^= code[bytesread >> 1];      
           };
         }
         else {
           //store the first hex digit first
           tempbyte = val;                          
         };
         //ready to read next digit
         bytesread++;                                
       }
     }

//output to Serial:
     //if 12 digit read is complete
     if (bytesread == 12) {          
       digitalWrite(greenPin, HIGH);  
       delay(1000);
       digitalWrite(greenPin, LOW);
       Serial.print("5-byte code: “);
       for (i=0; i<5; i++) {
         if (code[i] < 16)
         Serial.print(“0”);
         //this prints 1B 2D 3F 4H HI
         Serial.print(code[i], HEX);
         Serial.print(” ");
        //THIS IS WHERE I’M HAVING TROUBLE mqttString[i]=(code[i], HEX);
       }
       mqclient.connect(“arduino”);
       if (mqclient.connected()) {
           digitalWrite(greenPin, HIGH);
           mqclient.publish(“broker/client/arduino/DEADBEEFFEED/Badge”, mqttString);
           delay(1000);
           digitalWrite(greenPin, LOW);  
       } else {
           digitalWrite(redPin, HIGH);  
           delay(1000);
           digitalWrite(redPin, LOW);  
       }
       Serial.println();

Serial.print(“Checksum: “);
       Serial.print(code[5], HEX);
       Serial.println(code[5] == checksum ? " – passed.” : " – error.”);
       Serial.println();
       delay(1000);                  
     }

bytesread = 0;
   }
 }
}*
```

Well, here’s how you can do the stuff that I guessed about:



Set up a pointer to the first position of the output array.

Here’s the loop for all of the bytes in the input array:

  Get the upper bits of a byte into the lower bits.
  Convert those four (binary) bits into a character '0'-'9','A'-'F'

  Store that character in the current position of the output array.

  Increment the pointer to the output array.

  Convert the four (binary) lower bits bits into a character '0'-'9','A'-'F'

  Store that character in the current position of the output array.

  Increment the pointer to the output array.

After the loop, store the terminating zero byte in the output array:



One possible implementation

void setup()
{
    Serial.begin(9600);
}

// Ten hex digits in a five-byte  array
byte code[5] = {0x01, 0x23, 0x45, 0x67, 0xab};

// Large enough to hold ten chars plus the terminating zero byte
char mqttCode[11];

void loop()
{
    int j = 0;
    Serial.print("Here are the bytes: ");
    for (int i = 0; i < 5; i++) {
        byte hinibble = (code[i] >> 4) & 0x0f;
        byte lonibble = code[i] & 0x0f;
        Serial.print(hinibble, HEX);
        Serial.print(lonibble, HEX);
        Serial.print(' ');
        mqttCode[j++] = bintohexascii(hinibble);
        mqttCode[j++] = bintohexascii(lonibble);1
    }
    mqttCode[j] = '\0';    
    
    Serial.print("\nHere is the string: ");
    Serial.println(mqttCode);
    Serial.println();
    delay(1000);
}
char bintohexascii(byte x)
{
    char hex[16] = {
        '0', '1', '2', '3', '4', '5', '6', '7',
        '8', '9', 'A', 'B', 'C', 'D', 'E', 'F'
    };
    return hex[x & 0x0f];
}

Another method might use sprintf() to put the char values of the hex bytes, but it would take 1400 or so additional bytes of program memory (unless you were already using sprintf for something else) and would deprive you of the opportunity to see a way to convert a four bit binary number to the hexadecimal ASCII char.

Output:


Here are the bytes: 01 23 45 67 AB 
Here is the string: 01234567AB

Regards,

Dave

Thanks Dave, I'm still learning - the more code examples I see, the more I start to understand.