Cosmos/source/Cosmos.Common/ByteToString.cs
Elia Sulimanov 4b5b2fa231 Added new condition check on StrToByteArray
Added new condition check on StrToByteArray to make sure the length of the passed string is divisible by 3
2020-05-28 14:28:31 +03:00

45 lines
1.3 KiB
C#

using System;
using System.Collections.Generic;
using System.Text;
namespace Cosmos.Common
{
static public class ByteToString
{
public static byte[] StrToByteArray(string str)
{
if (str.Length == 0 || !StringHelper.IsNumeric(str) || !StringHelper.IsLengthDivisible(str, 3))
throw new Exception("Invalid string value in StrToByteArray");
byte val;
byte[] byteArr = new byte[str.Length / 3];
int i = 0;
int j = 0;
do
{
val = byte.Parse(str.Substring(i, 3));
byteArr[j++] = val;
i += 3;
}
while (i < str.Length);
return byteArr;
}
public static string ByteArrToString(byte[] byteArr)
{
byte val;
string tempStr = "";
for (int i = 0; i <= byteArr.GetUpperBound(0); i++)
{
val = byteArr[i];
if (val < (byte)10)
tempStr += "00" + val.ToString();
else if (val < (byte)100)
tempStr += "0" + val.ToString();
else
tempStr += val.ToString();
}
return tempStr;
}
}
}