PHP函数需要将十六进制转换为后期&长


PHP function required to convert hex to lat & long

我有一个关于纬度和经度编码的问题,我的大脑拒绝给出答案。

我需要写一个php函数,它接受值'1446041F'和'447D1100' (Lat &Lng)进行一些处理(我无法理解的位)并输出'52.062297'和'0.191030'。

我被告知晚&Lng由带符号的度数、分钟数和十进制分钟数编码为4个字节,格式如下:

Latitude: SDDMM.MMMMM where 0≤DD≤90, S = [+|-], 0≤M≤9
Longitude: SDDDMM.MMMMM where 0≤DDD≤180, S = [+|-], 0≤M≤9

看到最后一点,我已经搜索了很多网站,但我仍然不知道这是什么意思。

我知道这是在黑暗中拍摄的一个巨大的镜头,它可能是如此简单,我被正确地告知坐在角落里戴着笨蛋的帽子,但我快没头发可拔了!

请多多指教。

谢谢,《马太福音》

您给出的例子,1446041F447D1100可能是32位带符号的小端字节顺序整数。它们的阅读方式如下:

1446041F -> 0x1F044614 -> 520373780
447D1100 -> 0x00117D44 -> 001146180

它们可以像这样用度和分钟来解释:

520373780 -> 52 degrees, 03.73780 minutes
1146480 -> 0 degrees, 11.46480 minutes

下面的函数将把指定的十六进制值转换为度数。我假设值是整数,如0x447D1100等。如果我假设错了,输入值实际上是字符串,请告诉我。我把这个函数放到了公有领域。

function hextolatlon($hex){
  // Assume hex is a value like 0x1446041F or 0x447D1100
  // Convert to a signed integer
  $h=$hex&0xFF;
  $h=($h<<8)|(($hex>>8)&0xFF);
  $h=($h<<8)|(($hex>>16)&0xFF);
  $h=($h<<8)|(($hex>>24)&0xFF);
  $negative=($h>>31)!=0; // Get the sign
  if($negative){
   $h=~$h;
   $h=$h&0x7FFFFFFF;
   $h++;
  }
  // Convert to degrees and minutes
  $degrees=floor($h/10000000);
  $minutes=$h%10000000;
  // Convert to full degrees
  $degrees+=($minutes/100000.0) / 60.0;
  if($negative)$degrees=-$degrees;
  return $degrees;
}

下面是PHP代码(为了清晰起见,这里冗长):

function llconv($hex) {
    // Pack hex string:
    $bin = pack('H*', $hex);
    // Unpack into integer (returns array):
    $unpacked = unpack('V', $bin);
    // Get first (and only) element:
    $int = array_shift($unpacked);
    // Decimalize minutes:
    $degmin = $int / 100000;
    // Get degrees:
    $deg = (int)($degmin/100);
    // Get minutes:
    $min = $degmin - $deg*100;
    // Return degress:
    return round($deg + ($min/60), 6);
}
$long = '1446041F';
$lat = '447D1100';
$iLong = llconv($long);
$iLat = llconv($lat);
print "Out: $iLong x $iLat'n";