Storing a DEC value as HEX

Hia!
I am trying to take a decimal number, (1330431386) and convert it to hex (4F4CC59A) like the pwd byte
When i attempt to parse it, i ether get the Decimal number or “16” for some reason.
Any help would be greatly appreciated.

         byte pwd[] = {0x00, 0x00, 0x00, 0x00};
         uint32_t k = getkey(uid); //10 digit value
         uint8_t* ptr_2_k = (void*)&k;
         int khex = (k, HEX);
      Serial.print("KEY:"); Serial.println(k);
      Serial.print("KEH:"); Serial.println(khex);
          /// key is not in byte passowed form try
       nfc.ntag2xx_Authenticate(khex);
       /// How do i use the Byte array instead of khex??

Is there any way to take “330431386” and turn in into “4F4CC59A” and then plug it into “pwd = {0x00, 0x00, 0x00, 0x00};” so that it would be stored as “pwd = {0x4F, 0x4C, 0xC5, 0x9A};”? Thanks!

Here is the whole program if needed. Uses the Adafruit PN532 lib with some modifications.

#include <Wire.h>
#include <SPI.h>
#include "Adafruit_PN532.h"

// If using the breakout with SPI, define the pins for SPI communication.
#define PN532_SCK  (2)
#define PN532_MOSI (3)
#define PN532_SS   (4)
#define PN532_MISO (5)

// If using the breakout or shield with I2C, define just the pins connected
// to the IRQ and reset lines.  Use the values below (2, 3) for the shield!
#define PN532_IRQ   (2)
#define PN532_RESET (3)  // Not connected by default on the NFC Shield

// Uncomment just _one_ line below depending on how your breakout or shield
// is connected to the Arduino:

// Use this line for a breakout with a software SPI connection (recommended):
//Adafruit_PN532 nfc(PN532_SCK, PN532_MISO, PN532_MOSI, PN532_SS);

// Use this line for a breakout with a hardware SPI connection.  Note that
// the PN532 SCK, MOSI, and MISO pins need to be connected to the Arduino's
// hardware SPI SCK, MOSI, and MISO pins.  On an Arduino Uno these are
// SCK = 13, MOSI = 11, MISO = 12.  The SS line can be any digital IO pin.
//Adafruit_PN532 nfc(PN532_SS);

// Or use this line for a breakout or shield with an I2C connection:
Adafruit_PN532 nfc(PN532_IRQ, PN532_RESET);
#include <stdio.h>
#include <inttypes.h>
#include <string.h>

#ifndef ROTL
# define ROTL(x,n) (((uintmax_t)(x) << (n)) | ((uintmax_t)(x) >> ((sizeof(x) * 8) - (n))))
#endif

uint32_t cb[] = {
  0x6D835AFC, 0x7D15CD97, 0x0942B409, 0x32F9C923, 0xA811FB02, 0x64F121E8,
  0xD1CC8B4E, 0xE8873E6F, 0x61399BBB, 0xF1B91926, 0xAC661520, 0xA21A31C9,
  0xD424808D, 0xFE118E07, 0xD18E728D, 0xABAC9E17, 0x18066433, 0x00E18E79,
  0x65A77305, 0x5AE9E297, 0x11FC628C, 0x7BB3431F, 0x942A8308, 0xB2F8FD20,
0x5728B869, 0x30726D5A
};

