refactored serde between waypoint number and 26 radix string . (#122)

This commit is contained in:
Zero Fanker 2024-11-26 22:53:24 -05:00 committed by GitHub
parent cace9efd2a
commit f0f2439be0
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
6 changed files with 74 additions and 70 deletions

View file

@ -1,6 +1,7 @@
#pragma once
#include <afx.h>
#include <algorithm>
#include <array>
// coordinate functions
inline void PosToXY(const char* pos, int* X, int* Y)
@ -89,4 +90,48 @@ inline std::array<unsigned char, 3> HSVToRGB(const unsigned char hsv[3])
std::array<unsigned char, 3> ret;
HSVToRGB(hsv, ret.data());
return ret;
}
inline int letter2number(char let) {
int reply = let - 'A';
return reply;
}
inline char number2letter(int let) {
int reply = let + 'A';
return reply;
}
inline int StringToWaypoint(const CString& str)
{
if (str.IsEmpty()) {
return -1;
}
int num = 0;
for (auto idx = 0; idx < str.GetLength(); ++idx) {
auto const ch = str[idx];
num = (num + idx) * 26 + letter2number(ch);
}
return num;
}
// Serialize waypoint, will be renamed later
inline CString WaypointToString(int num)
{
if (num < 0) {
return {};
}
char secondChar = number2letter(num % 26);
char carry = num / 26;
if (!carry) {
return secondChar;
}
char firstChar = number2letter(carry - 1);
CString ret;
ret += firstChar;
ret += secondChar;
return ret;
}