public bool ConvertDisplayTypes(ValueDisplay inValueDisplay, string inStr, ref ValueDisplay outValueDisplay, ref string outStr, bool displayMsg)
{
bool flag = true;
if (inStr == null || inStr.Length == 0)
{
outStr = inStr;
flag = true;
}
else if (outStr == null)
{
if (displayMsg)
m_msgBox.UserMsgBox(SharedObjects.MainWin, MsgBox.MsgTypes.Error, "Out String Cannot Be Null\nDeviceFormUtils\n");
flag = false;
}
else if (inValueDisplay == outValueDisplay)
{
outStr = inStr;
}
else
{
string str1 = "";
switch (inValueDisplay)
{
case ValueDisplay.Hex:
try
{
str1 = inStr;
break;
}
catch (Exception ex)
{
if (displayMsg)
m_msgBox.UserMsgBox(SharedObjects.MainWin, MsgBox.MsgTypes.Error, string.Format("Cannot Convert The Incoming String Value From Hex\n\n{0}\n", ex.Message) + "DeviceFormUtils\n");
flag = false;
break;
}
case ValueDisplay.Dec:
try
{
uint bits = Convert.ToUInt32(inStr, 10);
int index1 = 0;
bool dataErr = false;
byte[] data = new byte[4];
m_dataUtils.Load32Bits(ref data, ref index1, bits, ref dataErr, false);
if (dataErr)
throw new ApplicationException("Error Loading 32 Bit Value");
int num = 0;
for (int index2 = data.Length - 1; index2 >= 0 && data[index2] == 0; --index2)
++num;
if (num == 4)
num = 3;
byte[] numArray = new byte[4 - num];
Array.Copy(data, numArray, numArray.Length);
for (int index2 = 0; index2 < numArray.Length; ++index2)
{
str1 = str1 + numArray[index2].ToString("X2");
if (index2 < numArray.Length - 1)
str1 = str1 + ":";
}
break;
}
catch (Exception ex)
{
if (displayMsg)
m_msgBox.UserMsgBox(SharedObjects.MainWin, MsgBox.MsgTypes.Error, string.Format("Cannot Convert The Incoming String Value From Decimal\n\n{0}\n", ex.Message) + "DeviceFormUtils\n");
flag = false;
break;
}
case ValueDisplay.Ascii:
try
{
if (!m_dataUtils.CheckAsciiString(inStr))
throw new ApplicationException("Ascii String Value Contains Unprintable Characters");
byte[] bytesFromAsciiString = m_dataUtils.GetBytesFromAsciiString(inStr);
for (int index = 0; index < bytesFromAsciiString.Length; ++index)
{
str1 = str1 + string.Format("{0:S}", bytesFromAsciiString[index].ToString("X2"));
if (index < bytesFromAsciiString.Length - 1)
str1 = str1 + ":";
}
break;
}
catch (Exception ex)
{
if (displayMsg)
m_msgBox.UserMsgBox(SharedObjects.MainWin, MsgBox.MsgTypes.Error, string.Format("Cannot Convert The Incoming String Value From Ascii\n\n{0}\n", ex.Message) + "DeviceFormUtils\n");
flag = false;
break;
}
default:
if (displayMsg)
m_msgBox.UserMsgBox(SharedObjects.MainWin, MsgBox.MsgTypes.Error, string.Format("Unknown Incoming String Type #{0}\n", inValueDisplay) + "DeviceFormUtils\n");
flag = false;
break;
}
if (flag)
{
string[] strArray = str1.Split(new char[] { ' ', ':' }, StringSplitOptions.RemoveEmptyEntries);
foreach (string str2 in strArray)
{
if (str2.Length == 0)
{
if (displayMsg)
m_msgBox.UserMsgBox(SharedObjects.MainWin, MsgBox.MsgTypes.Error, "Incoming String Conversion Missing Byte In Delimited Format\nDeviceFormUtils\n");
flag = false;
break;
}
else if (str2.Length != 2)
{
if (displayMsg)
m_msgBox.UserMsgBox(SharedObjects.MainWin, MsgBox.MsgTypes.Error, "Incoming String Conversion Not In Single Byte Delimited Format\nDeviceFormUtils\n");
flag = false;
break;
}
}
if (flag)
{
switch (outValueDisplay)
{
case ValueDisplay.Hex:
try
{
outStr = str1;
break;
}
catch (Exception ex)
{
if (displayMsg)
m_msgBox.UserMsgBox(SharedObjects.MainWin, MsgBox.MsgTypes.Error, string.Format("Cannot Convert The Outgoing String Value To Hex\n\n{0}\n", ex.Message) + "DeviceFormUtils\n");
flag = false;
break;
}
case ValueDisplay.Dec:
try
{
foreach (string str2 in strArray)
outStr = outStr + str2;
if (strArray.Length > 4)
throw new ApplicationException("Conversion String Exceeds Four Hex Bytes");
uint num1 = 0U;
for (int index = 0; index < strArray.Length; ++index)
num1 += (uint)Convert.ToByte(strArray[index], 16) << (8 * index);
outStr = string.Format("{0:D}", num1);
Convert.ToUInt32(outStr, 10);
break;
}
catch (Exception ex)
{
if (displayMsg)
m_msgBox.UserMsgBox(SharedObjects.MainWin, MsgBox.MsgTypes.Error, string.Format("Cannot Convert The Outgoing String Value To Decimal\n\n{0}\n", ex.Message) + "DeviceFormUtils\n");
flag = false;
break;
}
case ValueDisplay.Ascii:
try
{
foreach (string str2 in strArray)
outStr += string.Format("{0:S}", Convert.ToChar(Convert.ToByte(str2, 16)).ToString());
if (!m_dataUtils.CheckAsciiString(outStr))
throw new ApplicationException("Ascii String Value Contains Unprintable Characters");
else
break;
}
catch (Exception ex)
{
if (displayMsg)
m_msgBox.UserMsgBox(SharedObjects.MainWin, MsgBox.MsgTypes.Error, string.Format("Cannot Convert The Outgoing String Value To Ascii\n\n{0}\n", ex.Message) + "DeviceFormUtils\n");
flag = false;
break;
}
default:
if (displayMsg)
m_msgBox.UserMsgBox(SharedObjects.MainWin, MsgBox.MsgTypes.Error, string.Format("Unknown Out String Type #{0}\n", outValueDisplay) + "DeviceFormUtils\n");
flag = false;
break;
}
}
}
}
if (outStr != null && !flag)
{
outStr = inStr;
outValueDisplay = inValueDisplay;
}
return flag;
}