package com.njcn.csharmonic.offline.log;

import java.nio.ByteBuffer;
import java.nio.ByteOrder;

public class Service
{
	//C# TO JAVA CONVERTER TODO TASK: There is no preprocessor in Java:
	///#region 解析通讯报文相关代码
//C# TO JAVA CONVERTER TODO TASK: There is no preprocessor in Java:
	///#region 整型->浮点  浮点->整型
	public static float IntToFloat(int num)
	{
//                        uint i_middle = 0xffff0000;
//                        uint j_middle = 0x0000ffff;
//                        bool bNav = false;
//                        if ((num & 0x8000000) == 0x8000000)
//                        {
//                            int nTemp = num & 0x7fffffff;
//                            num = ~nTemp + 1;
//                            bNav = true;
//                        }
//                        int i = (int)((num & i_middle) / 65536);
//                        float j = (num & j_middle) / 65536.0f;
//                        if (bNav)
//                            return -((float)i + j);
//                        return (float)i + j;
		float j = (float)num / 65536.0f;
		return (float)j;
	}
	public static float IntToFloat1000(int num)
	{
//                        uint i_middle = 0xffff0000;
//                        uint j_middle = 0x0000ffff;
//                        bool bNav = false;
//                        if ((num & 0x8000000) == 0x8000000)
//                        {
//                            int nTemp = num & 0x7fffffff;
//                            num = ~nTemp + 1;
//                            bNav = true;
//                        }
//                        int i = (int)((num & i_middle) / 65536);
//                        float j = (num & j_middle) / 65536.0f;
//                        if (bNav)
//                            return -((float)i + j);
//                        return (float)i + j;
		float j = (float)num / 1000.0f;
		return (float)j;
	}
	//C# TO JAVA CONVERTER WARNING: Unsigned integer types have no direct equivalent in Java:
//ORIGINAL LINE: public static float UintToFloat(uint num)
	public static float UintToFloat(int num)
	{
//C# TO JAVA CONVERTER WARNING: Unsigned integer types have no direct equivalent in Java:
//ORIGINAL LINE: uint i_middle = 0xffff0000;
		int i_middle = 0xffff0000;
//C# TO JAVA CONVERTER WARNING: Unsigned integer types have no direct equivalent in Java:
//ORIGINAL LINE: uint j_middle = 0x0000ffff;
		int j_middle = 0x0000ffff;
		boolean bNav = false;
		if ((num & 0x8000000) == 0x8000000)
		{
//C# TO JAVA CONVERTER WARNING: Unsigned integer types have no direct equivalent in Java:
//ORIGINAL LINE: uint nTemp = num & 0x7fffffff;
			int nTemp = num & 0x7fffffff;
			num = ~nTemp + 1;
			bNav = true;
		}
		int i = (int)((num & i_middle) / 65536);
		float j = (num & j_middle) / 65536.0f;
		if (bNav)
		{
			return -((float)i + j);
		}
		return (float)i + j;
	}
	//C# TO JAVA CONVERTER WARNING: Unsigned integer types have no direct equivalent in Java:
//ORIGINAL LINE: public static float UshorToFloat(ushort num)
	public static float UshorToFloat(short num)
	{
//C# TO JAVA CONVERTER WARNING: Unsigned integer types have no direct equivalent in Java:
//ORIGINAL LINE: ushort i_middle = 0xff00;
		short i_middle = (short) 0xff00;
//C# TO JAVA CONVERTER WARNING: Unsigned integer types have no direct equivalent in Java:
//ORIGINAL LINE: ushort j_middle = 0x00ff;
		short j_middle = 0x00ff;
		boolean bNav = false;
		if ((num & 0x8000) == 0x8000)
		{
//C# TO JAVA CONVERTER WARNING: Unsigned integer types have no direct equivalent in Java:
//ORIGINAL LINE: ushort nTemp = (ushort)(num & 0x7fff);
			short nTemp = (short)(num & 0x7fff);
			num = (short)(~nTemp + 1);
			bNav = true;
		}
		int i = (num & i_middle) / 256;
		float j = (num & j_middle) / 256.0f;
		if (bNav)
		{
			return -((float)i + j);
		}
		return (float)i + j;
	}

