下面的是一段c#的代码.请大牛帮忙转成java的.跪谢.人在
[code="c#"]
public string GetHexChar(string value)
{
string sReturn = string.Empty;
switch (value)
{
case "10":
sReturn = "A";
break;
case "11":
sReturn = "B";
break;
case "12":
sReturn = "C";
break;
case "13":
sReturn = "D";
break;
case "14":
sReturn = "E";
break;
case "15":
sReturn = "F";
break;
default:
sReturn = value;
break;
}
return sReturn;
}
//将10进制字符串转化为16进制字符串
public string ConvertHex(string value)
{
string sReturn = string.Empty;
try
{
while (int.Parse(value) > 16)
{
int v = int.Parse(value);
sReturn = GetHexChar((v % 16).ToString()) + sReturn;
value = Math.Floor(Convert.ToDouble(v / 16)).ToString();
}
sReturn = GetHexChar(value) + sReturn;
if (sReturn.Length == 1)
sReturn = "0" + sReturn;
}
catch
{
return "00";
}
return sReturn;
}
public void CreateHZ(string IMSI, string X, string Y, string speed, string GPS_Dir)
{
try
{
Socket sck_Client = Connect_Server();
string strTel = IMSI.Trim();
byte[] bt_Tel = new byte[16];
int i = 0;
for (i = 0; i < strTel.Length; i++)
{
string strEach = strTel.Substring(i, 1);
int iEach = Int32.Parse(strEach);
iEach = 48 + iEach;
bt_Tel[i] = (byte)iEach;
}
if (strTel.Length < 16)
{
for (int j = 0; j < 16 - strTel.Length; j++)
bt_Tel[i + j] = 0;
}
string strLon = X.Trim();
double dLon = Double.Parse(strLon);
dLon = dLon * 648000000 / 180;
int iLon = Convert.ToInt32(dLon);
string strILon = iLon.ToString();
string strHexLon = ConvertHex(strILon);
if (strHexLon.Length == 7)
strHexLon = "0" + strHexLon;
string strHL1 = strHexLon.Substring(0, 2);
int iHL1 = Convert.ToInt32(strHL1, 16);
byte btHL1 = (byte)iHL1;
string strHL2 = strHexLon.Substring(2, 2);
int iHL2 = Convert.ToInt32(strHL2, 16);
// if (iHL2>128)
// iHL2=iHL2-128;
byte btHL2 = (byte)iHL2;
string strHL3 = strHexLon.Substring(4, 2);
int iHL3 = Convert.ToInt32(strHL3, 16);
// if (iHL3 >128)
// iHL3=iHL3-128;
byte btHL3 = (byte)iHL3;
string strHL4 = strHexLon.Substring(6, 2);
int iHL4 = Convert.ToInt32(strHL4, 16);
byte btHL4 = (byte)iHL4;
string strLat = Y.Trim();
double dLat = Double.Parse(strLat);
dLat = dLat * 324000000 / 90;
int iLat = Convert.ToInt32(dLat);
string strILat = iLat.ToString();
string strHexLat = ConvertHex(strILat);
if (strHexLat.Length == 7)
strHexLat = "0" + strHexLat;
string strHA1 = strHexLat.Substring(0, 2);
int iHA1 = Convert.ToInt32(strHA1, 16);
byte btHA1 = (byte)iHA1;
string strHA2 = strHexLat.Substring(2, 2);
int iHA2 = Convert.ToInt32(strHA2, 16);
// if (iHA2 >128)
// iHA2=iHA2-128;
byte btHA2 = (byte)iHA2;
string strHA3 = strHexLat.Substring(4, 2);
int iHA3 = Convert.ToInt32(strHA3, 16);
byte btHA3 = (byte)iHA3;
string strHA4 = strHexLat.Substring(6, 2);
int iHA4 = Convert.ToInt32(strHA4, 16);
// if (iHA4>128)
// iHA4=iHA4-128;
byte btHA4 = (byte)iHA4;
string strYear = DateTime.Now.AddHours(-8).Year.ToString();
byte bt_Year = 0;
if (strYear.Length == 4)
{
strYear = strYear.Substring(2, 2);
string strYear1 = strYear.Substring(0, 1);
if (strYear1 == "0")
{
strYear = strYear.Substring(1, 1);
}
int iYear = Convert.ToInt32(strYear);
bt_Year = (byte)iYear;
}
string strMonth = DateTime.Now.AddHours(-8).Month.ToString();
byte bt_Month = 0;
int iMonth = Convert.ToInt32(strMonth);
bt_Month = (byte)iMonth;
string strDay = DateTime.Now.AddHours(-8).Day.ToString();
byte bt_Day = 0;
int iDay = Convert.ToInt32(strDay);
bt_Day = (byte)iDay;
string strH = DateTime.Now.AddHours(-8).Hour.ToString();
byte bt_Hour = 0;
int iHour = Convert.ToInt32(strH);
bt_Hour = (byte)iHour;
string strM = DateTime.Now.AddHours(-8).Minute.ToString();
int iMin = Convert.ToInt32(strM);
byte bt_Min = (byte)iMin;
string strS = DateTime.Now.AddHours(-8).Second.ToString();
int iSec = Convert.ToInt32(strS);
byte bt_Sec = (byte)iSec;
string strSpeed = speed.Trim();
double dSpeed = Convert.ToDouble(strSpeed);
int iSpeed = 0;
if (dSpeed <= 1)
iSpeed = 1;
else if (dSpeed > 1)
iSpeed = (int)dSpeed;
int iSp1 = 0;
int iSp2 = 0;
if (iSpeed > 255)
{
iSp1 = iSpeed / 256;
}
iSp2 = iSpeed - iSp1 * 256;
byte bt_Sp1 = (byte)iSp1;
byte bt_Sp2 = (byte)iSp2;
string strDirect = GPS_Dir;
double dDirect = 100 * Convert.ToDouble(strDirect);
int iDirect = (int)dDirect;
int iDir1 = 0;
int iDir2 = 0;
if (iDirect > 255)
{
iDir1 = iDirect / 256;
}
iDir2 = iDirect - iDir1 * 256;
byte bt_Dir1 = (byte)iDir1;
byte bt_Dir2 = (byte)iDir2;
if (sck_Client != null)
{
byte[] byteData = { 35, 35, 85, 83, 101, bt_Tel[0], bt_Tel[1], bt_Tel[2], bt_Tel[3], bt_Tel[4], bt_Tel[5], bt_Tel[6], bt_Tel[7], bt_Tel[8], bt_Tel[9], bt_Tel[10], bt_Tel[11], bt_Tel[12], bt_Tel[13], bt_Tel[14], bt_Tel[15], 99, 00, 22, 00, 00, 00, 33, bt_Year, bt_Month, bt_Day, bt_Hour, bt_Min, bt_Sec, btHL1, btHL2, btHL3, btHL4, btHA1, btHA2, btHA3, btHA4, bt_Sp1, bt_Sp2, bt_Dir1, bt_Dir2, 13, 10 };
//string strSend = Encoding.ASCII.GetString
//byte[] byteMessage = Encoding.GetEncoding("gb2312").GetBytes(strSend);
sck_Client.Send(byteData, byteData.Length, SocketFlags.None);
byte[] recvBytes = new byte[8000];
sck_Client.Shutdown(SocketShutdown.Both);
sck_Client.Close();
}
}
catch (Exception exp)
{
string strExp = exp.Message.ToString();
return;
}
}
}
}
[/code]