Encoder - Decoder un nombre de 5 chiffres en un nombre unique de 4 chiffre (de façon réversible

Bonjour,
Français, Informaticien à la retraite depuis 20 ans, je continue, pour le plaisir , à utiliser les langages de programmation. COBOL, Basic,Pascal, Langage Base, Windev, et maintenant XOJO. Donc je débute dans ce langage et j’ai besoin d’un peu d’aide pour trouver une solution à mon problème
J’ai une numérotation de 1 à 99999
Je dois transformer ces nombres en un nombre de 4 chiffres sans doublons
je dois faire une liaison entre les 2
Table 1 : NumPart (1 à 999999),EntreeSB(1 à 9999)
EntreeSB doit être encodé à partir de NumPart

Table : EntreeSB

EntreeSB doit me permettre d’accéder à NumPart

J’ai essayé différentes solutions avec des entiers, ça ne fonctionne pas dans tous les cas (La limitation de EntreeSB à 4 chiffres et pas de doublons) EntreeSB ne peut être qu’un entier (maxi 9999 : limitation du matériel : un Synthé)

j’ai dans l’idée de passer par de l’hexadécimale et de travailler au niveau octet

J’ai comme point de départ
Exemple : NumPart = 12458
Milliers = NumPart / 9999
Unites = Modulo(NumPart,9999)
Après il faut combiner ces 2 valeur pour obtenir un nombre de 4 chiffres
et là le travail dans les octets je sais pas trop faire,
SI une âme charitable peut me donner un coup de main pour obtenir les 2 fonctions
EntreeSB = EncodeNumPart(NumPart)
NumPart = DecodeSB(EntreeSB)
Merci d’avance

Traduction de mon Ami GoogleHello,

French, a retired computer scientist for 20 years, I continue, for pleasure, to use programming languages. COBOL, Basic, Pascal, Language Base, Windev, and now XOJO. So I’m starting in this language and I need a little help to find a solution to my problem

I have a number from 1 to 99999

I have to turn these numbers into a number of 4 digits without duplicates

I have to make a connection between the 2

Table 1: NumPart (1 to 999999),EntreeSB(1 to 9999)

EntreeSB must be encoded from NumPart

Table: EntreeSB

EntreeSB must allow me to access NumPart

I tried different solutions with integers, it doesn’t work in all cases (The limitation of EntreeSB to 4 digits and no duplicates) EntreeSB can only be an integer (maxi 9999: hardware limitation: a Synth)

I have the idea of going through hexadecimal and working at the byte level

I have as a starting point

Example: NumPart = 12458

Thousands = NumPart / 9999

Unites = Modulo(NumPart,9999)

Then you have to combine these 2 values to get a number of 4 digits

And there the work in the bytes I don’t know how to do too much,

IF a charitable soul can give me a hand to obtain the 2 functions

EntreeSB = EncodeNumPart(NumPart)

NumPart = DecodeSB(EntreeSB)

Thanks in advance

  ' Converts an Integer into a string representation of the number in base 26 (A-Z)
  Static base() As String = Split("ABCDEFGHIJKLMNOPQRSTUVWXYZ", "")
  Const NewBase As Integer = 26
  
  If Value < 0 Then
    Return "-" + ConvertToBase26(-Value)
  End If
  
  If Value = 0 Then
    Return "A"
  End If
  
  Dim digit() As String
  Dim n As Integer = Value
  
  Do Until n = 0
    n = n - 1 ' Adjust to make 'A' represent 0
    Dim remainder As Integer = n Mod NewBase
    n = n \ NewBase
    digit.Insert(0, base(remainder))
  Loop
  
  Return Join(digit, "")
End Function

Function ConvertFromBase26(Value As String) As Integer
  ' Converts a base 26 string (A-Z) into an Integer
  Static base() As String = Split("ABCDEFGHIJKLMNOPQRSTUVWXYZ", "")
  Const FromBase As Integer = 26
  
  Dim Result As Integer
  For i As Integer = 1 To Value.Len
    Dim datum As Integer = base.IndexOf(Value.Mid(i, 1))
    Result = FromBase * Result + datum
  Next
  
  Return Result + 1 ' Adjust to make 'A' represent 1
End Function

convert both numbers to BASE26 strings

Dim num As Integer = 701
Dim base26 As String = ConvertToBase26(num)
Print(base26) ' Output: ZZ

Dim decoded As Integer = ConvertFromBase26(base26)
Print(decoded) ' Output: 701

Hello DaveS,
I had not thought of the “Base Change” approach which is interesting I am under XOJO 2023 R1.2
I tested your code but I do not have the right result
For your example 701 I have Output = ZY to the encoding and 675 to the decoding
I notice that in the encoding the result of the line n = n \ NewBase is not reused
So we only work on a remainder without taking care of the entire part?

When decoding we do 26 * result + position of the letter For 701 ( ZZ ) it’s
26 * 0 +25 = 25
26 * 1 + 25 = 650
I apologize, I didn’t understand everything
For my problem the encoding of 4 characters must be numeric, so you have to re-encode ZZ to get a number in base 10
Thank you for your interest in my problem