Help in Hex conversion

Hi all,
I have been stuck in at one point urgent help needed :confused:

I want to convert D9291475 this number to 751429D9 ..that is reverse in order ..how to do it?

akshay123:
Hi all,
I have been stuck in at one point urgent help needed :confused:

I want to convert D9291475 this number to 751429D9 ..that is reverse in order ..how to do it?

Urgent help? Probably not REALLY urgent or you would have shown your code and identified where you are stuck and shown the actual results you are getting.

Paul

#include <w5100.h>
#include "pins_arduino.h"

#define ID_LEN 34
#define DATA_PIN_0 4
#define DATA_PIN_1 5
#include <SPI.h>
#include <Ethernet.h>
unsigned long startTime;
unsigned long interval = 5000;
// Newer Ethernet shields have a MAC address printed on a sticker on the shield
byte mac[] = { 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED };
// if you don't want to use DNS (and reduce your sketch size)
// use the numeric IP instead of the name for the server:
IPAddress gateway( 192, 168, 10, 3);
IPAddress subnet( 255, 255, 255, 0 );
IPAddress dns1( 59, 144, 127, 16);
IPAddress ip(192, 168, 10, 11);
IPAddress server( 192, 168, 10, 191);
EthernetClient client;
char buf[100];
String data;
int start;
int ind1; // - locations
int ind2;
//////////////////////
volatile long extract;
volatile uint8_t *port_to_pcmask[] = { &PCMSK0, &PCMSK1, &PCMSK2 };
typedef void (*voidFuncPtr)(void);
volatile static voidFuncPtr PCintFunc[24] = { NULL };
volatile static uint8_t PCintLast[3];

////////////////////////////////////////
// attach an interrupt to a specific pin using pin change interrupts.
void PCattachInterrupt(uint8_t pin, void (*userFunc)(void), int mode)
{

  uint8_t bit = digitalPinToBitMask(pin);
  uint8_t port = digitalPinToPort(pin);
  uint8_t slot;
  volatile uint8_t *pcmask;

  if (mode != CHANGE)
  {
    return;
  }

  // map pin to PCIR register
  if (port == NOT_A_PORT)
  {
    return;
  }
  else
  {
    port -= 2;
    pcmask = port_to_pcmask[port];
  }

  slot = port * 8 + (pin % 8);
  PCintFunc[slot] = userFunc;

  // set the mask
  *pcmask |= bit;

  // enable the interrupt
  PCICR |= 0x01 << port;

}

////////////////////////////////////////
void PCdetachInterrupt(uint8_t pin)
{
  uint8_t bit = digitalPinToBitMask(pin);
  uint8_t port = digitalPinToPort(pin);
  volatile uint8_t *pcmask;

  // map pin to PCIR register
  if (port == NOT_A_PORT)
  {
    return;
  }
  else
  {
    port -= 2;
    pcmask = port_to_pcmask[port];
  }

  // disable the mask.
  *pcmask &= ~bit;

  // if that's the last one, disable the interrupt.
  if (*pcmask == 0)
  {
    PCICR &= ~(0x01 << port);
  }
}

////////////////////////////////////////
// common code for isr handler. "port" is the PCINT number.
// there isn't really a good way to back-map ports and masks to pins.
static void PCint(uint8_t port)
{
  uint8_t bit;
  uint8_t curr;
  uint8_t mask;
  uint8_t pin;

  // get the pin states for the indicated port.
  curr = *portInputRegister(port + 2);
  mask = curr ^ PCintLast[port];
  PCintLast[port] = curr;

  // mask is pins that have changed. screen out non pcint pins.
  if ((mask &= *port_to_pcmask[port]) == 0)
  {
    return;
  }

  // mask is pcint pins that have changed.
  for (uint8_t i = 0; i < 8; i++)
  {
    bit = 0x01 << i;
    if (bit & mask)
    {
      pin = port * 8 + i;
      if (PCintFunc[pin] != NULL)
      {
        PCintFunc[pin]();
      }
    }
  }
}

