×

Loading...
Ad by
  • 最优利率和cashback可以申请特批,好信用好收入offer更好。请点链接扫码加微信咨询,Scotiabank -- Nick Zhang 6478812600。
Ad by
  • 最优利率和cashback可以申请特批,好信用好收入offer更好。请点链接扫码加微信咨询,Scotiabank -- Nick Zhang 6478812600。

My code in C# (replaced leading spaces with dots to maintenance indentation). I used only basic language elements and it's easy to convert into other languages like Java or C or C++. Output of the numbers mentioned above are appended as well.

本文发表在 rolia.net 枫下论坛using System;

namespace Z0z0z0
{
....class Program
....{
........public static string Z0ToString(uint number)
........{
............char[] digits = { 'A', '0', 'A', '0', 'A', '0' };
............for (int i = digits.Length -1; i >= 0 && number > 0; --i)
............{
................uint divisor = (uint)(digits[i] == '0' ? 10 : 26);
................digits[i] = (char)(((uint)digits[i]) + number % divisor);
................number /= divisor;
............}

............string z0 = string.Join("", digits);

............return z0;
........}

........static void Main(string[] args)
........{
............Console.WriteLine("Decimal {0}: {1}", 0, Z0ToString(0));
............Console.WriteLine("Decimal {0}: {1}", 1, Z0ToString(1));
............Console.WriteLine("Decimal {0}: {1}", 10, Z0ToString(10));
............Console.WriteLine("Decimal {0}: {1}", 11, Z0ToString(11));
............Console.WriteLine("Decimal {0}: {1}", 123, Z0ToString(123));
............for (uint i = 295; i <= 303; ++i)
............{
................Console.WriteLine("Decimal {0}: {1}", i, Z0ToString(i));
............}
............Console.WriteLine("Decimal {0}: {1}", 939159, Z0ToString(939159));
............Console.WriteLine("Decimal {0}: {1}", 1000001, Z0ToString(1000001));
........}

....}
}
// Output:
Decimal 0: A0A0A0
Decimal 1: A0A0A1
Decimal 10: A0A0B0
Decimal 11: A0A0B1
Decimal 123: A0A0M3
Decimal 295: A0A1D5
Decimal 296: A0A1D6
Decimal 297: A0A1D7
Decimal 298: A0A1D8
Decimal 299: A0A1D9
Decimal 300: A0A1E0
Decimal 301: A0A1E1
Decimal 302: A0A1E2
Decimal 303: A0A1E3
Decimal 939159: B3X2D9
Decimal 1000001: B4U6E1更多精彩文章及讨论,请光临枫下论坛 rolia.net
Report

