Question
CM Wang · Jun 27, 2018

Parse binary data by InterSystems ObjectScript

I am trying to read some binary data from a local file or through socket.

The binary data is like H.264 codec, I need to read data BYTE by BYTE (even BIT by BIT) and decide the next step based on the data read so far.

I check the documentation and it seems like most of the sample focus on human readable IO, ie: LINE by LINE.

Could I achieve my goal through COS?

Thanks. 

0
0 902
Discussion (3)1
Log in or sign up to continue

you have the option to read by character  using

READ *var

Then you read exactly 1 byte in its  decimal representation 

set file="C:\ZZ\myfile.bin"
open file:"RS":0  else  write "file not open", ! quit
for  use file read *byte use 0 write byte,?5,"x\"_$ZHEX(byte),?12,"char:",$C(byte),!

Docs  on READ

You may also use %File Class and use a Read method with length 1

In addition to other answers, if want to operate with bits, you can use functions:

  • $zhex - converts integer to hex and vice versa, be careful with type of variables. If you pass string it will convert from hex to dec, if you will pass integer it will convert from dec to hex.
  • $factor - converts integer to a $bit string
  • $bit (with bunch of $bit* functions) - operates with bitstrings.
  • You may possible need some encryption functions, you can find
  • And possible zlib, $system.Util.Compress and $system.Util.Decompress from class %SYSTEM.Util 

If you are going to work with H.264, I would not recommend to try to implement it in COS, it will not be fast enough. I'm not very know with this technology but I'm sure I know, it is too complicated and encoding should work in GPU. But it is impossible with Caché. I don't know why exactly do you need it, but I would recommend you to look at external tool ffmpeg, which is very useful and leader on work with video. And I think you can connect ffmpeg.dll, to work from Caché with $zf functions.