Decode GPS NMEA code using arduino - c++

I know this has been asked many times but I really can't find what I am really searching.
I am using an Arduino Uno and a GPS Shield that shows GPS data through serial.
Here is the code I am uploading to my Arduino to interface the GPS Shield:
void loop() // run over and over
{
while(!(mySerial.available())){}
Serial.write(mySerial.read());
}
That is just the code. Nevertheless, as it continuously loops, on a Serial Monitor, it also output GPS data every second.
Here is its output every second:
$GPGGA,013856.000,000.9090,N,9090.90,E,1,09,1.1,316.97,M,0.00,M,,*66
$GPGSA,A,3,07,08,11,1ÿ3,16,19,23,27,42,,,,2.8,1.1,2.5*3F
$GPRMC,013856.000,A,000.9090,N,9090.90,E,0.0,038.1,310814,,,A*62
$GPGSV,ÿ3,1,12,16,26,059,33,27,33,025,44,08,30,330,32,07,31,326,34*7A
$GPGSV,3,2,12,19,58,354,31,01,33,186,18,23,32,221,24,11,5ÿ9,198,31*70
$GPGSV,3,3,12,42,60,129,32,13,38,253,27,32,06,161,,31,01,140,*7E
As it updates every second, the coordinates changes to minimal, which means the GPS Shield is working.
The problem here is, I wanted to parse the GPS data, especially on the GPGGA line only, and ignore the other lines. I would like to parse the Status, Latitude, N/S Indicator, Longitude, and E/W Indicator.
I have searched for the NMEA Library (http://nmea.sourceforge.net/), but I have no idea how to use it.
Can someone please help me here? Thank you.

NMEA data is in a GPS-style (ddmm.ssss) format, Google wants it in Decimal Style (dd.mmssss), there is a coversion function at the bottom of the code for this step.
I wrote this because I don't like the large, complicated libraries to do simple little things, especially when I am trying to figure out how it works.
This parses the GLL sentence, but you can change the sentence it's looking for and rearrange the sections if needed.
String ReadString;
void setup() {
Serial.begin(9600); //Arduino serial monitor thru USB cable
Serial1.begin(9600); // Serial1 port connected to GPS
}
void loop() {
ReadString=Serial1.readStringUntil(13); //NMEA data ends with 'return' character, which is ascii(13)
ReadString.trim(); // they say NMEA data starts with "$", but the Arduino doesn't think so.
// Serial.println(ReadString); //All the raw sentences will be sent to monitor, if you want them, maybe to see the labels and data order.
//Start Parsing by finding data, put it in a string of character array, then removing it, leaving the rest of thes sentence for the next 'find'
if (ReadString.startsWith("$GPGLL")) { //I picked this sentence, you can pick any of the other labels and rearrange/add sections as needed.
Serial.println(ReadString); // display raw GLL data in Serial Monitor
// mine looks like this: "$GPGLL,4053.16598,N,10458.93997,E,224431.00,A,D*7D"
//This section gets repeated for each delimeted bit of data by looking for the commas
//Find Lattitude is first in GLL sentence, other senetences have data in different order
int Pos=ReadString.indexOf(','); //look for comma delimetrer
ReadString.remove(0, Pos+1); // Remove Pos+1 characters starting at index=0, this one strips off "$GPGLL" in my sentence
Pos=ReadString.indexOf(','); //looks for next comma delimetrer, which is now the first comma because I removed the first segment
char Lat[Pos]; //declare character array Lat with a size of the dbit of data
for (int i=0; i <= Pos-1; i++){ // load charcters into array
Lat[i]=ReadString.charAt(i);
}
Serial.print(Lat); // display raw latitude data in Serial Monitor, I'll use Lat again in a few lines for converting
//repeating with a different char array variable
//Get Lattitude North or South
ReadString.remove(0, Pos+1);
Pos=ReadString.indexOf(',');
char LatSide[Pos]; //declare different variable name
for (int i=0; i <= Pos-1; i++){
LatSide[i]=ReadString.charAt(i); //fill the array
Serial.println(LatSide[i]); //display N or S
}
//convert the variable array Lat to degrees Google can use
float LatAsFloat = atof (Lat); //atof converts the char array to a float type
float LatInDeg;
if(LatSide[0]==char(78)) { //char(69) is decimal for the letter "N" in ascii chart
LatInDeg= ConvertData(LatAsFloat); //call the conversion funcion (see below)
}
if(LatSide[0]==char(83)) { //char(69) is decimal for the letter "S" in ascii chart
LatInDeg= -( ConvertData(LatAsFloat)); //call the conversion funcion (see below)
}
Serial.println(LatInDeg,15); //display value Google can use in Serial Monitor, set decimal point value high
//repeating with a different char array variable
//Get Longitude
ReadString.remove(0, Pos+1);
Pos=ReadString.indexOf(',');
char Longit[Pos]; //declare different variable name
for (int i=0; i <= Pos-1; i++){
Longit[i]=ReadString.charAt(i); //fill the array
}
Serial.print(Longit); //display raw longitude data in Serial Monitor
//repeating with a different char array variable
//Get Longitude East or West
ReadString.remove(0, Pos+1);
Pos=ReadString.indexOf(',');
char LongitSide[Pos]; //declare different variable name
for (int i=0; i <= Pos-1; i++){
LongitSide[i]=ReadString.charAt(i); //fill the array
Serial.println(LongitSide[i]); //display raw longitude data in Serial Monitor
}
//convert to degrees Google can use
float LongitAsFloat = atof (Longit); //atof converts the char array to a float type
float LongInDeg;
if(LongitSide[0]==char(69)) { //char(69) is decimal for the letter "E" in ascii chart
LongInDeg=ConvertData(LongitAsFloat); //call the conversion funcion (see below
}
if(LongitSide[0]==char(87)) { //char(87) is decimal for the letter "W" in ascii chart
LongInDeg=-(ConvertData(LongitAsFloat)); //call the conversion funcion (see below
}
Serial.println(LongInDeg,15); //display value Google can use in Serial Monitor, set decimal point value high
//repeating with a different char array variable
//Get TimeStamp - GMT
ReadString.remove(0, Pos+1);
Pos=ReadString.indexOf(',');
char TimeStamp[Pos]; //declare different variable name
for (int i=0; i <= Pos-1; i++){
TimeStamp[i]=ReadString.charAt(i); //fill the array
}
Serial.print(TimeStamp); //display raw longitude data in Serial Monitor, GMT
Serial.println("");
}
}
//Conversion function
float ConvertData(float RawDegrees)
{
float RawAsFloat = RawDegrees;
int firstdigits = ((int)RawAsFloat)/100; // Get the first digits by turning f into an integer, then doing an integer divide by 100;
float nexttwodigits = RawAsFloat - (float)(firstdigits*100);
float Converted = (float)(firstdigits + nexttwodigits/60.0);
return Converted;
}

I wrote this decent code, and it works up to two decimal places.
Code:
String gpsData;
String LATval = "######";
String LNGval = "######";
char inChar;
String gpsData;
String latt;
String la;
String lonn;
String lo;
float lattt;
float lonnn;
int latDeg;
int lonDeg;
float latMin;
float lonMin;
float latttt;
float lonnnn;
String sGPRMC;
void setup() {
Serial.begin(9600);
}
void loop() {
while (Serial.available()) {
inChar = Serial.read();
gpsData += inChar;
if (inChar == '$') {
gpsData = Serial.readStringUntil('\n');
break;
}
}
Serial.println(gpsData);
sGPRMC = gpsData.substring(0, 5);
if (sGPRMC == "GPRMC") {
Serial.flush();
latt = gpsData.substring(18, 28);
la = gpsData.substring(29, 30);
lonn = gpsData.substring(31, 42);
lo = gpsData.substring(43, 44);
Serial.print("latt:");
Serial.println(latt);
Serial.print("la:");
Serial.println(la);
Serial.print("lonn:");
Serial.println(lonn);
Serial.print("lo:");
Serial.println(lo);
lattt = latt.toFloat();
lonnn = lonn.toFloat();
Serial.print("lattt:");
Serial.println(lattt);
Serial.print("lonnn:");
Serial.println(lonnn);
if (la == "N" and lo == "E") {
latDeg = float(int(lattt / 100));
latMin = float(lattt - (latDeg * 100));
latMin = latMin / 60;
lonDeg = float(int(lonnn / 100));
lonMin = float(lonnn - (lonDeg * 100));
lonMin = lonMin / 60;
latttt = latDeg + latMin;
lonnnn = lonDeg + lonMin;
LATval = String(latttt);
LNGval = String(lonnnn);
Serial.print("latDeg:");
Serial.println(latDeg);
Serial.print("latMin:");
Serial.println(latMin);
Serial.print("lonDeg:");
Serial.println(lonDeg);
Serial.print("lonMin:");
Serial.println(lonMin);
Serial.print("LATval:");
Serial.println(LATval);
Serial.print("LNGval:");
Serial.println(LNGval);
}
}
}

I have searched the Internet, and the best answer would be using the "TinyGPS++" library for Arduino. Almost all GPS-related codes are already included on the Library.

You can use TinyGPS to parse the NMEA strings. If you are interested in only 1 sentence. You can write a custom parser as below for that sentence only.
int handle_byte(int byteGPS) {
buf[counter1] = byteGPS;
//Serial.print((char)byteGPS);
counter1++;
if (counter1 == 300) {
return 0;
}
if (byteGPS == ',') {
counter2++;
offsets[counter2] = counter1;
if (counter2 == 13) {
return 0;
} } if (byteGPS == '*') {
offsets[12] = counter1; }
// Check if we got a <LF>, which indicates the end of line if (byteGPS == 10) {
// Check that we got 12 pieces, and that the first piece is 6 characters
if (counter2 != 12 || (get_size(0) != 6)) {
return 0;
}
// Check that we received $GPRMC
// CMD buffer contains $GPRMC
for (int j=0; j<6; j++) {
if (buf[j] != cmd[j]) {
return 0;
}
}
// Check that time is well formed
if (get_size(1) != 10) {
return 0;
}
// Check that date is well formed
if (get_size(9) != 6) {
return 0;
}
SeeedOled.setTextXY(7,0);
for (int j=0; j<6; j++) {
SeeedOled.putChar(*(buf+offsets[1]+j));
}
SeeedOled.setTextXY(7,7);
for (int j=0; j<6; j++) {
SeeedOled.putChar(*(buf+offsets[9]+j));
}
// TODO: compute and validate checksum
// TODO: handle timezone offset
return 0; }
return 1; }

