在Javascript/NodeJS中按字节分析文件



我对按字节读取文件并将这些字节转换为可读内容的理解非常迷失。我有一个来自Sierra Charts的.scid文件,这是一个股票/交易平台。scid文件包含股票图表的条形数据数组。数据结构规范如下:https://www.sierrachart.com/index.php?page=doc/IntradayDataFileFormat.html

作为参考,已经将这些文件转换为其他语言的人在这里:

https://gist.github.com/vladaman/8696352

https://www.sierrachart.com/SupportBoard.php?ThreadID=1570

以及Robinhood最近股票的样本文件:

https://drive.google.com/file/d/13gqkb4zK51uq8DraKRxCw22u_53_NW4g/view?usp=sharing

根据我所掌握的信息,我假设,我将读取文件,字节块,然后将这些字节转换为指定的类型。

下面是一些非常非常粗略的测试代码,只是想对有一个基本的了解


fs.open('E://SierraChart/Data/hood.scid', 'r', function(err, fd) {
if (err)
throw err;
var buffer = Buffer.alloc(1);
let count = 0
let header_read = false
let block = ''
while (true) {   
// reading the file, in what i think is byte by byte
var num = fs.readSync(fd, buffer, 0, 1, null);
if (num === 0)
break;
count = parseInt(count) + 1
block = block + buffer[0] + ' '
// per the doc, the first 56 bytes is the header, which we can throw away
if(!header_read && count == 56){
header_read = true
console.log(block + 'n')
block = ''
count = 0
}
// then a data block is represented in 40 byte chunks
else if(header_read && count == 40){
console.log(block + 'n')
block = ''
count = 0
}
}
});

我的输出是这样的:

// header (56 bytes)
83 67 73 68 56 0 0 0 40 0 0 0 1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0        
224 115 51 15 126 161 13 0 0 0 24 66 0 0 24 66 0 0 12 66 52 128 22 66 44 5 0 0 204 234 6 0 252 173 3 0 208 60 3 0 
96 22 202 15 126 161 13 0 225 122 22 66 0 0 33 66 82 184 21 66 194 245 26 66 74 29 0 0 101 126 48 0 139 105 14 0 218 20 34 0 
192 16 93 19 126 161 13 0 143 194 27 66 143 194 27 66 10 215 20 66 71 225 22 66 134 23 0 0 93 247 27 0 5 166 17 0 88 81 10 0 
...

我甚至不确定它看起来是否正确。我希望前8个字节看起来非常相似,因为假设它们代表一个日期,我认为增量是1分钟。从那时起,我真的不知道如何将这些字节转换为其他类型,如浮点和int。

根据我所看到和阅读的内容,我觉得自己甚至没有走上正轨。我不确定我是否正确地读取了文件,或者是否理解了进出的值。这东西太令人困惑了。

每次使用它们提供的头位启动scid文件。他们根据数据类型的大小编写字节。是的,一个人只需通过1个字节就可以获得日期/时间,而其他7个字节则必须留在那里。准确的大小很重要,所以要使用正确的数据类型。如果你严重卡住,请给我发电子邮件

最新更新