我有一个使用散列函数创建的字节数组。 我想将这个数组转换成一个字符串。 到目前为止,它会给我十六进制字符串。
现在我想使用不同于十六进制字符的东西,我想用这36个字符来编码字节数组 :[az] [0-9] 。
我该怎么办?
编辑:我会这样做的原因是因为我想有一个比十六进制字符串更小的字符串。
I've got a byte array that was created using a hash function. I would like to convert this array into a string. So far so good, it will give me hexadecimal string.
Now I would like to use something different than hexadecimal characters, I would like to encode the byte array with these 36 characters: [a-z][0-9].
How would I go about?
Edit: the reason I would to do this, is because I would like to have a smaller string, than a hexadecimal string.
最满意答案
我将这个答案的任意长度基础转换函数调整为C#:
static string BaseConvert(string number, int fromBase, int toBase) { var digits = "0123456789abcdefghijklmnopqrstuvwxyz"; var length = number.Length; var result = string.Empty; var nibbles = number.Select(c => digits.IndexOf(c)).ToList(); int newlen; do { var value = 0; newlen = 0; for (var i = 0; i < length; ++i) { value = value * fromBase + nibbles[i]; if (value >= toBase) { if (newlen == nibbles.Count) { nibbles.Add(0); } nibbles[newlen++] = value / toBase; value %= toBase; } else if (newlen > 0) { if (newlen == nibbles.Count) { nibbles.Add(0); } nibbles[newlen++] = 0; } } length = newlen; result = digits[value] + result; // } while (newlen != 0); return result; }由于它来自PHP,它可能不太习惯C#,也没有参数有效性检查。 但是,您可以用十六进制编码的字符串来提供它,并且它可以正常工作
var result = BaseConvert(hexEncoded, 16, 36);这不是你要求的,但是将byte[]编码为十六进制是很简单的。
在行动中看到它 。
I adapted my arbitrary-length base conversion function from this answer to C#:
static string BaseConvert(string number, int fromBase, int toBase) { var digits = "0123456789abcdefghijklmnopqrstuvwxyz"; var length = number.Length; var result = string.Empty; var nibbles = number.Select(c => digits.IndexOf(c)).ToList(); int newlen; do { var value = 0; newlen = 0; for (var i = 0; i < length; ++i) { value = value * fromBase + nibbles[i]; if (value >= toBase) { if (newlen == nibbles.Count) { nibbles.Add(0); } nibbles[newlen++] = value / toBase; value %= toBase; } else if (newlen > 0) { if (newlen == nibbles.Count) { nibbles.Add(0); } nibbles[newlen++] = 0; } } length = newlen; result = digits[value] + result; // } while (newlen != 0); return result; }As it's coming from PHP it might not be too idiomatic C#, there are also no parameter validity checks. However, you can feed it a hex-encoded string and it will work just fine with
var result = BaseConvert(hexEncoded, 16, 36);It's not exactly what you asked for, but encoding the byte[] into hex is trivial.
See it in action.
如何将一个字节数组(MD5哈希)转换为一个字符串(36个字符)?(How to convert a byte array (MD5 hash) into a string (36 chars)?)我有一个使用散列函数创建的字节数组。 我想将这个数组转换成一个字符串。 到目前为止,它会给我十六进制字符串。
现在我想使用不同于十六进制字符的东西,我想用这36个字符来编码字节数组 :[az] [0-9] 。
我该怎么办?
编辑:我会这样做的原因是因为我想有一个比十六进制字符串更小的字符串。
I've got a byte array that was created using a hash function. I would like to convert this array into a string. So far so good, it will give me hexadecimal string.
Now I would like to use something different than hexadecimal characters, I would like to encode the byte array with these 36 characters: [a-z][0-9].
How would I go about?
Edit: the reason I would to do this, is because I would like to have a smaller string, than a hexadecimal string.
最满意答案
我将这个答案的任意长度基础转换函数调整为C#:
static string BaseConvert(string number, int fromBase, int toBase) { var digits = "0123456789abcdefghijklmnopqrstuvwxyz"; var length = number.Length; var result = string.Empty; var nibbles = number.Select(c => digits.IndexOf(c)).ToList(); int newlen; do { var value = 0; newlen = 0; for (var i = 0; i < length; ++i) { value = value * fromBase + nibbles[i]; if (value >= toBase) { if (newlen == nibbles.Count) { nibbles.Add(0); } nibbles[newlen++] = value / toBase; value %= toBase; } else if (newlen > 0) { if (newlen == nibbles.Count) { nibbles.Add(0); } nibbles[newlen++] = 0; } } length = newlen; result = digits[value] + result; // } while (newlen != 0); return result; }由于它来自PHP,它可能不太习惯C#,也没有参数有效性检查。 但是,您可以用十六进制编码的字符串来提供它,并且它可以正常工作
var result = BaseConvert(hexEncoded, 16, 36);这不是你要求的,但是将byte[]编码为十六进制是很简单的。
在行动中看到它 。
I adapted my arbitrary-length base conversion function from this answer to C#:
static string BaseConvert(string number, int fromBase, int toBase) { var digits = "0123456789abcdefghijklmnopqrstuvwxyz"; var length = number.Length; var result = string.Empty; var nibbles = number.Select(c => digits.IndexOf(c)).ToList(); int newlen; do { var value = 0; newlen = 0; for (var i = 0; i < length; ++i) { value = value * fromBase + nibbles[i]; if (value >= toBase) { if (newlen == nibbles.Count) { nibbles.Add(0); } nibbles[newlen++] = value / toBase; value %= toBase; } else if (newlen > 0) { if (newlen == nibbles.Count) { nibbles.Add(0); } nibbles[newlen++] = 0; } } length = newlen; result = digits[value] + result; // } while (newlen != 0); return result; }As it's coming from PHP it might not be too idiomatic C#, there are also no parameter validity checks. However, you can feed it a hex-encoded string and it will work just fine with
var result = BaseConvert(hexEncoded, 16, 36);It's not exactly what you asked for, but encoding the byte[] into hex is trivial.
See it in action.
发布评论