Try this which can help you
#include <SoftwareSerial.h>
#include <TinyGPS.h>
TinyGPS gps;
SoftwareSerial ss(3,4);
static void smartdelay(unsigned long ms);
static void print_float(float val, float invalid, int len, int prec);
static void print_int(unsigned long val, unsigned long invalid, int len);
static void print_date(TinyGPS &gps);
static void print_str(const char *str, int len);
void setup()
{
Serial.begin(9600);
ss.begin(9600);
}
void loop()
{
float flat, flon;
unsigned short sentences = 0, failed = 0;
gps.f_get_position(&flat, &flon);
Serial.print("LATITUDE: ");
print_float(flat, TinyGPS::GPS_INVALID_F_ANGLE, 10, 6);
Serial.println(" ");
Serial.print("LONGITUDE: ");
print_float(flon, TinyGPS::GPS_INVALID_F_ANGLE, 11, 6);
Serial.println(" ");
Serial.print("altitude: ");
print_float(gps.f_altitude(), TinyGPS::GPS_INVALID_F_ALTITUDE, 7, 2);
Serial.println(" ");
Serial.print("COURSE:");
print_float(gps.f_course(), TinyGPS::GPS_INVALID_F_ANGLE, 7, 2);
Serial.println("");
Serial.print("DIRECTION: ");
int d;
print_str(gps.f_course() == TinyGPS::GPS_INVALID_F_ANGLE ? "*** " : TinyGPS::cardinal(gps.f_course()), 6);
d=gps.f_course();
Serial.println();
Serial.println();
smartdelay(1000);
}
static void smartdelay(unsigned long ms)
{
unsigned long start = millis();
do
{
while (ss.available())
gps.encode(ss.read());
} while (millis() - start < ms);
}
static void print_float(float val, float invalid, int len, int prec)
{
if (val == invalid)
{
while (len-- > 1)
Serial.print('*');
Serial.print(' ');
}
else
{
Serial.print(val, prec);
int vi = abs((int)val);
int flen = prec + (val < 0.0 ? 2 : 1); // . and -
flen += vi >= 1000 ? 4 : vi >= 100 ? 3 : vi >= 10 ? 2 : 1;
for (int i=flen; i<len; ++i)
Serial.print(' ');
}
smartdelay(0);
}
static void print_int(unsigned long val, unsigned long invalid, int len)
{
char sz[32];
if (val == invalid)
strcpy(sz, "*******");
else
sprintf(sz, "%ld", val);
sz[len] = 0;
for (int i=strlen(sz); i<len; ++i)
sz[i] = ' ';
if (len > 0)
sz[len-1] = ' ';
Serial.print(sz);
smartdelay(0);
}
static void print_str(const char *str, int len)
{
int slen = strlen(str);
for (int i=0; i<len; ++i)
Serial.print(i<slen ? str[i] : ' ');
smartdelay(0);
}

