Decode hexadecimal to double

This is the code I am using to convert my double value into hexadecimal .
Now I need to decode this hexa decimal value to double/float in payload formatter.
Please let me know if there is any code available for it

uint64_t GetHexValue(long double f, unsigned bits, unsigned expBits)
{
	long double fNorm;
	int shift;
	long long sign, exp, significand;
	unsigned significandBits = bits - expBits - 1; // -1 for sign bit

	if (f == 0.0) return 0; // get this special case out of the way

	// check sign and begin normalization
	if (f < 0) { sign = 1; fNorm = -f; }
	else { sign = 0; fNorm = f; }

	// get the normalized form of f and track the exponent
	shift = 0;
	while(fNorm >= 2.0) { fNorm /= 2.0; shift++; }
	while(fNorm < 1.0) { fNorm *= 2.0; shift--; }
	fNorm = fNorm - 1.0;

	// calculate the binary form (non-float) of the significand data
	significand = fNorm * ((1LL<<significandBits) + 0.5f);

	// get the biased exponent
	exp = shift + ((1<<(expBits-1)) - 1); // shift + bias

	// return the final answer
	return (sign<<(bits-1)) | (exp<<(bits-expBits-1)) | significand;
}


You need to do a bit of reading, searching.

2 Likes

It’s preferable that you keep the same topic going rather than open a new one what’s very much related.

As well as the enormous body of knowledge on the forum, there is also:

https://www.thethingsnetwork.org/docs/devices/bytes/

You can disregard the pink warning box - it’s a generic article.

Top hint: Sending floats or doubles is problematic - multiply by the number of decimal places you need & turn it in to an integer.

1 Like

Hello
First of all ,I would like to thank all the people who have guided me to achieve my results by commenting on my post.
I am able to send the data successfully to TTN.
I am using LORA RN2483 module.
the response of the device is taking some time.is there any way to reduce the time of response .

Response of the AT commands or how often the device sends messages?

What is the device? How long is “taking some time”? 10 minutes, 10 seconds, 10 hours?

We will need far more detail to figure this out.

I am sending the commads specific to RN2483 module. example : “mac tx uncnf 1 45667”
The device is sending the data for every half a second i.e 500 mili second.(sometimes i might need to send data very quickly )
the response of the device takes 2 sec.
I need the data to be send to TTN as when my device is sending.
Is that possible?
please let me know.

Given that the minimum Rx1 time is 1 second and the default is 5 seconds on TTN, 2 seconds isn’t unreasonable.

No, because it is illegal (police/courts/fines/jail sort of issue) and totally in breach of the TTN Fair Use Policy. This is not a reasonable use case for LoRaWAN - the TTN FUP would allow 2 bytes every 133.4 seconds, you appear to be sending 2.5 bytes, so if we go to 3 bytes, you can send every 148.2 seconds, so you are about 300 times more than you should be sending.

LoRaWAN is the wrong technology for that requirement. Go WiFi or something like it.

The numbers @descartes quotes are based on optimal conditions, with less optimal conditions you are looking at a maximum of 20 transmissions a day.

1 Like

Also the RN2483 runs its own LoRaWAN s/w stack - which will also throttle your messages to try and prevent the breach of duty cycle, this may lead you to assume prblems with the device if not aware of LoRaWAN fundamentals and not undertanding what is going on under the lid - helps avoid the handcuffs and court appearance but its down to you to be responsible as a TTN user and comply with the TTN FUP limits…simply put follow the guidance and rules just given and massively reduce your TX rate.

thanks for the concern.
I am very new to this system that is the reason i have asked in the community.

yes Now i have realised the limits of lorawan.
i will opt for another technology to achieve my results

Iam using rn2483 connected to my private gateway. Iam able to only send the data to cloud but not able to receive back from the server. I have send the commands like “mac set ar on” to enable the automatic reply .
I am supposed to get the response of the device as “mac_rx_” but the response of the device is as “mac_tx_ok” which means there is only transmission but not the reception.
can you please guide me how to get the response back from the server

Which server?

What do you mean by private gateway - what is it connected to?

That is to get the module to send a reply if it receives a confirmed downlink or the frame pending bit is set - it doesn’t make the gateway or server send a response.

I am talking about TTN .

  • I am using “The Things Indoor Gateway LoRaWAN”.

  • how would i receive the response from server.

So not a private gateway, just one you own - if it’s on TTN, then everything you do needs to be within the Fair Use Policy - have you stopped sending data every ½second?

By sending a downlink - it’s all in the documentation which is linked bottom right of the console.

yes i have stopped it as soon as i got the information from you

I have already referred the document and i tried to send the bytes,I am supposed to receive the reply from the device as “mac_rx “”” but i am receiving just “mac_tx_ok”
what would be the reason for not receiving the data. i have looked in the community discussion. many have faced the problem but i have not seen any solution for it.

Excellent - because that’s how it works.

You send your uplink with mac_tx and if there is a downlink, you will get back a mac_rx with the port number and the data as hex. If there is no downlink, you’ll just get mac_tx_ok

Which means it’s working as expected.

What is the issue again?

Iam sorry it was a typo.
it has to ve mac_tx_ok not mac_rx.
sorry for the typo

I don’t understand, the documentation is clear, you get mac_tx_ok if the Tx is OK and mac_rx with data if the Tx was OK and you’ve got a downlink.

If you don’t get a downlink or it doesn’t hear the downlink or you haven’t scheduled a downlink, you won’t get a mac_rx response.