////////////////////////////////////////
SIGNAL(PCINT0_vect)
{
  PCint(0);
}

////////////////////////////////////////
SIGNAL(PCINT1_vect)
{
  PCint(1);
}

////////////////////////////////////////
SIGNAL(PCINT2_vect)
{
  PCint(2);
}

////////////////////////////////////////////////////////
// end of interrupt code
////////////////////////////////////////////////////////

volatile long reader1 = 0;
volatile int reader1Count = 0;

////////////////////////////////////////
void read0(void)
{
  if (digitalRead(DATA_PIN_0) == LOW)
  {
    reader1Count++;
    reader1 = reader1 << 1;
    // Serial.print("0");
  }
}

////////////////////////////////////////
void read1(void)
{
  if (digitalRead(DATA_PIN_1) == LOW)
  {
    reader1Count++;
    reader1 = reader1 << 1;
    reader1 |= 1;
    //Serial.print("1");
  }
}


void setup() {

  // Open serial communications and wait for port to open:

  PCattachInterrupt(DATA_PIN_0, read0, CHANGE);//Attachinterrupt D0
  PCattachInterrupt(DATA_PIN_1, read1, CHANGE);//Attachinterrupt D1
  pinMode(2, OUTPUT);
  pinMode(3, OUTPUT);

  delay(10);

  for (int i = 4; i < 5; i++)
  {
    pinMode(i, OUTPUT);
    digitalWrite(i, HIGH); // enable internal pull up causing a one
    digitalWrite(i, LOW);  // disable internal pull up causing zero and thus an interrupt
    pinMode(i, INPUT);
    digitalWrite(i, HIGH); // enable internal pull up
  }



  reader1 = 0;
  reader1Count = 0;



  Serial.begin(115200);
  Ethernet.begin(mac, ip, dns1, gateway, subnet);
  delay(2000);
  client.setTimeout(200);
  W5100.setRetransmissionTime(1000);
  W5100.setRetransmissionCount(2);
  Serial.println("Ready");
  Serial.println(F("Please Give Your Punch")); // what to do to test

}

void loop() {
  if (reader1Count >= ID_LEN)
  {

    Serial.println(F(""));
    Serial.println(F("---------------------"));
    Serial.print(F("data: "));

    extract = (reader1 >> 1) & 0xFFFFFFF;
    Serial.println(extract);
    sprintf(buf, "%10lu", extract);
    Serial.println(buf,HEX);
    delay(250);
    sendGET();

    reader1 = 0;
    reader1Count = 0;

  }
}



void sendGET()
{
  if (client.connect(server, 8484) == 1) { //starts client connection, checks for connection
    Serial.println(F("connected"));
    const unsigned long startTimestamp = micros();
    client.print("GET /api/Test/Getvalues/");
    client.print(buf);
    client.print("/123 HTTP/1.1\r\nHost: 192.168.10.191\r\nConnection: close\r\n\r\n");
    const unsigned long endTimestamp = micros();
    Serial.println(endTimestamp - startTimestamp);
    Serial.println(F("request sent"));
  }
  else {
    Serial.println(F("connection failed")); //error message if no client connect

    Serial.println();
  }

  String readString;
  String line ;
  int lines_received = 0;
  while (client.connected() && !client.available()) delay(1); //waits for data
  while (client.connected() && client.available()) { //connected or data available
    char c = client.read();

    readString.concat(c);

  }

  client.stop();
  Serial.println(F("client disconnected."));
  Serial.println("Data from server captured in readString:");
  Serial.println();
  Serial.print(readString); //prints readString to serial monitor
  Serial.println();
  start = readString.indexOf(" ");  //finds a unique start location
  ind1 = readString.indexOf('"', start + 2); //finds location of first -
  ind2 = readString.indexOf('"', ind1 + 1 ); //finds location of second -
  data = readString.substring(ind1 + 1, ind2); //captures data String
  Serial.print("my data is: ");
  Serial.println(data);
  char charBuf[50];
  data.toCharArray(charBuf, 1024) ;
  int length = strlen(charBuf);
  char end_char = charBuf[strlen(charBuf) - 1]; ;
  Serial.print(F("Received:"));
  Serial.println(charBuf);
  Serial.print(F("Received:"));
  Serial.println(end_char);
  Serial.println();
  if (end_char == '1') {
    Serial.println(F("Plz open the Turnstile"));
    // startTime  = millis();
    Serial.println(F("RELAY TURN ON"));
    digitalWrite (2, HIGH);
    delay(2000);


  }
  else {
    Serial.println(F("Wrong Entry"));
    digitalWrite (3, HIGH);
    delay(200);
    digitalWrite (3, LOW);

  }

  digitalWrite (2, LOW);
  Serial.println(F("RELAY TURN OFF"));

  Serial.println("End of readString");
  Serial.println("==================");
  Serial.println();
  readString = ""; //clear readString variable
  data = ""; //clear data variable
}