Related

Arduino C/C++ Convert binary string to decimal

I wanted to post this on the Arduino forum but couldn't find any "new post" button...
Anyway, I wrote this function to convert a binary string into an int/long.
However, it doesn't always work with big numbers.
The following code is supposed to return "888888" but returns "888.887"
void setup() {
Serial.begin(9600);
String data = "11011001000000111000"; //888888
Serial.print(binStringToInt(data));
}
unsigned long binStringToInt(String bin) {
unsigned long total = 0;
int binIndex = 0;
for (int i = bin.length() - 1; i > - 1; i--) {
total += round(pow(2, binIndex)) * (bin.charAt(i) - '0');
binIndex++;
}
return total;
}
You can use a simpler function to achieve that:
long binary_to_int(char *binary_string){
long total = 0;
while (*binary_string)
{
total *= 2;
if (*binary_string++ == '1') total += 1;
}
return total;
}
void setup(){
Serial.begin(9600);
String data = "11011001000000111000";
Serial.print(binary_to_int(data.c_str())); // prints 888888
}
I used.c_str() to get a char * to Arduino String.
When programming Arduino rather forget about String and vectors, use C strings and C arrays as it is very resources limited.
Never use floats and float functions when dealing with integers.
unsigned long long convert(const char *str)
{
unsigned long long result = 0;
while(*str)
{
result <<= 1;
result += *str++ == '1' ? 1 : 0;
}
return result;
}