void transform(uint8_t* ru)
{
  //Transform
  uint8_t i;
  uint8_t p = 0;
  uint32_t v1 = (((uint32_t)ru[3] << 24) | ((uint32_t)ru[2] << 16) | ((uint32_t)ru[1] << 8) | (uint32_t)ru[0]) + cb[p++];
  uint32_t v2 = (((uint32_t)ru[7] << 24) | ((uint32_t)ru[6] << 16) | ((uint32_t)ru[5] << 8) | (uint32_t)ru[4]) + cb[p++];

  for (i = 0; i < 12; i += 2)
  {
    uint32_t t1 = ROTL(v1 ^ v2, v2 & 0x1F) + cb[p++];
    uint32_t t2 = ROTL(v2 ^ t1, t1 & 0x1F) + cb[p++];
    v1 = ROTL(t1 ^ t2, t2 & 0x1F) + cb[p++];
    v2 = ROTL(t2 ^ v1, v1 & 0x1F) + cb[p++];
  }

  //Re-use ru
  ru[0] = v1 & 0xFF;
  ru[1] = (v1 >> 8) & 0xFF;
  ru[2] = (v1 >> 16) & 0xFF;
  ru[3] = (v1 >> 24) & 0xFF;
  ru[4] = v2 & 0xFF;
  ru[5] = (v2 >> 8) & 0xFF;
  ru[6] = (v2 >> 16) & 0xFF;
  ru[7] = (v2 >> 24) & 0xFF;
}

uint32_t getkey(uint8_t* uid)
{
  int i;
  //Rotate
  uint8_t r = (uid[1] + uid[3] + uid[5]) & 7; //Rotation offset
  uint8_t ru[8] = { 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 }; //Rotated UID
  for (i = 0; i < 7; i++)
    ru[(i + r) & 7] = uid[i];

  //Transform
  transform(ru);

  //Calc key
  uint32_t k = 0; //Key as int
  r = (ru[0] + ru[2] + ru[4] + ru[6]) & 3; //Offset
  for (i = 3; i >= 0; i--) 
    k = ru[i + r] + (k << 8);

  return k;
};

void setup(void) {
  Serial.begin(115200);
  Serial.println("Hello!");

  nfc.begin();

  uint32_t versiondata = nfc.getFirmwareVersion();
  if (! versiondata) {
    Serial.print("Didn't find PN53x board");
    while (1); // halt
  }
  // Got ok data, print it out!
  Serial.print("Found chip PN5"); Serial.println((versiondata>>24) & 0xFF, HEX); 
  Serial.print("Firmware ver. "); Serial.print((versiondata>>16) & 0xFF, DEC); 
  Serial.print('.'); Serial.println((versiondata>>8) & 0xFF, DEC);

  // configure board to read RFID tags
  nfc.SAMConfig();

  Serial.println("Waiting for an ISO14443A Card ...");
}

void loop(void) {
  uint8_t success;
  uint8_t uid[] = { 0, 0, 0, 0, 0, 0, 0 };  // Buffer to store the returned UID
  uint8_t uidLength;                        // Length of the UID (4 or 7 bytes depending on ISO14443A card type)
  uint8_t buf[16];


  success = nfc.readPassiveTargetID(PN532_MIFARE_ISO14443A, uid, &uidLength);

  if (success) {
    // Display some basic information about the card
    Serial.println("Found an ISO14443A card");
    Serial.print("  UID Length: ");Serial.print(uidLength, DEC);Serial.println(" bytes");
    Serial.print("  UID Value: ");
    nfc.PrintHex(uid, uidLength);
    Serial.println("");

    if (uidLength == 7)
    {
      uint8_t data[32];

      Serial.println("Seems to be an NTAG2xx tag (7 byte UID)");    

      
      // bullshit
      byte pwd[] = {0x00, 0x00, 0x00, 0x00};
      uint32_t k = getkey(uid); //10 digit value
      uint8_t* ptr_2_k = (void*)&k;
      int khex = (k, HEX);
      Serial.print("KEY:"); Serial.println(k);
      Serial.print("KEH:"); Serial.println(khex);
      /// key is not in byte passowed form try to fox
       nfc.ntag2xx_Authenticate(khex);
      for (uint8_t i = 0; i < 42; i++) 
      {
        success = nfc.ntag2xx_ReadPage(i, data);

        Serial.print("PAGE ");
        if (i < 10)
        {
          Serial.print("0");
          Serial.print(i);
        }
        else
        {
          Serial.print(i);
        }
        Serial.print(": ");
        // Display the results, depending on 'success'
        if (success) 
        {
          // Dump the page data
          nfc.PrintHexChar(data, 4);
        }
        else
        {
          Serial.println("Unable to read the requested page!");
        }
      }
      delay(5000);
      Serial.print("Writing");
      byte spool[] = {0x40, 0x0D, 0x03, 0x0A};
      nfc.ntag2xx_WritePage(14, spool);  
      delay(1000);
      Serial.print("Reading");
      nfc.ntag2xx_ReadPage(14, buf);
      nfc.PrintHexChar(buf, 4);
      Serial.print("Done!");
      delay(5000);
          

    }
    else
    {
      Serial.println("This doesn't seem to be an NTAG203 tag (UUID length != 7 bytes)!");
    }

    // Wait a bit before trying again
    Serial.println("\n\nSend a character to scan another tag!");
    Serial.flush();
    while (!Serial.available());
    while (Serial.available()) {
    Serial.read();
    }
    Serial.flush();    
  }
}