Replies, comments and Discussions:

  • 工作学习 / 学科技术 / Integer to String 的转换
    有一个整数, 是从数据库的Sequence来的想把它转换成String, 格式要求是:Z0Z0Z0, 就是第1,3,5 位是字母,2,4,6位是0-9的数字, 应该怎样实现? 有API或代码吗? Java最好.

    1。 0 对应 A0A0A0
    2。 1 对应 A0A0A1
    3。 10 对应 A0A0B0
    4。 11 对应 A0A0B1
    • java code +1
      if I did not understand you wrong, here you are the code:
      • 非常感谢。
        但好像有bug。
        i=295 A0A2J5
        i=296 A0A2J6
        i=297 A0A2J7
        i=298 A0A2J8
        i=299 A0A2J9
        i=300 A0A3A0
        i=301 A0A3A1
        i=302 A0A3A2
        i=303 A0A3A3

        300 应该是A0A2K0
        • Are you sure? My code gets Decimal 300: A0A1E0. because 300 = (1 * 26 + 4) * 10 + 0 +1
          • 写了个验证,300=A0A1E0; +1
            本文发表在 rolia.net 枫下论坛public class Z0ToString {

            public static void main(String[] args) {
            for (int i = 0; i < 10000000; i++) {
            String convertedValue = convertBaseTenToString(i);
            System.out.println("i=" + i + " " + convertedValue);
            if (i!= convertStringToBaseTen(convertedValue)) {
            throw new RuntimeException("not equal: i = " + i + " convertStringToBaseTen(a)="+ convertStringToBaseTen(convertedValue));
            }
            }
            }

            static String convertBaseTenToString(int number) {
            char[] digits = { 'A', '0', 'A', '0', 'A', '0' };
            for (int i =digits.length -1; i>=0;i-- ) {
            int divisor = (i%2 !=0) ? 10 : 26;
            int a = (int)digits[i];
            int b = (a+ number % divisor);
            digits[i] = (char)b ;
            number /= divisor;
            }
            return new String(digits);
            }

            static int convertStringToBaseTen(String number) {
            int returnNumber=0;
            int base =1 ;
            for (int i = number.length()-1; i>=0; i--) {
            if (i%2 ==0) {
            int value = (number.charAt(i) -((int)'A'));
            if (i!= number.length()-1 ) {
            base = base * 10;
            }
            returnNumber = value* base + returnNumber;
            } else {
            int value = (number.charAt(i) -(int)'0');
            if (i!= number.length()-1 ) {
            base = base * 26;
            }
            returnNumber = value* base + returnNumber;
            }
            }
            return returnNumber;
            }
            }更多精彩文章及讨论,请光临枫下论坛 rolia.net
        • it might come from the way to split integer into digits, i will take a peek ,,,
    • 个位、百位和万位十进制,十位、千位和十万位26进制吗?或者全部十进制?允许超过6位数吗?
      • 个位、百位和万位十进制,十位、千位和十万位26进制吗?对 允许超过6位数吗?输入可以, 输出不可以。
    • answer='';"939159".split("").forEach(function(element, index, array) { answer+=(String.fromCharCode(65-(index%2)*17 + +1*element)); } );answer
      • looks very interesting; is '0' returning 'A0A0A0', '1' returns 'A0A0A1' ?
      • 看不懂, 能解析一下吗?
        • 1000001 的结果是什么?
    • My code in C# (replaced leading spaces with dots to maintenance indentation). I used only basic language elements and it's easy to convert into other languages like Java or C or C++. Output of the numbers mentioned above are appended as well.
      本文发表在 rolia.net 枫下论坛using System;

      namespace Z0z0z0
      {
      ....class Program
      ....{
      ........public static string Z0ToString(uint number)
      ........{
      ............char[] digits = { 'A', '0', 'A', '0', 'A', '0' };
      ............for (int i = digits.Length -1; i >= 0 && number > 0; --i)
      ............{
      ................uint divisor = (uint)(digits[i] == '0' ? 10 : 26);
      ................digits[i] = (char)(((uint)digits[i]) + number % divisor);
      ................number /= divisor;
      ............}

      ............string z0 = string.Join("", digits);

      ............return z0;
      ........}

      ........static void Main(string[] args)
      ........{
      ............Console.WriteLine("Decimal {0}: {1}", 0, Z0ToString(0));
      ............Console.WriteLine("Decimal {0}: {1}", 1, Z0ToString(1));
      ............Console.WriteLine("Decimal {0}: {1}", 10, Z0ToString(10));
      ............Console.WriteLine("Decimal {0}: {1}", 11, Z0ToString(11));
      ............Console.WriteLine("Decimal {0}: {1}", 123, Z0ToString(123));
      ............for (uint i = 295; i <= 303; ++i)
      ............{
      ................Console.WriteLine("Decimal {0}: {1}", i, Z0ToString(i));
      ............}
      ............Console.WriteLine("Decimal {0}: {1}", 939159, Z0ToString(939159));
      ............Console.WriteLine("Decimal {0}: {1}", 1000001, Z0ToString(1000001));
      ........}

      ....}
      }
      // Output:
      Decimal 0: A0A0A0
      Decimal 1: A0A0A1
      Decimal 10: A0A0B0
      Decimal 11: A0A0B1
      Decimal 123: A0A0M3
      Decimal 295: A0A1D5
      Decimal 296: A0A1D6
      Decimal 297: A0A1D7
      Decimal 298: A0A1D8
      Decimal 299: A0A1D9
      Decimal 300: A0A1E0
      Decimal 301: A0A1E1
      Decimal 302: A0A1E2
      Decimal 303: A0A1E3
      Decimal 939159: B3X2D9
      Decimal 1000001: B4U6E1更多精彩文章及讨论,请光临枫下论坛 rolia.net