Using scientific prefixes

I have a device that calculates resistances in the range of 10uΩ upto 200KΩ. I want to write a piece of code to convert these numbers into a simpler to read format; e.g: convert 99912.3125 to 99.91KΩ and 0.010039 to 10.04mΩ. so far I have written this code which works ok for the most part:
float num[9] = {100000, 10000, 1000, 100, 10, 1, 0.1, 0.01, 0.001};
float nums[9] = {99999.9,9999.9,999.9,99.9,9.9,0.9,0.09,0.009,0.0009};
String prefixes[3] = {"mΩ","Ω","KΩ"};
int powers[3] = {-3,0,3};
void setup() {
Serial.begin(9600);
}
int prefix(int x){
return (x < 0) ? 0 : (x/3)+1;
}
int decimalPlaces(int x){
return (x < 0) ? abs(x) : 3-(int(x)%3);
}
void loop() {
for (int i=0; i<9; i++){
Serial.print(num[i],6);
Serial.print("\t");
if (i>4){
Serial.print("\t");
}
int lg = log10(num[i]);
int pref = prefix(lg);
Serial.print(" | ");
Serial.print(num[i]/pow(10,powers[pref]),decimalPlaces(lg));
Serial.println(prefixes[pref]);
}
Serial.println("##############################");
for (int i=0; i<9; i++){
Serial.print(nums[i],6);
Serial.print("\t");
if (i>3){
Serial.print("\t");
}
int lg = log10(nums[i]);
int pref = prefix(lg);
Serial.print(" | ");
Serial.print(nums[i]/pow(10,powers[pref]),decimalPlaces(lg));
Serial.println(prefixes[pref]);
}
Serial.println("##############################");
Serial.println(int(log10(0.1)));
Serial.println(int(log10(0.9)));
Serial.println(int(log10(1)));
while(true);
}
And here is the output of the code:
but there is an issue with changing between milli ohm and the ohm range. As you can see log10(0.9) is the same as log10(1), despite being on the same order of magnitude as 0.1! so instead of 0.9 being displayed as 900mΩ, it is displayed as 0.900Ω which is not what I intended to do. Is there an easier way to achieve this type of formatting? if not how can I solve the issue for this small bug?

Why is strtoul returning 0 from a "1" string?

