我有一个独特的情况,我必须在已经建立的平台上编写代码,所以我试图找出一个可以使某些东西工作的黑客。
我遇到的问题是我有一个用户定义的字符串。基本上命名一个信号。我需要将其转换为另一个程序,但唯一可用的方法是在double值内。以下是我尝试但未能使其工作的内容。我尝试将字符串转换为字节数组,然后通过循环字节创建一个新字符串。然后我将此字符串转换为Double。然后使用BitCoverter将其恢复为字节数组,然后尝试获取字符串。
不确定这是否可以实现。有什么想法吗?
string signal = "R3MEXA";
string newId = "1";
byte[] asciiBytes = System.Text.Encoding.ASCII.GetBytes(signal);
foreach (byte b in asciiBytes)
newId += b.ToString();
double signalInt = Double.Parse(newId);
byte[] bytes = BitConverter.GetBytes(signalInt);
string result = System.Text.Encoding.ASCII.GetString(bytes);
答案 0 :(得分:0)
假设你的字符串由ASCII字符组成(7Bit):
将字符串转换为位数组,每个字符7位。
将此位数组转换为数字字符串,每个数字使用3位。 (有数字0..7)
将此数字字符串转换为双数字。
答案 1 :(得分:0)
您最初将newId
设置为"1"
,这意味着当您以后进行转换时,除非考虑{{1},否则您将无法获得正确的输出再次。
答案 2 :(得分:0)
它不起作用,因为如果将其转换回来,则不知道字节的长度。 所以我将每个字节的长度设为3。
string signal = "R3MEXA";
string newId = "1";
byte[] asciiBytes = System.Text.Encoding.ASCII.GetBytes(signal);
foreach (byte b in asciiBytes)
newId += b.ToString().PadLeft(3,'0'); //Add Zero, if the byte has less than 3 digits
double signalInt = Double.Parse(newId);
//Convert it back
List<byte> bytes = new List<byte>(); //Create a list, we don't know how many bytes will come (Or you calc it: maximum is _signal / 3)
//string _signal = signalInt.ToString("F0"); //Maybe you know a better way to get the double to string without scientific
//This is my workaround to get the integer part from the double:
//It's not perfect, but I don't know another way at the moment without losing information
string _signal = "";
while (signalInt > 1)
{
int _int = (int)(signalInt % 10);
_signal += (_int).ToString();
signalInt /= 10;
}
_signal = String.Join("",_signal.Reverse());
for (int i = 1; i < _signal.Length; i+=3)
{
byte b = Convert.ToByte(_signal.Substring(i, 3)); //Make 3 digits to one byte
if(b!=0) //With the ToString("F0") it is possible that empty bytes are at the end
bytes.Add(b);
}
string result = System.Text.Encoding.ASCII.GetString(bytes.ToArray()); //Yeah "R3MEX" The "A" is lost, because double can't hold that much.
194 | 68 | 75 | 13
194687513
Reverse:
315786491
31 //5 is too big 13
57 //8 is too big 75
86 //4 is too big 68
491 //1 is ok 194