Buf variable outputting D9291475 But need to have 751429D9…How to do it?

Read from one array and write into another in the opposite order.

Or, use a union.

	union
	{
		struct
		{
			byte b1;
			byte b2;
			byte b3;
			byte b4;
		};
		uint32_t llong;
	} md;

	md.llong = 0xD9291475;	// 3,643,348,085
	uint32_t new_data = md.b1 << 24 | md.b2 << 16 | md.b3 << 8 | md.b4;
	println(new_data);		// 1,964,255,705 -> 751429D9

Thanks Sir.. :slight_smile: I will test

actually I am referring this above link to read only response body (excluding http headers )

Which is getting after two blank lines..I want to do that in that way ..is it possible with my code?

DKWatson:
Or, use a union.

 union

{
struct
{
byte b1;
byte b2;
byte b3;
byte b4;
};
uint32_t llong;
} md;

md.llong = 0xD9291475; // 3,643,348,085
uint32_t new_data = md.b1 << 24 | md.b2 << 16 | md.b3 << 8 | md.b4;
println(new_data); // 1,964,255,705 → 751429D9

code only printing 4 hex values not all the 8 hex values:

code only printing 4 values not 8 values:

Ready
Please Give Your Punch
D9291475
29D9
D9291475
29D9

code:

#include <SPI.h>
#include <Ethernet.h>
#include <utility\w5100.h>
#define ID_LEN 34
#define DATA_PIN_0 4
#define DATA_PIN_1 5
union
{
  struct
  {
    byte b1;
    byte b2;
    byte b3;
    byte b4;
    byte b5;
    byte b6;
    byte b7;
    byte b8;
  };
  volatile unsigned long llong;
} md;
uint32_t new_data;
byte mac[] = { 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED };
// if you don't want to use DNS (and reduce your sketch size)
// use the numeric IP instead of the name for the server:
IPAddress gateway(10, 10, 53, 1);
IPAddress subnet( 255, 255, 255, 0 );
IPAddress dns1( 10, 10, 10, 55);
IPAddress ip(10, 10, 53, 191);
IPAddress server(10, 10, 10, 60);
EthernetClient client;
//
//  md.llong = 0xD9291475;  // 3,643,348,085
//  uint32_t new_data = md.b1 << 24 | md.b2 << 16 | md.b3 << 8 | md.b4;
//  println(new_data);    // 1,964,255,705 -> 751429D9

/*
  pins_arduino.h is an extension to the interrupt support for arduino.
  add pin change interrupts to the external interrupts, giving a way
  for users to have interrupts drive off of any pin.
  Refer to avr-gcc header files, arduino source and atmega datasheet.

  Theory: all IO pins on Atmega168 are covered by Pin Change Interrupts.
  The PCINT corresponding to the pin must be enabled and masked, and
  an ISR routine provided.  Since PCINTs are per port, not per pin, the ISR
  must use some logic to actually implement a per-pin interrupt service.

  Pin to interrupt map:
  D0-D7 = PCINT 16-23 = PCIR2 = PD = PCIE2 = pcmsk2
  D8-D13 = PCINT 0-5 = PCIR0 = PB = PCIE0 = pcmsk0
  A0-A5 (D14-D19) = PCINT 8-13 = PCIR1 = PC = PCIE1 = pcmsk1
*/


