Convert a decimal value in char to hex and store in the char

lot of repetitious code

and

consider using more sub-functions and arrays which may make your code more readable and easier to find bugs

char s [80];

struct Can_s {
    void beginExtendedPacket (unsigned long id) {
        sprintf (s, " %08lx", id);
        Serial.print (s);
    }
    void write (byte b)  {
        sprintf (s, " %02x", b);
        Serial.print (s);
    }
    void endPacket (void)  { Serial.println (); };
} CAN;

// -----------------------------------------------------------------------------
void
canSend (
    long  id,
    byte *buf,
    int   nByte )
{
    CAN.beginExtendedPacket(id);

    for (int i = 0; i < nByte; i++)
        CAN.write (buf [i]);

    CAN.endPacket();
}

// -----------------------------------------------------------------------------
void
longToByte (
    unsigned long  val,
    byte          *buf,
    int            offset )
{
    for (unsigned i = 0; i < sizeof(long); i++)  {
        buf [i]   = val & 0xFF;
        val     >>= 8;
    }
}

// -----------------------------------------------------------------------------
void setup() {
  Serial.begin (9600);

    byte gpsData [8];

    longToByte (2268365056, gpsData, 0);
    longToByte (0x12345678, gpsData, 4);
    canSend (0x18fef31c, gpsData, sizeof(gpsData));
}

void loop() {
}