-
Notifications
You must be signed in to change notification settings - Fork 37
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Working version of alpha5 conversion
Signed-off-by: Cees Bassa <[email protected]>
- Loading branch information
Showing
1 changed file
with
86 additions
and
33 deletions.
There are no files selected for viewing
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,51 +1,104 @@ | ||
#include <stdio.h> | ||
#include <stdlib.h> | ||
#include <string.h> | ||
#include <stdlib.h> | ||
#include <ctype.h> | ||
|
||
// Fixed mapping | ||
const char mapping[] = "0123456789ABCDEFGHJKLMNPQRSTUVWXYZ"; | ||
|
||
int alpha5_to_number(char *s) | ||
// Function to convert alpha5 format to number | ||
int alpha5_to_number(const char *s) | ||
{ | ||
int i; | ||
char c,spart[]="1234"; | ||
char alpha5[]="ABCDEFGHJKLMNPQRSTUVWXYZ"; | ||
|
||
printf("%c %s %d\n",alpha5[0],alpha5,strlen(alpha5)); | ||
|
||
c = s[0]; | ||
strncpy(spart,s+1,5); | ||
spart[5]='\0'; | ||
for (i=0;i<strlen(alpha5);i++) | ||
if (c==alpha5[i]) | ||
break; | ||
|
||
printf(">> %d %c |%c| |%s|\n",i,c,alpha5[i],spart); | ||
return (i+10)*10000+atoi(spart); | ||
// Split components | ||
char first_char = s[0]; | ||
int digits = 0; | ||
sscanf(s + 1, "%4d", &digits); // Get the last four digits | ||
|
||
// Decode first character | ||
int first_digit = 0; | ||
if (first_char == ' ') { | ||
first_digit = 0; | ||
} else if (isdigit(first_char)) { | ||
first_digit = first_char - '0'; | ||
} else { | ||
for (int i = 0; mapping[i] != '\0'; i++) { | ||
if (mapping[i] == first_char) { | ||
first_digit = i; | ||
break; | ||
} | ||
} | ||
} | ||
|
||
// Create and return number | ||
return first_digit * 10000 + digits; | ||
} | ||
|
||
void number_to_alpha5(int number,char *s) | ||
// Function to convert number to alpha5 format | ||
void number_to_alpha5(int number, char *result) | ||
{ | ||
int i; | ||
char c,alpha5[]="ABCDEFGHJKLMNPQRSTUVWXYZ"; | ||
// Split components | ||
int first_digit = number / 10000; | ||
int digits = number % 10000; | ||
|
||
// Get char | ||
char first_char = mapping[first_digit]; | ||
|
||
i=number/10000; | ||
c=alpha5[i-10]; | ||
sprintf(s,"%c%04d",c,number-i*10000); | ||
// Create string | ||
sprintf(result, "%c%04d", first_char, digits); | ||
|
||
return; | ||
return; | ||
} | ||
|
||
int main(int argc, char *argv[]) | ||
// Function to strip leading spaces | ||
void strip_leading_spaces(const char *s, char *result) | ||
{ | ||
int number; | ||
char s[] = "A0000",st[]=" "; | ||
while (*s == ' ') { | ||
s++; // Skip leading spaces | ||
} | ||
strcpy(result, s); // Copy the remainder of the string | ||
|
||
return; | ||
} | ||
|
||
// Function to zero pad a string | ||
void zero_pad(const char *s, char *result) | ||
{ | ||
char stripped[6]; // Temporarily store the string without leading spaces | ||
strip_leading_spaces(s, stripped); | ||
|
||
if (isdigit(stripped[0])) { | ||
int value = atoi(stripped); | ||
sprintf(result, "%05d", value); | ||
} else { | ||
strcpy(result, stripped); // No zero-padding for non-digit strings | ||
} | ||
} | ||
|
||
int main() { | ||
const char *norad_strings[] = {"00001", " 1", " 1234", "05697", "12345", "A0000", "J2931", "W1928", "E8493", "P4018", "Z9999"}; | ||
const int correct_numbers[] = {1, 1, 1234, 5697, 12345, 100000, 182931, 301928, 148493, 234018, 339999}; | ||
|
||
for (int i = 0; i < 11; i++) { | ||
const char *norad_string = norad_strings[i]; | ||
int correct_number = correct_numbers[i]; | ||
|
||
int number = alpha5_to_number(norad_string); | ||
printf("|%s| -> |%6d|==|%6d| %d\n", norad_string, number, correct_number, number == correct_number); | ||
} | ||
|
||
printf("\n"); | ||
|
||
number = alpha5_to_number(s); | ||
for (int i = 0; i < 11; i++) { | ||
int correct_number = correct_numbers[i]; | ||
char s[7]; // For storing the alpha5 string | ||
|
||
printf("%d\n",number); | ||
number_to_alpha5(correct_number, s); | ||
|
||
char zero_padded[6]; // For storing the zero-padded string | ||
zero_pad(norad_strings[i], zero_padded); | ||
|
||
number_to_alpha5(number,st); | ||
printf("|%6d| -> |%s|==|%s| %d\n", correct_number, s, zero_padded, strcmp(s, zero_padded) == 0); | ||
} | ||
|
||
printf("%s\n",st); | ||
|
||
return 0; | ||
return 0; | ||
} |