Exemple #1
0
        public static int getOctNumber(string octString, Array dest, int offset, int baseSizeInByte = 4, Boolean isSwap = false)
        {
            if (octString == null || octString.Length == 0)
            {
                return(0);
            }

            if (octString[0].Equals('0'))
            {
                octString = octString.Substring(1);
            }
            Int64 octNum  = getOctNumber(octString);
            int   octSize = getIntBytes(octNum);

            if (octSize > baseSizeInByte)
            {
                baseSizeInByte = octSize;
            }

            if (isSwap)
            {
                NetUsable <byte> .arrayToOtherArraySwappedBySize(
                    BitConverter.GetBytes(octNum), 0, dest, offset, baseSizeInByte, sizeof(Int64));
            }
            else
            {
                Buffer.BlockCopy(BitConverter.GetBytes(octNum), sizeof(Int64) - baseSizeInByte, dest, offset, baseSizeInByte);
            }

            return(baseSizeInByte);
        }
Exemple #2
0
        public static int parseNumberToByteArray(string num, Array dest, int offset, int parseTypeSize, Boolean isSwap = false)
        {
            if (num == null || num.Length == 0)
            {
                num = "0";
            }
            //parseTypeSize 는 string을 숫자로 변환시킬 때, 기준이 될 타입의 크기를 정하는 것이다.
            //예를 들어, 1은 byte에도 들어갈 수 있는 숫자이지만, parseTypeSize를 4로 하면 int에 들어가게 된다.

            Byte[] buff = new Byte[8]; //숫자가 가질 수 있는 최대 크기.

            if (getTypeKind(num) == TypeName.HexString)
            {
                int size;
                if (isSwap)
                {
                    size = getHexNumber(num, buff, offset);
                    Buffer.BlockCopy(NetUsable <byte> .getTArrayFromArraySwapped(buff, parseTypeSize, 0, size), 0, dest, offset, size);
                }
                else
                {
                    size = getHexNumber(num, dest, offset);
                }
                return(size);
            }
            else if (getTypeKind(num) == TypeName.OctString)
            {
                return(getOctNumber(num, dest, offset, parseTypeSize, isSwap));
            }
            else if (getTypeKind(num) == TypeName.Float)
            {
                Buffer.BlockCopy(
                    NetUsable <byte> .getTArrayFromArraySwapped(BitConverter.GetBytes(float.Parse(num)), sizeof(float), 0, sizeof(float)),
                    0,
                    dest,
                    offset,
                    sizeof(float));
                return(sizeof(float));
            }
            else if (getTypeKind(num) == TypeName.Integer)
            {
                return(getIntNumber(num, dest, offset, parseTypeSize, isSwap));
            }
            else if (getTypeKind(num) == TypeName.Char)
            {
                Buffer.SetByte(dest, offset, (Byte)(num[0]));
                return(1);
            }
            else //CharArray. char array doesn't need swapping.
            {
                for (int i = 0; i < num.Length; i++)
                {
                    Buffer.SetByte(dest, offset++, (Byte)num[i]);
                }
                return(num.Length);
            }
        }
Exemple #3
0
        private static int getIntNumber(string num, Array dest, int offset, int baseSizeInByte, Boolean isSwap = false)
        {
            if (num == null || num.Length == 0)
            {
                return(0);
            }
            Int64 tempNum = Int64.Parse(num);

            baseSizeInByte = (getIntBytes(tempNum) > baseSizeInByte) ? getIntBytes(tempNum) : baseSizeInByte;
            Byte[] numArr;

            if (baseSizeInByte == 1)
            {
                numArr = BitConverter.GetBytes((byte)tempNum);
            }
            else if (baseSizeInByte == 2)
            {
                numArr = BitConverter.GetBytes((short)tempNum);
            }
            else if (baseSizeInByte == 4)
            {
                numArr = BitConverter.GetBytes((int)tempNum);
            }
            else
            {
                numArr = BitConverter.GetBytes(tempNum);
            }

            if (isSwap)
            {
                NetUsable <byte> .arrayToOtherArraySwappedBySize(numArr, 0, dest, offset, baseSizeInByte, baseSizeInByte);
            }
            else
            {
                Buffer.BlockCopy(numArr, 0, dest, offset, baseSizeInByte);
            }

            return(baseSizeInByte);
        }