让我发疯,数字转换 [英] Driving me insane, number conversion
问题描述
:mad:
亲爱的编码员,这个问题使我发疯.
我正在使用Visual Studio 2008,C#.net,Windows窗体...
我想在Windows窗体上有一个编辑/文本框,该框将接受如下所示的数字字符串:
456345-007876-343234-765676-543456-566565-321234-345654
然后在去除-"间隔符之后,我想将上面的内容表示为字节数组,这将是x字节编码,例如:
{0x56,0x33,0x56,0x66,0x77 ........}(示例)
然后,我可以处理上述字节数组,例如对其进行加密等.
然后,我想将新的字节数组转换回其数字字符串,因此在更改字节数组后,它现在可能会输出:
456544-676777-231234-567423-456776-567898-998788-455654
我发现了许多示例,这些示例允许我将代码编码/解码为十六进制或以64为基数或以32为基数,但是重要的是我可以用数字表示数据.
希望有人能帮助我吗?
提前非常感谢您
Steve
:mad:
Dear Coders, this problem is driving me insane.
I am using Visual Studio 2008, C# .net, windows forms...
I want to have an edit/text box on my windows form that will accepts a string of digits like the following:
456345-007876-343234-765676-543456-566565-321234-345654
Then after stripping out the ''-'' spacers, i want to represent the above as a byte array, which would be an x byte encoding, something like:
{0x56,0x33,0x56,0x66,0x77........} (example)
I can then work on the above byte array, for example encrypt it etc.
Then i want to convert the new byte array back to its number string, so after altering the byte array, it might now output:
456544-676777-231234-567423-456776-567898-998788-455654
I have found many examples that allow me to encode/decode to hex or base 64 or base 32 etc, but it is important that i can represent my data with numbers.
Hope some on can please help me?
Thank you VERY much in advance
Steve
推荐答案
对我来说,这似乎是一项艰巨的任务.到目前为止你做了什么?您的特定问题是什么?
:)
It looks like a quite strightforward task to me. What have you done so far? What is your specific problem about?
:)
类似这样的事情??
Something like this...?
internal static string SerialToHex(string serial)
{
serial = serial.Replace("-", string.Empty);
int value;
string result = string.Empty;
for (int i = 0; i < serial.Length / 6; i++)
{
int.TryParse(serial.Substring(6 * i, 6), NumberStyles.None, CultureInfo.InvariantCulture, out value);
result += value.ToString("X5", CultureInfo.InvariantCulture);
}
return result;
}
internal static byte[] HexToBytes(string hex)
{
byte[] result = new byte[hex.Length / 2];
for (int i = 0; i < hex.Length / 2; i++)
{
byte.TryParse(hex.Substring(2 * i, 2), NumberStyles.AllowHexSpecifier, CultureInfo.InvariantCulture, out result[i]);
}
return result;
}
internal static byte[] EncryptBytes(byte[] bytes)
{
// TODO: Add your code here
return bytes;
}
internal static string BytesToHex(byte[] bytes)
{
string result = string.Empty;
for (int i = 0; i < bytes.Length; i++)
{
result += bytes[i].ToString("X2", CultureInfo.InvariantCulture);
}
return result;
}
internal static string HexToSerial(string hex)
{
string result = string.Empty;
int value;
for (int i = 0; i < hex.Length / 5; i++)
{
int.TryParse(hex.Substring(5 * i, 5), NumberStyles.AllowHexSpecifier, CultureInfo.InvariantCulture, out value);
result += value.ToString("D6", CultureInfo.InvariantCulture);
result += "-";
}
result = result.Remove(result.Length - 1);
return result;
}
internal static bool Test(string serialIn)
{
string hexIn = SerialToHex(serialIn);
byte[] bytes = HexToBytes(hexIn);
string hexOut = BytesToHex(bytes);
string serialOut = HexToSerial(hexOut);
return serialIn == serialOut;
}
这篇关于让我发疯,数字转换的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!