转换字节数组到任何基地 [英] Convert byte array into any base

查看:201
本文介绍了转换字节数组到任何基地的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个字节数组(任意长度),我想EN code数组到字符串中使用我自己的基本连接codeR。在 .NET 为标准的Base64 连接codeR,但如果我想连接code中的阵列中的 Base62 Base53 Base13

I have an array of bytes (any length), and I want to encode this array into string using my own base encoder. In .NET is standard Base64 encoder, but what if I want to encode the array in Base62, Base53 or Base13?

它甚至有可能创造这样的通用底座连接codeR?

Is it even possible to create such universal base encoder?

我知道我能做到这一点简单的方法,那就是,对于字符的每个字节储备一定数量(如遇 Base62 中,这将是5个字符),并做直接针对字节>碳化的编码,但我会浪费空间,5 Base62 字符都能够包含超过1个字节,但不超过2个字节。

I know I could do it the simple way, that is, for each byte reserve fixed number of chars (in case of Base62, that would be 5 chars), and do direct byte->chars encoding, but I would be wasting space, as 5 Base62 chars are able to contain more than 1 byte, but less than 2 bytes.

我应该怎么写这样的一间codeR?或者是有已经有一些类呢?
而且请注意,我需要普遍德codeR为好,否则这对我没用。

How should I write such an encoder? Or is there already some class for this?
And please note that I need universal decoder as well, otherwise this is useless to me.

由于该解决方案是已知的(使用的BigInteger ),我只想把这里一些与资源的BigInteger 类,因为它不具备的.NET 3.5:

As the solution is already known (use BigInteger), I would just like to put here some resources relating the BigInteger class, as it is not available in .NET 3.5:

<一个href="http://stackoverflow.com/questions/176775/big-integers-in-c">http://stackoverflow.com/questions/176775/big-integers-in-c
HTTP://intx.$c$cplex.com/
<一href="https://svn.apache.org/repos/asf/incubator/heraldry/libraries/csharp/openid/trunk/Mono/Mono.Math/BigInteger.cs">https://svn.apache.org/repos/asf/incubator/heraldry/libraries/csharp/openid/trunk/Mono/Mono.Math/BigInteger.cs
<一href="http://www.$c$cproject.com/KB/cs/BigInteger_Library.aspx">http://www.$c$cproject.com/KB/cs/BigInteger_Library.aspx
<一href="http://www.$c$cproject.com/KB/cs/biginteger.aspx">http://www.$c$cproject.com/KB/cs/biginteger.aspx

http://stackoverflow.com/questions/176775/big-integers-in-c
http://intx.codeplex.com/
https://svn.apache.org/repos/asf/incubator/heraldry/libraries/csharp/openid/trunk/Mono/Mono.Math/BigInteger.cs
http://www.codeproject.com/KB/cs/BigInteger_Library.aspx
http://www.codeproject.com/KB/cs/biginteger.aspx

推荐答案

如果性能是不是一个问题,使用的 BigInteger的在后台类。你有一个构造函数BigInteger的需要字节数组,然后可以手动运行分工和模量的循环,以获得重新presentation其他非标准基地。

If performance is not an issue, use the BigInteger class in the background. You have a constructor for BigInteger that takes byte array, and you can then manually run loops of division and modulus to get the representation in other non-standard bases.

另外一起来看看

这篇关于转换字节数组到任何基地的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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