You are misunderstanding how numbers are stored - they are stored as numbers; showing them as hex vs decimal only happens when you display them.

What you actually want to do is split up a 32-bit unsigned number into bytes.

unsigned long thenumber=330431386; 
byte pwd[] = {0x00, 0x00, 0x00, 0x00};
pwd[3]=thenumber&255; 
thenumber=thenumber>>8;
pwd[2]=thenumber&255; 
thenumber=thenumber>>8;
pwd[1]=thenumber&255; 
thenumber=thenumber>>8;
pwd[0]=thenumber;

HEX is just a #define macro, which you can pass to print functions that take a number and the - specifically somewhere in the arduino core there's the line:
#define HEX 16
so Serial.print(number,HEX); is the same as Serial.print(number,16).

However, when you do something like
x=(number,HEX)
here, you're not calling a function that takes the base as a second argument - so that comma isn't marking the boundary between two arguments - it's just acting as the comma operator. The comma operator disregards the first operand, and returns the second one. So (x,y)=y. And since HEX is #defined as 16, (number,HEX) evaluates to HEX, which is 16.

Ah, i was confused on why i kept getting 16. Thanks for the explanation and the help, now my program works as intended. Thank you so much!

snupsplus:
Is there any way to take "330431386" and turn in into "4F4CC59A" and then plug it into "pwd = {0x00, 0x00, 0x00, 0x00};" so that it would be stored as "pwd = {0x4F, 0x4C, 0xC5, 0x9A};"?

The decimal number 330431386 is equal to 13B1FB9A in hexadecimal base and not 4F4CC59A.

Another approach to convert decimal number to hex format using union structure:

void setup()
{
  Serial.begin(9600);
  union
  {
      unsigned long x = 330431386;
      byte myData[4]; //holds: {0x13, 0xB1, 0xFB, 0x9A}; myData[0] = 9A
  }data;
  Serial.print(data.myData[3], HEX); //shows: 13
}

void loop() 
{
 
}
      byte myData[4]; //holds: {0x13, 0xB1, 0xFB, 0x9A}; myData[0] = 9A

The comment is contradictory.

Whandall:

      byte myData[4]; //holds: {0x13, 0xB1, 0xFB, 0x9A}; myData[0] = 9A

The comment is contradictory.

Should I adjust or delete it?

If you feel the OP should visualize the byte content it should show the correct bytes,
you could fixed that in the original post, the second part of the comment is superfluous IMHO.

Whandall:
If you feel the OP should visualize the byte content it should show the correct bytes,
you could fixed that in the original post, the second part of the comment is superfluous IMHO.

With myData[0] = 9A, I have wanted to mean that lower byte of the data is in the lower-most location of the array; but, it has appeared as that I am assigning something into location myData[0], which is totally meaningless (9A instead of 0x9A or 154). Why should I try to assign? It has already been assigned! I have understood now why you have objected on the comment field. Comment field should be accurate to the best possible level as it explains the purpose of the code. Probably, I would have saved my fate by writing
this: 'myData[0] location contains 9A (the lower-most byte)'.