I'm trying to compact a raster file in a way that is easy to read without GDAL library (my web server cannot install GDAL). Following this question, I'm doing the following to convert a raster's bytes (only 0 and 1 values) to bits:
int main(int argc,char *argv[]) {
if (argc < 3) {
return 1;
}
GDALDataset *poDataset;
GDALAllRegister();
poDataset = (GDALDataset*)GDALOpen(argv[1],GA_ReadOnly);
if (poDataset == NULL) {
return 2;
}
int tx=poDataset->GetRasterXSize(), ty=poDataset->GetRasterYSize();
GDALRasterBand *poBand;
int nBlockXSize,nBlockYSize;
poBand = poDataset->GetRasterBand(1);
printf("Type: %s\n",GDALGetDataTypeName(poBand->GetRasterDataType()));
// Type: Byte
poBand->GetBlockSize(&nBlockXSize,&nBlockYSize);
int i, nX = tx/nBlockXSize, nY = ty/nBlockYSize;
char *data = (char*)CPLMalloc(nBlockXSize*nBlockYSize + 1);
uint32_t out[nBlockXSize*nBlockYSize/32];
char temp;
CPLErr erro;
FILE* pFile;
pFile = fopen(argv[2],"wb");
for (y=0; y<nY; y++) {
for (x=0; x<nX; x++) {
erro = poBand->ReadBlock(x,y,data);
if (erro > 0) {
return 3;
}
for (i=0; i<nBlockXSize*nBlockYSize; i+=32) {
temp = data[i+32];
data[i+32] = 0;
out[i/32] = strtoul(&data[i],0,2);
if (data[i] != 0) {
printf("%u/%u ",data[i],out[i/32]);
}
data[i+32] = temp;
}
ch = getchar(); // for debugging
}
fwrite(out,4,nBlockXSize*nBlockYSize/32,pFile);
}
fclose(pFile);
CPLFree(data);
return 0;
}
After the first set of bytes is read (for (i=0; i<nBlockXSize*nBlockYSize; i+=32)), I can see that printf("%u/%u ",data[i],out[i/32]); is printing some "1/0", meaning that, where my raster has a 1 value, this is being passed to strtoul, which is returning 0. Obviously I'm messing with something (pointers, probably), but can't find where. What am I doing wrong?
strtoul is for converting printable character data to an integer. The string should contain character codes for digits, e.g. '0', '1' etc.
Apparently in your case the source data is actually the integer value 1 and so strtoul finds there are no characters of the expected form and returns 0 .

Does arduino count++ has a limit and how to fix it?

I needed to make a meters counter for a work thing, so I decided to just Arduino for it. I found an old encoder, found/wrote a simple code and hacked it all together and encountered a unexpected problem.
For some reason my counter won't count past around 8 meters or 31991 encoder pulses. Once it reaches this 8m limit, the number turns negative and starts counting backwards like -7.9 > -7.8 (i.e. continues counting upward towards 0).
Then it reaches zero and again counts to 8...
This is very strange to me and my limited coding knowledge can't fix it.
Does anyone know how to fix this or what I could do to make it work?
#include <LiquidCrystal.h>
#define inputA_in 6
#define inputB_in 7
LiquidCrystal lcd(12, 11, 5, 4, 3, 2);
int inputA_V = 0;
int inputB_V = 0;
int inputA = 0;
int inputB = 0;
int counter = 0;
// smeni vrednost tuka pred run
int console_frequency_milliseconds = 200; /// edna sekunda
int aLastState = 0;
int bLastState = 0;
float meters = 0.0;
unsigned long lasttime = 0;
int move_positive = 0;
int move_negative = 0;
int maximum_input_digital_v = 300; //treba da citash od konzola i da gi setirash max i min
int minimum_input_digital_v = 0;
int logical_threshold_v = 150; //brojkive se random staveni
void setup() {
pinMode (inputA_in, INPUT);
pinMode (inputB_in, INPUT);
Serial.begin (9600);
lcd.begin(16, 2);
// Print a message to the LCD
lcd.print("Metraza [m]");
aLastState = inputA;
bLastState = inputB;
lasttime = 0;
}
void loop () {
inputA = digitalRead(inputA_in);
if (inputA != aLastState) {
if (digitalRead(inputB_in) != inputA) {
counter ++;
aLastState = inputA;
} else {
counter --;
aLastState = inputA;
}
}
if (millis() - console_frequency_milliseconds > lasttime)//Detect once every 150ms
{
meters = 0.50014 * counter / 2000;
Serial.print("Position: ");
Serial.println(meters);
lasttime = millis();
lcd.setCursor(0, 1);
//Print a message to second line of LCD
lcd.print(meters);
}
}
Your counter is a simple int,
int counter = 0;
It seems that on your system they are only 16bit wide (with a maximum value of 32767), not surprising.
Use
long int counter = 0;
to get wider variables.
You might also want to change the calculation from
meters = 0.50014 * counter / 2000;
to
meters = 0.50014 * counter / 2000.0;
to avoid losing precision and range. Even with an int that would extend your range from 31991 encoder pulses to 32757 encoder pulses; and analog for the wider range.
You might also like to try changing the counter to an unsigned int or unsigned long int. I did not analyse your whole code, but I think you do not have anything which relies on representation of negative numbers. So you probably could double the range again. But no guarantees, subject to testing.