	//C# TO JAVA CONVERTER WARNING: Unsigned integer types have no direct equivalent in Java:
//ORIGINAL LINE: public static ushort FloatToShort(float num)
	public static short FloatToShort(float num)
	{
//C# TO JAVA CONVERTER WARNING: Unsigned integer types have no direct equivalent in Java:
//ORIGINAL LINE: ushort i = (ushort)num;
		short i = (short)num;
		float middle_j = num - (float)i;
		float j = 256 * middle_j;
		return (short)(i + (short)j);
	}
	//C# TO JAVA CONVERTER WARNING: Unsigned integer types have no direct equivalent in Java:
//ORIGINAL LINE: public static float UshorToFloat10000(ushort num)
	public static float UshorToFloat10000(short num)
	{
		float j = num / 10000.0f;
		return j;
	}

	//C# TO JAVA CONVERTER WARNING: Unsigned integer types have no direct equivalent in Java:
//ORIGINAL LINE: public static ushort FloatToShort10000(float num)
	public static short FloatToShort10000(float num)
	{
//C# TO JAVA CONVERTER WARNING: Unsigned integer types have no direct equivalent in Java:
//ORIGINAL LINE: ushort i = (ushort)(num * 10000.0f);
		short i = (short)(num * 10000.0f);
		return i;
	}
	//C# TO JAVA CONVERTER WARNING: Unsigned integer types have no direct equivalent in Java:
//ORIGINAL LINE: public static float UshorToFloat100(ushort num)
	public static float UshorToFloat100(short num)
	{
		float j = num / 100.0f;
		return j;
	}
	public static float ShorToFloat100(short num)
	{
		float j = num / 100.0f;
		return j;
	}
	public static float ShorToFloat1000(short num)
	{
		float j = num / 1000.0f;
		return j;
	}
	public static float ShorToFloat10000(short num)
	{
		float j = num / 10000.0f;
		return j;
	}

	public static void reverseByteArray(byte[] arr) {
		int start = 0;
		int end = arr.length - 1;
		while (start < end) {
			byte temp = arr[start];
			arr[start] = arr[end];
			arr[end] = temp;
			start++;
			end--;
		}
	}

	//C# TO JAVA CONVERTER TODO TASK: There is no preprocessor in Java:
	///#endregion
	public static byte[] ByteReversa(byte[] temp) //翻转Byte数组
	{

		reverseByteArray(temp);
		return temp;
	}
	public enum enum_Value_type
	{
		Value_uint(1),
		Value_int(2),
		Value_ushort(3),
		Value_short(4),
		Value_float(5),
		Value_Long(6),
		Value_UInt64(7);

		private int intValue;
		private static java.util.HashMap<Integer, enum_Value_type> mappings;
		private synchronized static java.util.HashMap<Integer, enum_Value_type> getMappings()
		{
			if (mappings == null)
			{
				mappings = new java.util.HashMap<Integer, enum_Value_type>();
			}
			return mappings;
		}

		private enum_Value_type(int value)
		{
			intValue = value;
			enum_Value_type.getMappings().put(value, this);
		}

		public int getValue()
		{
			return intValue;
		}

		public static enum_Value_type forValue(int value)
		{
			return getMappings().get(value);
		}
	}