////////////////////////////////////////////////////////
// interrupt code
////////////////////////////////////////////////////////

volatile uint8_t *port_to_pcmask[] = { &PCMSK0, &PCMSK1, &PCMSK2 };
typedef void (*voidFuncPtr)(void);
volatile static voidFuncPtr PCintFunc[24] = { NULL };
volatile static uint8_t PCintLast[3];

////////////////////////////////////////
// attach an interrupt to a specific pin using pin change interrupts.
void PCattachInterrupt(uint8_t pin, void (*userFunc)(void), int mode)
{

  uint8_t bit = digitalPinToBitMask(pin);
  uint8_t port = digitalPinToPort(pin);
  uint8_t slot;
  volatile uint8_t *pcmask;

  if (mode != CHANGE)
  {
    return;
  }

  // map pin to PCIR register
  if (port == NOT_A_PORT)
  {
    return;
  }
  else
  {
    port -= 2;
    pcmask = port_to_pcmask[port];
  }

  slot = port * 8 + (pin % 8);
  PCintFunc[slot] = userFunc;

  // set the mask
  *pcmask |= bit;

  // enable the interrupt
  PCICR |= 0x01 << port;

}
// wouter: this function is not used at all.

//////////////////////////////////////////
//void PCdetachInterrupt(uint8_t pin)
//{
//  //  uint8_t bit = digitalPinToBitMask(pin);
//  uint8_t port = digitalPinToPort(pin);
//
//  // wouter: this doesn't do anything outside of this function, so no need to make it volatile.
//  //  volatile uint8_t *pcmask;
//  uint8_t *pcmask;
//
//  // map pin to PCIR register
//  if (port == NOT_A_PORT)
//  {
//    return;
//  }
//  else
//  {
//    port -= 2;
//    pcmask = port_to_pcmask[port];
//  }
//
//  // disable the mask.
//  // wouter: no need to store this bit in a variable.
//  //  *pcmask &= ~bit;
//  *pcmask &= ~digitalPinToBitMask(pin);
//
//  // if that's the last one, disable the interrupt.
//  if (*pcmask == 0)
//  {
//    PCICR &= ~(0x01 << port);
//  }
//}

////////////////////////////////////////
// common code for isr handler. "port" is the PCINT number.
// there isn't really a good way to back-map ports and masks to pins.
static void PCint(uint8_t port)
{
  //  uint8_t bit;
  uint8_t curr;
  uint8_t mask;
  uint8_t pin;

  // get the pin states for the indicated port.
  curr = *portInputRegister(port + 2);
  mask = curr ^ PCintLast[port];
  PCintLast[port] = curr;

  // mask is pins that have changed. screen out non pcint pins.
  if ((mask &= *port_to_pcmask[port]) == 0)
  {
    return;
  }

  // mask is pcint pins that have changed.
  for (uint8_t i = 0; i < 8; i++)
  {
    // wouter: little optimisation.
    //    bit = 0x01 << i;
    //    if (bit & mask)
    if (mask & (1 << i))
    {
      pin = port * 8 + i;
      if (PCintFunc[pin] != NULL)
      {
        PCintFunc[pin]();
      }
    }
  }
}

////////////////////////////////////////
SIGNAL(PCINT0_vect)
{
  PCint(0);
}

////////////////////////////////////////
SIGNAL(PCINT1_vect)
{
  PCint(1);
}

////////////////////////////////////////
SIGNAL(PCINT2_vect)
{
  PCint(2);
}

////////////////////////////////////////////////////////
// end of interrupt code
////////////////////////////////////////////////////////

volatile unsigned long reader1 = 0;
volatile unsigned int reader1Count = 0;