Using c++ is it possible to convert an Ascii character to Hex?

I have written a program that sets up a client/server TCP socket over which the user sends an integer value to the server through the use of a terminal interface. On the server side I am executing byte commands for which I need hex values stored in my array.
sprint(mychararray, %X, myintvalue);
This code takes my integer and prints it as a hex value into a char array. The only problem is when I use that array to set my commands it registers as an ascii char. So for example if I send an integer equal to 3000 it is converted to 0x0BB8 and then stored as 'B''B''8' which corresponds to 42 42 38 in hex. I have looked all over the place for a solution, and have not been able to come up with one.
Finally came up with a solution to my problem. First I created an array and stored all hex values from 1 - 256 in it.
char m_list[256]; //array defined in class
m_list[0] = 0x00; //set first array index to zero
int count = 1; //count variable to step through the array and set members
while (count < 256)
{
m_list[count] = m_list[count -1] + 0x01; //populate array with hex from 0x00 - 0xFF
count++;
}
Next I created a function that lets me group my hex values into individual bytes and store into the array that will be processing my command.
void parse_input(char hex_array[], int i, char ans_array[])
{
int n = 0;
int j = 0;
int idx = 0;
string hex_values;
while (n < i-1)
{
if (hex_array[n] = '\0')
{
hex_values = '0';
}
else
{
hex_values = hex_array[n];
}
if (hex_array[n+1] = '\0')
{
hex_values += '0';
}
else
{
hex_values += hex_array[n+1];
}
cout<<"This is the string being used in stoi: "<<hex_values; //statement for testing
idx = stoul(hex_values, nullptr, 16);
ans_array[j] = m_list[idx];
n = n + 2;
j++;
}
}
This function will be called right after my previous code.
sprint(mychararray, %X, myintvalue);
void parse_input(arrayA, size of arrayA, arrayB)
Example: arrayA = 8byte char array, and arrayB is a 4byte char array. arrayA should be double the size of arrayB since you are taking two ascii values and making a byte pair. e.g 'A' 'B' = 0xAB
While I was trying to understand your question I realized what you needed was more than a single variable. You needed a class, this is because you wished to have a string that represents the hex code to be printed out and also the number itself in the form of an unsigned 16 bit integer, which I deduced would be something like unsigned short int. So I created a class that did all this for you named hexset (I got the idea from bitset), here:
#include <iostream>
#include <string>
class hexset {
public:
hexset(int num) {
this->hexnum = (unsigned short int) num;
this->hexstring = hexset::to_string(num);
}
unsigned short int get_hexnum() {return this->hexnum;}
std::string get_hexstring() {return this->hexstring;}
private:
static std::string to_string(int decimal) {
int length = int_length(decimal);
std::string ret = "";
for (int i = (length > 1 ? int_length(decimal) - 1 : length); i >= 0; i--) {
ret = hex_arr[decimal%16]+ret;
decimal /= 16;
}
if (ret[0] == '0') {
ret = ret.substr(1,ret.length()-1);
}
return "0x"+ret;
}
static int int_length(int num) {
int ret = 1;
while (num > 10) {
num/=10;
++ret;
}
return ret;
}
static constexpr char hex_arr[16] = {'0','1','2','3','4','5','6','7','8','9','A','B','C','D','E','F'};
unsigned short int hexnum;
std::string hexstring;
};
constexpr char hexset::hex_arr[16];
int main() {
int number_from_file = 3000; // This number is in all forms technically, hex is just another way to represent this number.
hexset hex(number_from_file);
std::cout << hex.get_hexstring() << ' ' << hex.get_hexnum() << std::endl;
return 0;
}
I assume you'll probably want to do some operator overloading to make it so you can add and subtract from this number or assign new numbers or do any kind of mathematical or bit shift operation.