	/**
	 @brief 将二进制流的数组转换为制定的数据类型,增加字节序的转换
	 //	 @param byte[] in 二进制流数据
	 //	 @param int in 需转换的数据在二进制流的位置
	 @return uint out 32位无符号整型数据

	 */
//C# TO JAVA CONVERTER WARNING: Unsigned integer types have no direct equivalent in Java:
//ORIGINAL LINE: public static uint convertUInt32_net(byte[] b, int haveread)
	public static int convertUInt32_net(byte[] b, int haveread)
	{
		String s1 = (new Byte(b[haveread])).toString();
		String s2 = (new Byte(b[haveread + 1])).toString();
		String s3 = (new Byte(b[haveread + 2])).toString();
		String s4 = (new Byte(b[haveread + 3])).toString();
		return (int)convertInt32(s1, s2, s3, s4);
	}
	/**
	 @brief 将二进制流的数组转换为制定的数据类型
	 //	 @param byte[] in 二进制流数据
	 //	 @param int in 需转换的数据在二进制流的位置
	 @return uint out 32位无符号整型数据

	 */
//C# TO JAVA CONVERTER WARNING: Unsigned integer types have no direct equivalent in Java:
//ORIGINAL LINE: public static uint convertUInt32(byte[] b, int haveread)
	public static int convertUInt32(byte[] b, int haveread)
	{
		String s1 = (new Byte(b[haveread + 3])).toString();
		String s2 = (new Byte(b[haveread + 2])).toString();
		String s3 = (new Byte(b[haveread + 1])).toString();
		String s4 = (new Byte(b[haveread])).toString();
		return (int)convertInt32(s1, s2, s3, s4);
	}
	/**
	 @brief 将二进制流的数组转换为制定的数据类型,增加字节序的转换
	 //	 @param byte[] in 二进制流数据
	 //	 @param int in 需转换的数据在二进制流的位置
	 @return int out 32位整型数据

	 */
	public static int convertInt32_net(byte[] b, int haveread)
	{
		String s1 = (new Byte(b[haveread])).toString();
		String s2 = (new Byte(b[haveread + 1])).toString();
		String s3 = (new Byte(b[haveread + 2])).toString();
		String s4 = (new Byte(b[haveread + 3])).toString();
		return convertInt32(s1, s2, s3, s4);
	}
	/**
	 @brief 将二进制流的数组转换为制定的数据类型
	 //	 @param byte[] in 二进制流数据
	 //	 @param int in 需转换的数据在二进制流的位置
	 @return int out 32位整型数据

	 */
	public static int convertInt32(byte[] b, int haveread)
	{
		String s1 = (new Byte(b[haveread + 3]) & 0xFF)+"";
		String s2 = (new Byte(b[haveread + 2])& 0xFF)+"";
		String s3 = (new Byte(b[haveread + 1])& 0xFF)+"";
		String s4 = (new Byte(b[haveread])& 0xFF)+"";
		return convertInt32(s1, s2, s3, s4);
	}
	/**
	 @brief 将字符型的数据转换无符号的整数
	 //	 @param string in 高位字符
	 //	 @param string in 低位字符
	 @return int out 16位无符号整型数据

	 */
	public static int convertInt32(String s1, String s2, String s3, String s4)
	{
		int nValue = (Integer.parseInt(s1, 10) * 256 * 256 * 256 + Integer.parseInt(s2, 10) * 256 * 256 + Integer.parseInt(s3, 10) * 256 + Integer.parseInt(s4, 10));
		return nValue;
	}
	/**
	 @brief 将二进制流写入到数组中
	 //	 @param byte[] in 待转换的二进制流
	 //	 @param int in 已读取的二进制个数
	 //	 @param int[] in 需写入的数组
	 @return int out 总共转换了多少个byte

	 */


	/**
	 @brief 将二进制流写入到数组中
	 //	 @param byte[] in 待转换的二进制流
	 //	 @param int in 已读取的二进制个数
	 //	 @param uint[] in 需写入的数组
	 @return int out 总共转换了多少个byte

	 */

	/**
	 @brief 将二进制流的数组转换为制定的数据类型
	 //	 @param byte[] in 二进制流数据
	 //	 @param int in 需转换的数据在二进制流的位置
	 @return float out 32位浮点数据

	 */
	public static float convertfloat(byte[] b, int haveread)
	{
		// 确保有足够的数据来读取一个float（4字节）
		if (haveread + 4 > b.length) {
			throw new IndexOutOfBoundsException("Not enough bytes to read a float.");
		}

		// 创建一个ByteBuffer来处理字节
		ByteBuffer buffer = ByteBuffer.wrap(b, haveread, 4);
		// 设置字节顺序，假设是大端序（与许多C#系统和网络协议相同）
		buffer.order(ByteOrder.BIG_ENDIAN);

		// 从ByteBuffer中读取float
		float fValue = buffer.getFloat();

		return fValue;
	}
	/**
	 @brief 将二进制流的数组转换为制定的数据类型,增加字节序的转换
	 //	 @param byte[] in 二进制流数据
	 //	 @param int in 需转换的数据在二进制流的位置
	 @return float out 32位浮点数据

	 */
	public static float convertfloat_net(byte[] b, int haveread)
	{
		// 确保有足够的数据来读取一个float（4字节）
		if (haveread + 4 > b.length) {
			throw new IndexOutOfBoundsException("Not enough bytes to read a float.");
		}

		// 创建一个ByteBuffer来处理字节
		ByteBuffer buffer = ByteBuffer.wrap(b, haveread, 4);
		// 设置字节顺序，假设是大端序（与许多C#系统和网络协议相同）
		buffer.order(ByteOrder.BIG_ENDIAN);

		// 从ByteBuffer中读取float
		float fValue = buffer.getFloat();

		return fValue;
	}
	/**
	 @brief 将二进制流写入到数组中
	 //	 @param byte[] in 待转换的二进制流
	 //	 @param int in 已读取的二进制个数
	 //	 @param float[] in 需写入的数组
	 @return int out 总共转换了多少个byte

	 */


//C# TO JAVA CONVERTER WARNING: Unsigned integer types have no direct equivalent in Java:
//ORIGINAL LINE: public static ushort convertUInt16_net(byte[] b, int haveread)
//	public static short convertUInt16_net(byte[] b, int haveread)
//	{
//		String s1 = (new Byte(b[haveread])).toString();
//		String s2 = (new Byte(b[haveread + 1])).toString();
//		return (short)convertInt16(s1, s2);
//	}

