编码数,C#实现的Z-基地-32​​或其他什么东西? [英] Encoding a number, C# implementation of z-base-32 or something else?

查看:223
本文介绍了编码数,C#实现的Z-基地-32​​或其他什么东西?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要连接code /德code整数是高达9位数,但最多7位。我想,使其更容易沟通/记忆 - 这将通过电话传达,复制及放大器;粘贴,从卡/内存/标签中键入,手写的标签,一般滥用!

I need to encode/decode an integer which is up to 9 digits long but most often 7 digits long. I'd like to make it easier to communicate/memorise - it will be communicated by phone, copied & pasted, keyed in from a card/memory/label, hand-written on labels and generally abused!

我在寻找减少的位数(同时增加一个校验)使用基地32 方案。我最赞成 Z-基32 (通过的RFC4648 的实施),置换后的有利于更易于字符读,写,讲,记住)。但是,我找不到一个C#实现,我很担心从现有的Python实现移植。

I'm looking to reduce the number of digits (whilst adding a checksum) using a base 32 scheme. I'm most in favour of z-base-32 (over the RFC4648 implementation) because of some of the design goals (e.g. handwriting) and choice of alphabet (lower-case, permuted to favour characters that are easier to read, write, speak, and remember). However, I can't find a C# implementation and I'm concerned about porting from the existing Python implementation.

有没有人有一个C#实现?另外,没有任何人有一组测试案例,我可以用它来验证端口(较规范的例子除外)?

Does anyone have a C# implementation? Alternatively, does anyone have a set of test cases (other than the examples in the spec) that I can use to validate a port?

我愿意接受关于可选编码方案的建议。

I'm open to suggestions about alternative encoding schemes.

推荐答案

我开始与死帐户提供的code,并提出根据我的测试中的一些变化。我希望这是有益的。

I started with the code provided by Dead account and made some changes based on my tests. I hope this is useful.

/// <summary>
/// Author: Ben Maddox
/// </summary>
public class ZBase32Encoder
{
    /*
     * Accepted characters based on code from: 
     * http://www.codeproject.com/KB/recipes/Base32Encoding.aspx?display=Print
     */
    public const string AcceptedCharacters = "ybndrfg8ejkmcpqxot1uwisza345h769";

    public static string Encode(int input)
    {
        string result = "";

        if (input == 0)
        {
            result += AcceptedCharacters[0];
        }
        else
        {
            while (input > 0)
            {
                //Must make sure result is in the correct order
                result = AcceptedCharacters[input%AcceptedCharacters.Length] + result;
                input /= AcceptedCharacters.Length;
            }
        }

        return result;
    }

    public static int Decode(string input)
    {
        var inputString = input.ToLower();

        int result = 0;
        for (int i = 0; i < inputString.Length; i++)
        {
            result *= AcceptedCharacters.Length;
            var character = inputString[i];
            result += AcceptedCharacters.IndexOf(character);
        }
        return result;
    }

    public static int Decode(char data)
    {
        return Decode(data.ToString());
    }
}

和这里是我使用的测试。 MS测试与如若断言库。

And here are the tests I used. MS Test with the Should assertions library.

[TestClass]
public class ZBase32EncoderTests
{

    [TestMethod]
    public void Encoding_0_ReturnsFirstCharacter()
    {
        var result = ZBase32Encoder.Encode(0);
        result.ShouldEqual(ZBase32Encoder.AcceptedCharacters[0].ToString());
    }

    [TestMethod]
    public void Encoding_1_ReturnsSecondCharacter()
    {
        var result = ZBase32Encoder.Encode(1);
        result.ShouldEqual(ZBase32Encoder.AcceptedCharacters[1].ToString());
    }

    [TestMethod]
    public void Encoding_32_ReturnsSecondAndFirstValues()
    {
        var result = ZBase32Encoder.Encode(32);
        result.ShouldEqual(ZBase32Encoder.AcceptedCharacters[1].ToString() + ZBase32Encoder.AcceptedCharacters[0].ToString());
    }

    [TestMethod]
    public void Encoding_64_ReturnsThirdAndFirstValues()
    {
        var result = ZBase32Encoder.Encode(64);
        result.ShouldEqual(ZBase32Encoder.AcceptedCharacters[2].ToString() + ZBase32Encoder.AcceptedCharacters[0].ToString());
    }

    [TestMethod]
    public void Encoding_65_ReturnsThirdAndSecondValues()
    {
        var result = ZBase32Encoder.Encode(65);
        result.ShouldEqual(ZBase32Encoder.AcceptedCharacters[2].ToString() + ZBase32Encoder.AcceptedCharacters[1].ToString());
    }



    [TestMethod]
    public void Decoding_FirstCharacter_Returns_0()
    {
        var inputCharacter = ZBase32Encoder.AcceptedCharacters[0];
        var result = ZBase32Encoder.Decode(inputCharacter);
        result.ShouldEqual(0);
    }

    [TestMethod]
    public void Decoding_SecondCharacter_Returns_1()
    {
        var inputCharacter = ZBase32Encoder.AcceptedCharacters[1];
        var result = ZBase32Encoder.Decode(inputCharacter);
        result.ShouldEqual(1);
    }

    [TestMethod]
    public void Decoding_SecondAndFirstValues_Shows_32()
    {
        var inputCharacters = ZBase32Encoder.AcceptedCharacters[1].ToString() + ZBase32Encoder.AcceptedCharacters[0];
        var result = ZBase32Encoder.Decode(inputCharacters);
        result.ShouldEqual(32);
    }

    [TestMethod]
    public void Decoding_ThirdAndFirstCharacters_Shows_64()
    {
        var inputCharacters = ZBase32Encoder.AcceptedCharacters[2].ToString() + ZBase32Encoder.AcceptedCharacters[0];
        var result = ZBase32Encoder.Decode(inputCharacters);
        result.ShouldEqual(64);
    }
}

这篇关于编码数,C#实现的Z-基地-32​​或其他什么东西?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