////////////////////////////////////////
void read0(void)
{
  if (digitalRead(DATA_PIN_0) == LOW)
  {
    reader1Count++;
    reader1 = reader1 << 1;
    //Serial.print("0");
  }
}

////////////////////////////////////////
void read1(void)
{
  if (digitalRead(DATA_PIN_1) == LOW)
  {
    reader1Count++;
    reader1 = reader1 << 1;
    reader1 |= 1;
    // Serial.print("1");
  }
}



void setup()
{

  Serial.begin(9600);

  // Attach pin change interrupt service routines from the Wiegand RFID readers
  PCattachInterrupt(DATA_PIN_0, read0, CHANGE);
  PCattachInterrupt(DATA_PIN_1, read1, CHANGE);

  delay(10);

  // the interrupt in the Atmel processor misses out the first negative pulse as the inputs are already high,
  // so this gives a pulse to each reader input line to get the interrupts working properly.
  // Then clear out the reader variables.
  // The readers are open collector sitting normally at a one so this is OK
  for (int i = 4; i < 8; i++)
  {
    pinMode(i, OUTPUT);
    digitalWrite(i, HIGH); // enable internal pull up causing a one
    digitalWrite(i, LOW);  // disable internal pull up causing zero and thus an interrupt

    // wouter: can do this in one go.
    //    pinMode(i, INPUT);
    //    digitalWrite(i, HIGH); // enable internal pull up
    pinMode(i, INPUT_PULLUP);
  }

  reader1 = 0;
  reader1Count = 0;


  Serial.begin(115200);
  Ethernet.begin(mac, ip, dns1, gateway, subnet);
  delay(2000);
  client.setTimeout(200);
  W5100.setRetransmissionTime(1000);
  W5100.setRetransmissionCount(2);
  Serial.println("Ready");
  Serial.println(F("Please Give Your Punch"));

}


void loop()
{

  if (reader1Count >= ID_LEN)
  {
    md.llong = ((reader1 >> 1)) + 0x80000000;
    //volatile unsigned long TEST = ((reader1 >> 1) & 0xFFFFFF);

    Serial.println(md.llong, HEX);


    //
    new_data = md.b1 << 24 | md.b2 << 16 | md.b3 << 8 | md.b4;
    Serial.println(new_data, HEX);   // 1,964,255,705 -> 751429D9


    reader1 = 0;
    reader1Count = 0;
  }
}

Interesting. It works with the gcc compiler from the command line.

Sir you have taken directly Hex value and mine i am taking is long I think..is that reason?

This does work in IDE

    union
    {
        struct
        {
            byte b1 :8;
            byte b2 :8;
            byte b3 :8;
            byte b4 :8;
        };
        uint32_t llong;
    } md;

    md.llong = 0xD9291475;  // 3,643,348,085
    uint32_t new_data = md.b1;
    new_data <<= 8;
    new_data += md.b2;
    new_data <<= 8;
    new_data += md.b3;
    new_data <<= 8;
    new_data += md.b4;
    
    Serial.println(new_data, HEX);      // prints 751429D9
int n = 0xd9291475;
int nn =((n & 0xff000000) >> 24) + ((n &0xff0000) >> 8) + ((n & 0xff00) << 8) + ((n & 0xff) << 24);

or

unsigned int n = 0xd9291475;
unsigned int nn =(n >> 24) + ((n & 0xff0000) >> 8) + ((n & 0xff00) << 8) + (n  << 24);

@Dkwatson..Perfect ! Thank You

boolrules:

int n = 0xd9291475;

int nn =((n & 0xff000000) >> 24) + ((n &0xff0000) >> 8) + ((n & 0xff00) << 8) + ((n & 0xff) << 24);



or


unsigned int n = 0xd9291475;
unsigned int nn =(n >> 24) + ((n & 0xff0000) >> 8) + ((n & 0xff00) << 8) + (n  << 24);

unsigned long would be a better choice

RIght. I was in i686 mode mentally.