HEX - DEC Datatype issue

Hello,

i have a hard time understanding datatypes right now.

I’m working with the the Sparkfun canshield atm, i read out the canbus and log it on a pcduino in this format:

<536870910,32,0,0,100,192,255,253,70>
<ID(11 or 29bit),8 seperate bytes of data, all in dec format>

Afterwards i hand over the data line by line back to the arduino via serial.

If you want to hardcode data for the Seeedstudio can library manually you do it in HEX
Let’s say like this:

CanSend(0xDA0, 0x01, 0x80, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00);

My issue right now is that i don’t quite understand if it makes a difference if i hand over “0x” to a CAN bus/library or just the HEXvalues without the “0x”.

Secondly i have problems understanding how to find out if the base of a long is really 16.

In this small part of my code i am not sure if “hexaddress” for example is base 16 or not.

If i want to check the value over Serial.print(hexaddress) it’s formatted to dec but how do i actually check the base of a variable over serial or debug what values are really handed over to the library?

while(init_main_menu == false)
  { 
    main_menu_interrupt();
    
    while (Serial.available()>0)
    {
    long address;
    long hexaddress = 0xFFFFFFFFul;
    byte datatosend[8];
    char buf[9];
    
    if (Serial.read() == '<')
      {
      delay(50);
      byte i = 0;


      byte bytesIN = Serial.available();
      char IN[bytesIN];
      Serial.readBytesUntil('>',IN,bytesIN);
      IN[bytesIN]='\0';
      
      char *hold;
      hold = strtok(IN, ",>");;

      while(hold != NULL)
      {
        if (i == 0)
        {
          address = atol(hold);
        }
        else if (i!=0)
        {
          datatosend[i-1] = atoi(hold);
        }
        i++;
        hold = strtok(NULL, ",>");
      }
      }

.....

      ltoa(address,buf,16);
      Serial.println(buf);
      hexaddress = (long)strtoul(buf, NULL, 16);

    }
  }

Thanks for any input. I’m really stuck understanding this.

i have a hard time understanding datatypes right now.

So, you decide to drag a red herring in to confuse things? HEX and DEC have NOTHING to do with data types.

My issue right now is that i don't quite understand if it makes a difference if i hand over "0x" to a CAN bus/library or just the HEXvalues without the "0x".

If you create an int variable, with some value (whether that is 0x01, 1 or 01), the VALUE is sent when you use the proper function. If you want to send the value AS A STRING, then you have to KNOW whether the receiving function expects the 0x, or not.

      byte bytesIN = Serial.available();
      char IN[bytesIN];
      Serial.readBytesUntil('>',IN,bytesIN);
      IN[bytesIN]='\0';

Rubbish. If there are 4 bytes available to read, you need an array of 5 elements to store them AND the terminating NULL in.

The readBytesUntil() function normally blocks until the character of interest has arrived. The position of the character of interest in the data stream has NO relationship to the number of bytes that have already arrived (that available() tells you about).

      char *hold;
      hold = strtok(IN, ",>");;

There is NO reason to separate the declaration of the variable and the initialization of the variable.