我如何使用大写字母和数字对长数字进行编码以缩短输入时间?

How could I encode a long number using uppercase letters and numbers to make it shorter to type?

有没有一种方法可以将长数字(例如 12349874529768521)编码为小写字母和数字以缩短其长度?这个想法是用户可能在一张纸上有一个很长的数字。

在我看来,如果有更多可用的符号,则结果数字可能会更短。所以我正在寻找类似十六进制的东西,但使用 A-Z 的较大符号 space 而不仅仅是 A-F。

这将在 C# 中(如果重要的话)

您可以使用 base 36 编码器。

Base36 is a binary-to-text encoding scheme that represents binary data in an ASCII string format by translating it into a radix-36 representation. The choice of 36 is convenient in that the digits can be represented using the Arabic numerals 0–9 and the Latin letters A–Z1 (the ISO basic Latin alphabet).

这是一个例子,但任何一个都应该有效:https://github.com/thewindev/csharpbase36

用法示例

// Encoding
Base36.Encode(10);    // returns "A"
Base36.Encode(10000); // returns "7PS"

// Decoding
Base36.Decode("Z");   // returns 35L
Base36.Decode("10");  // returns 36L
Base36.Decode("7PS"); // returns 10000L

默认使用大写字母。如果你真的想小写,那么一个简单的 string.ToLowerInvarient() 就可以改变它。

但是,大写字母通常更易于阅读,这就是默认使用大写字母的原因,因此您可能需要考虑使用大写字母而不是小写字母。

我猜你的意思是你想用更少的字符来表示数字。 Base 36 将执行此操作 (0-9, a-z)。

您可以查看 Base64 编码。它使用 0-9A-Za-z+/ 字符.或者 Base36,如果您只对 0-9A-Z.

感兴趣

Base32 编码旨在生成明确、紧凑、人类可读(且非淫秽!)的表示形式。来自 Wikipedia:

Base32 has a number of advantages over Base64:

  • The resulting character set is all one case, which can often be beneficial when using a case-insensitive filesystem, spoken language, or human memory.

  • The result can be used as a file name because it can not possibly contain the '/' symbol, which is the Unix path separator.

  • The alphabet can be selected to avoid similar-looking pairs of different symbols, so the strings can be accurately transcribed by hand. (For example, the RFC 4648 symbol set omits the digits for one, eight and zero, since they could be confused with the letters 'I', 'B', and 'O'.)

  • A result excluding padding can be included in a URL without encoding any characters.

Base32 also has advantages over hexadecimal/Base16: Base32 representation takes roughly 20% less space. (1000 bits takes 200 characters, compared to 250 for Base16)

Douglas Crockford 的 original article on Base32 encoding 也值得一读。

编辑:这里有一些 C# 将对整数进行 base-N 编码:

class Program {
    private const string BINARY = "01";
    private const string DECIMAL = "0123456789";
    private const string HEX = "0123456789abcdef";
    private const string BASE32 = "0123456789abcdefghjkmnpqrstvwxyz";

    static string EncodeInt32(string alphabet, int value) {
        var sb = new StringBuilder();
        while (value > 0) {
            sb.Insert(0, alphabet[value % alphabet.Length]);
            value = value / alphabet.Length;
        }
        return sb.ToString();
    }

    static int DecodeInt32(string alphabet, string value) {
        int result = 0;
        int b = alphabet.Length;
        int pow = 0;
        for (var i = value.Length-1; i >= 0; i--) {
            result += (int)(Math.Pow(b, pow++)) * alphabet.IndexOf(value[i]);
        }
        return (result);
    }

    static void Main(string[] args) {
        for (var i = 0; i < 1234567890; i += 1234567) { 
            Console.WriteLine("{0} {1} {2}", i, EncodeInt32(BASE32, i), DecodeInt32(BASE32, EncodeInt32(BASE32, i))); 
        }
        Console.ReadKey(false);
    }
}

显示字符串长度典型减少的示例输出:

1227159598 14j9y1e 1227159598
1228394165 14kfknn 1228394165
1229628732 14mn99w 1229628732
1230863299 14ntyy3 1230863299
1232097866 14q0mja 1232097866
1233332433 14r6a6h 1233332433
1234567000 14sbztr 1234567000

如何使用 BaseN 方法 encode/decode 将 long 转换为包含您自己定义的字符的字符串

public static class BaseN
{
    private const string CharList = "0123456789abcdefghijklmnopqrstuvwxyz";
    public static String Encode(long input)
    {
        if (input < 0) throw new ArgumentOutOfRangeException("input", input, "input cannot be negative");
        var result = new System.Collections.Generic.Stack<char>();
        while (input != 0)
        {
            result.Push(CharList[(int)(input % CharList.Length)]);
            input /= CharList.Length;
        }
        return new string(result.ToArray());
    }

    public static long Decode(string input)
    {
        long result = 0, pos = 0;
        foreach (char c in input.Reverse())
        {
            result += CharList.IndexOf(c) * (long)Math.Pow(CharList.Length, pos);
            pos++;
        }
        return result;
    }
}

用法:

long number = 12349874529768521;
string result = BaseN.Encode(number);

样本:

https://dotnetfiddle.net/odwFlk

这是与其他方法类似的方法,使用 Base-N 转换:

using System;
using System.Text;

namespace ConsoleApp3
{
    class Program
    {
        static void Main()
        {
            long n = 12349874529768521;

            string baseChars = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz@#";

            var encoded = AsBaseN(n, baseChars.ToCharArray());
            Console.WriteLine(encoded); // Prints "9HXNyK2uh"

            long decoded = AsLong(encoded, baseChars.ToCharArray());
            Console.WriteLine(decoded); // Prints "12349874529768521"
        }

        public static string AsBaseN(long value, char[] baseChars)
        {
            var result = new StringBuilder();
            int targetBase = baseChars.Length;

            do
            {
                result.Append(baseChars[value % targetBase]);
                value /= targetBase;
            }
            while (value > 0);

            return result.ToString();
        }

        public static long AsLong(string number, char[] baseChars)
        {
            long result = 0;
            int numberBase = baseChars.Length;
            long multiplier = 1;

            foreach (char c in number)
            {
                result += multiplier * Array.IndexOf(baseChars, c);
                multiplier *= numberBase;
            }

            return result;
        }
    }
}

如果您想要一组不同的允许字符,只需适当更改 baseChars。例如,如果您只想要 0-9 和 A-Z:

string baseChars = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ";

这给出了 T3OPA1YNLD3(基数 36)而不是 9HXNyK2uh(基数 64)的结果。