These are some of my experimentation codes I made while trying to do different variable type-casting in C language. It might be useful for future references, I guess.
Last active
February 4, 2021 04:22
-
-
Save ifindev/3329c02b7657edc09319784772a853bc to your computer and use it in GitHub Desktop.
C & C++ Exploratory Codes: Part-3
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <stdio.h> // for basic io | |
#include <stdint.h> // for uint data type | |
#include <inttypes.h> // for printing uint64_t | |
#include <float.h> // for printing float value | |
#include <string.h> // for memcpy | |
#define MSG_TS_LEN 5 | |
int Digs = DECIMAL_DIG; // for printing double value | |
// UWB Frame | |
uint8_t rx_buffer[7] = {0xC5, 0xA1, 0x1A, 0, 0, 0, 0}; | |
// Fungsi untuk mengubah nilai double menjadi uint64_t | |
void doubleToUint32(float* data_float, uint32_t* data32) { | |
memcpy(data32, data_float, sizeof(data_float)); | |
} | |
void uint32ToDouble(uint32_t* data32, float* data_float) { | |
memcpy(data_float, data32, sizeof(data32)); | |
} | |
void set_data32(uint32_t data, uint8_t* buff, uint8_t idx) { | |
for(int i = 0; i < 4; i++) { | |
*(buff + (i + idx)) = (data >> (i * 8)) & 0xFF; | |
} | |
} | |
uint32_t get_data32(uint8_t* buff, uint8_t idx) | |
{ | |
uint32_t data32 = 0; | |
for (int i = 3; i >= 0; i--) | |
{ | |
data32 <<= 8; | |
data32 |= *(buff + (i + idx)); | |
} | |
return data32; | |
} | |
int main() | |
{ | |
// time of flight data | |
float tof_raw = 1000.12/ 299792458; // Raw tof value. Nilai pembilang harus xx.yz (harus ada komanya) | |
uint32_t tof32_send; // sent uint64_t tof value | |
uint32_t tof32_recv; // received uint64_t tof value | |
float tof_recv; // received and converted tof value | |
// Change double to uint32 | |
doubleToUint32(&tof_raw, &tof32_send); | |
// Embed tof data to uwb frame | |
set_data32(tof32_send, rx_buffer, 3); | |
// get tof data from frame | |
tof32_recv = get_data32(rx_buffer, 3); | |
// // change tof data | |
uint32ToDouble(&tof32_recv, &tof_recv); | |
printf("tof: %.*e\n", Digs, tof_raw); | |
printf("tof: %" PRIu32 "\n", tof32_send); | |
printf("tof: %.*e\n", Digs, tof_recv); | |
printf("tof: %" PRIu32 "\n", tof32_recv); | |
return 0; | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <stdio.h> | |
#include <stdlib.h> | |
#include <stdint.h> | |
#include <inttypes.h> // for printing uint64_t | |
#include <float.h> // for printing float value | |
#include <string.h> // for memcpy | |
int Digs = DECIMAL_DIG; // for printing double value | |
#define SPEED_OF_LIGHT 299792458.0 | |
#define FLOAT_TOF_CONV 10e14 // Float to uint32 conversion factor can handle up to 1200 meters tof value | |
#define FLOAT_POS_CONV 10e4 // four digits after comma precision | |
#define FLOAT_TOF_DATA 0xFC | |
#define FLOAT_POS_DATA 0xCF | |
uint32_t floatToU32(float floatData) | |
{ | |
uint32_t data32; | |
if(floatData > 0.1) { | |
// position data | |
data32 = (uint32_t)(floatData * FLOAT_POS_CONV); | |
} else { | |
// tof data | |
data32 = (uint32_t)(floatData * FLOAT_TOF_CONV); | |
} | |
return data32; | |
} | |
float data32ToFloat(uint32_t data32, uint8_t floatType) | |
{ | |
float dataFloat = 0.0; | |
if(floatType == FLOAT_TOF_DATA) { | |
dataFloat = (float)(data32) / FLOAT_TOF_CONV; | |
} else if(floatType == FLOAT_POS_DATA) { | |
dataFloat = (float)(data32) / FLOAT_POS_CONV; | |
} else { | |
dataFloat = 0.0; | |
} | |
return dataFloat; | |
} | |
int main() | |
{ | |
float dist = 5; | |
float tof_sent = dist / SPEED_OF_LIGHT; | |
uint32_t tof32 = floatToU32(tof_sent); | |
float tof_recv = data32ToFloat(tof32, FLOAT_TOF_DATA); | |
printf("tof float sent: %.*e\n", Digs, tof_sent); | |
printf("tof uint32: %" PRIu32 "\n", tof32); | |
printf("tof float recv: %.*e\n", Digs, tof_recv); | |
int arr[5] = {1, 2, 3, 4, 5}; | |
for(int i = 0; i < sizeof(arr)/sizeof(*arr); i++) | |
{ | |
printf("arr[%d]: %d\n", i, arr[i]); | |
} | |
printf("\n\n"); | |
memset(arr, 0, sizeof(arr)); | |
for(int i = 0; i < sizeof(arr)/sizeof(*arr); i++) | |
{ | |
printf("arr[%d]: %d\n", i, arr[i]); | |
} | |
return 0; | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <stdio.h> | |
#include <stdlib.h> | |
#include <stdint.h> | |
#include <inttypes.h> | |
#define MSG_TS_LEN 5 | |
// Simulated timestamp value | |
uint64_t tx_ts = 99309042740; | |
// Simulated received frame from anchor | |
uint8_t rx_buffer[8] = {0xC5, 0xA1, 0x1A, 0, 0, 0, 0, 0}; | |
uint8_t ts_idx = 3; | |
// received anchor timestamp data | |
uint64_t anchor_tx_ts; | |
// function declarations | |
void printData(uint8_t* data); | |
void set_ts(uint64_t ts, uint8_t* frame, uint8_t ts_idx); | |
static uint64_t get_ts(uint8_t* data, uint8_t ts_idx); | |
int main() | |
{ | |
// Set timestamp value to data frame | |
set_ts(tx_ts, rx_buffer, ts_idx); | |
// Print the stored timestamp value | |
printData(rx_buffer); | |
// Get timestamp data from frame to 64 bits variable | |
anchor_tx_ts = get_ts(rx_buffer, ts_idx); | |
printf("anchor_tx_ts: %" PRIu64 "\n", anchor_tx_ts); | |
printf("anchor_tx_ts: 0x%" PRIx64 "\n", anchor_tx_ts); | |
return 0; | |
} | |
// Function definitions | |
void set_ts(uint64_t ts, uint8_t* data, uint8_t ts_idx) { | |
for(int i = 0; i < MSG_TS_LEN; i++) { | |
*(data + (i + ts_idx)) = (ts >> (i * 8)) & 0xFF; | |
} | |
} | |
void printData(uint8_t* data) { | |
for(int i = ts_idx; i < (ts_idx + MSG_TS_LEN); i++) { | |
printf("rx_buffer[%d]: %#x\n", i, *(data + i)); | |
} | |
} | |
uint64_t get_ts(uint8_t* data, uint8_t ts_idx) | |
{ | |
uint64_t ts = 0; | |
for (int i = 4; i >= 0; i--) | |
{ | |
ts <<= 8; | |
ts |= *(data + (i + ts_idx)); | |
} | |
return ts; | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <stdio.h> // for basic io | |
#include <stdint.h> // for uint data type | |
#include <inttypes.h> // for printing uint type | |
uint8_t data_buffer[7] = {0xC5, 0xA1, 0x1A, 0, 0, 0, 0}; | |
void set_data32(uint32_t data, uint8_t* buff, uint8_t idx) { | |
for(int i = 0; i < 4; i++) { | |
*(buff + (i + idx)) = (data >> (i * 8)) & 0xFF; | |
} | |
} | |
uint32_t get_data32(uint8_t* buff, uint8_t idx) | |
{ | |
uint32_t data32 = 0; | |
for (int i = 3; i >= 0; i--) | |
{ | |
data32 <<= 8; | |
data32 |= *(buff + (i + idx)); | |
} | |
return data32; | |
} | |
int main() { | |
// hex data | |
uint32_t hexdata = 0x281cb9f4; | |
// set 32-bits hexdata to frame | |
set_data32(hexdata, data_buffer, 3); | |
// get_data 32bits from frame | |
uint32_t rethex = get_data32(data_buffer, 3); | |
// Print the result | |
printf("hexdata: 0x%" PRIx32 "\n", hexdata); | |
printf("rethex : 0x%" PRIx32 "\n", rethex); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment