将十六进制转换为经纬度所需的 PHP 函数长的

发布于 2024-11-30 19:37:37 字数 459 浏览 0 评论 0原文

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

我需要编写一个php函数,它采用值“1446041F”和“447D1100”(纬度和经度)进行一些处理(我无法理解的位)并输出“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

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

我知道这是在黑暗中拍摄的一个巨大镜头,而且可能很简单,我被正确地告知要戴着笨蛋帽子坐在角落里,但我的头发已经不多了!

非常感谢任何建议。

谢谢, 马修

I have a question regarding lattitude and longitude encoding, the answer to which my brain refuses to produce.

I need to write a php function that takes the value '1446041F' and '447D1100' (Lat & Lng) does some processing (the bit I cannot fathom) and outputs '52.062297' and '0.191030'.

I am told the Lat & Lng are encoded to 4 bytes from a signed degrees, minutes and decimal minutes with a format as follows;

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

See that last bit, I've searched many sites but I still have no idea what that all means.

I'm aware this is a massive shot in the dark and it may be so simple that I am rightfully told to sit in the corner wearing the dunce hat but I am running low on hair to pull out!

Any advice is much appreciated.

Thanks,
Matthew

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

謸气贵蔟 2024-12-07 19:37:37

您给出的示例 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;
}

The examples you gave, 1446041F and 447D1100 are probably 32-bit signed integers in little-endian byte order.
They are to be read as follows:

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

They can be interpreted in degrees and minutes like this:

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

The following function will convert the hex values you specified to degrees. I assume the values are integers like 0x447D1100 and the like. If I assume wrong and the input values are actually strings, let me know. I put this function into the public domain.

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;
}
2024-12-07 19:37:37

这是 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";

Here's the PHP (the verbosity is for clarity):

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";
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文