	public static short convertUInt16_net(byte[] b, int offset) {
		// 确保offset是有效的，并且偏移量之后至少还有一个字节
		if (b == null || offset < 0 || offset + 1 >= b.length) {
			throw new IndexOutOfBoundsException("Invalid offset or array length");
		}

		// 假设字节顺序是大端的（网络字节序通常是大端的），我们需要先读取高字节再读取低字节
		// Java的byte类型是有符号的，但我们可以使用无符号右移（>>>）来确保结果是无符号的
		return (short) (((b[offset] & 0xFF) << 8) | (b[offset + 1] & 0xFF));

		// 如果需要确保结果是正的（即无符号的），并且不会超出short的范围，可以返回强制类型转换后的short
		// 但请注意，如果结果超过了short的最大值（32767），这里会丢失高位的信息
		// 在大多数情况下，这不会是一个问题，因为网络数据通常被设计为不会超出这些范围
//		return (short) result;
	}
	/**
	 @brief 将二进制流的数组转换为制定的数据类型
	 //	 @param byte[] in 二进制流数据
	 //	 @param int in 需转换的数据在二进制流的位置
	 @return int out 16位无符号整型数据

	 */
//C# TO JAVA CONVERTER WARNING: Unsigned integer types have no direct equivalent in Java:
//ORIGINAL LINE: public static ushort convertUInt16(byte[] b, int haveread)
	public static short convertUInt16(byte[] b, int haveread)
	{
		String s1 = (new Byte(b[haveread + 1])).toString();
		String s2 = (new Byte(b[haveread])).toString();
		return (short)convertInt16(s1, s2);
	}
	/**
	 @brief 将二进制流的数组转换为制定的数据类型
	 //	 @param byte[] in 二进制流数据
	 //	 @param int in 需转换的数据在二进制流的位置
	 @return int out 16位整型数据

	 */
	public static short convertInt16_net(byte[] b, int haveread)
	{
		String s1 = (new Byte(b[haveread])).toString();
		String s2 = (new Byte(b[haveread + 1])).toString();
		return convertInt16(s1, s2);
	}
	/**
	 @brief 将二进制流的数组转换为制定的数据类型
	 //	 @param byte[] in 二进制流数据
	 //	 @param int in 需转换的数据在二进制流的位置
	 @return int out 16位整型数据

	 */
	public static short convertInt16(byte[] b, int haveread)
	{
		String s1 = (new Byte(b[haveread + 1]) & 0xFF)+"";
		String s2 = (new Byte(b[haveread]) & 0xFF)+"";
		return convertInt16(s1, s2);
	}
	/**
	 @brief 将字符型的数据转换无符号的整数
	 //	 @param string in 高位字符
	 //	 @param string in 低位字符
	 @return int out 16位无符号整型数据

	 */
	public static short convertInt16(String s1, String s2)
	{
		short Value = (short)(Short.parseShort(s1, 10) * 256 + Integer.parseInt(s2, 10));
		return Value;
	}
	//解析结构体
//	public static Object BytesToStuct(byte[] bytes, Class type)
//	{
//		//得到结构体的大小
//		int size = Marshal.SizeOf(type);
//		//byte数组长度小于结构体的大小
//		if (size > bytes.length)
//		{
//			//返回空
//			return null;
//		}
//		//分配结构体大小的内存空间
//		IntPtr structPtr = Marshal.AllocHGlobal(size);
//		//将byte数组拷到分配好的内存空间
//		Marshal.Copy(bytes, 0, structPtr, size);
//		//将内存空间转换为目标结构体
//		Object obj = Marshal.PtrToStructure(structPtr, type);
//		//释放内存空间
//		Marshal.FreeHGlobal(structPtr);
//		//返回结构体
//		return obj;
//	}

//C# TO JAVA CONVERTER TODO TASK: There is no preprocessor in Java:
	///#endregion
}