核新同花顺数据结构 - 字符串 - 博客园

53 人参与 | 时间:2024年10月18日 15:55:08
点击跳转 www.cnblogs.com
内容
<?php
$content = file_get_contents("400002.day");
function readByteToInt($bytes, $startPos, $length) {
$data = substr($bytes, $startPos, $length);
$result = unpack('v*', $data);
return $result[1];
}
function getValue($value) {
$num = $value & 0xFFFFFFF; // 取低 28 位
$b = ($value >> 28) & 0xF; // 取高 4 位
if (($b & 7) != 0) {
$num2 = pow(10, $b & 7);
if (($b & 8) == 0) {
$num *= $num2; // 乘以 10 的幂
} else {
$num /= $num2; // 除以 10 的幂
}
}
return $num;
}
// 从第 10 个字节开始读取长度为 2 的整数
$recordStartPos = readByteToInt($content, 10, 2);
$recordStartPos1 = readByteToInt($content, 12, 2);
$recordStartPos2 = readByteToInt($content, 14, 2);
// 使用 unpack 从 recordStartPos 开始读取长度为 4 的整数
$arr = [];
$data = substr($content, $recordStartPos, 4);
$arr[] = unpack('i', $data)[1]; // 使用 'i' 表示有符号整数
// 读取开盘价
$openPrice = readByteToInt($content, $recordStartPos + 4, 2) / 1000; // 开盘价
$binFile = substr($content, $recordStartPos + 4, 2);
$amount = unpack('v*', $binFile);
$arr["kaipan"] = $amount;
$arr[] = $openPrice;
// 读取收盘价
$closePrice = readByteToInt($content, $recordStartPos + 8, 2) / 1000; // 收盘价
$arr[] = $closePrice;
// 读取最高价
$highPrice = readByteToInt($content, $recordStartPos + 12, 2) / 1000; // 最高价
$arr[] = $highPrice;
// 读取最低价
$lowPrice = readByteToInt($content, $recordStartPos + 16, 2) / 1000; // 最低价
$arr[] = $lowPrice;
// 读取最低价
// $lowPrice = readByteToInt($content, $recordStartPos + 20, 4); // 最低价
// $arr[] = $lowPrice;
$binFile = substr($content, $recordStartPos + 20, 4);
$amount = (unpack('I', $binFile)[1] & 0xFFFFFFF) * 10;
$arr["nihao"] = $amount;
// $lowPrice = readByteToInt($content, $recordStartPos + 24, 4); // 最低价
// $arr[] = $lowPrice;
// 输出结果
print_r($arr);