• YottaDB: writing a binary file

    From ed de moel@21:1/5 to All on Tue Jan 24 08:09:26 2023
    I'm trying to write a binary file.
    I'm end up with a file that contains the UTF-8 escapes for the bytes with a value between 128 and 255.
    To demonstrate: I tried this:
    Set file="x.tmp"
    Open file:(newversion:stream:nowrap:nodelimiter:chset="M")
    Use file For i=0:1:255 Write $Char(i)
    c file
    the file that gets created has 385 bytes, while I would expect 256.

    Any suggestions?

    --- SoupGate-Win32 v1.05
    * Origin: fsxNet Usenet Gateway (21:1/5)
  • From K.S. Bhaskar@21:1/5 to ed de moel on Wed Jan 25 04:01:43 2023
    On Tuesday, January 24, 2023 at 11:09:28 AM UTC-5, ed de moel wrote:
    I'm trying to write a binary file.
    I'm end up with a file that contains the UTF-8 escapes for the bytes with a value between 128 and 255.
    To demonstrate: I tried this:
    Set file="x.tmp"
    Open file:(newversion:stream:nowrap:nodelimiter:chset="M")
    Use file For i=0:1:255 Write $Char(i)
    c file
    the file that gets created has 385 bytes, while I would expect 256.

    Any suggestions?

    Ed, try $ZCHAR() (https://docs.yottadb.com/ProgrammersGuide/functions.html#zchar) which refers to bytes. $CHAR() (https://docs.yottadb.com/ProgrammersGuide/functions.html#char) refers to characters.

    Regards
    – Bhaskar

    --- SoupGate-Win32 v1.05
    * Origin: fsxNet Usenet Gateway (21:1/5)
  • From K.S. Bhaskar@21:1/5 to K.S. Bhaskar on Wed Jan 25 04:03:15 2023
    On Wednesday, January 25, 2023 at 7:01:45 AM UTC-5, K.S. Bhaskar wrote:
    On Tuesday, January 24, 2023 at 11:09:28 AM UTC-5, ed de moel wrote:
    I'm trying to write a binary file.
    I'm end up with a file that contains the UTF-8 escapes for the bytes with a value between 128 and 255.
    To demonstrate: I tried this:
    Set file="x.tmp"
    Open file:(newversion:stream:nowrap:nodelimiter:chset="M")
    Use file For i=0:1:255 Write $Char(i)
    c file
    the file that gets created has 385 bytes, while I would expect 256.

    Any suggestions?
    Ed, try $ZCHAR() (https://docs.yottadb.com/ProgrammersGuide/functions.html#zchar) which refers to bytes. $CHAR() (https://docs.yottadb.com/ProgrammersGuide/functions.html#char) refers to characters.

    Regards
    – Bhaskar

    Also, remember to set $X to zero before closing the file. Otherwise, you may get a line terminator added at the end.

    Regards
    – Bhaskar

    --- SoupGate-Win32 v1.05
    * Origin: fsxNet Usenet Gateway (21:1/5)
  • From ed de moel@21:1/5 to All on Wed Jan 25 07:14:48 2023
    That works, but it means that instead of
    Write blob
    I have to
    For ii=1:1:$Length(blob) Write $Zchar($Ascii(blob,ii))

    --- SoupGate-Win32 v1.05
    * Origin: fsxNet Usenet Gateway (21:1/5)
  • From K.S. Bhaskar@21:1/5 to ed de moel on Wed Jan 25 07:59:46 2023
    On Wednesday, January 25, 2023 at 10:14:50 AM UTC-5, ed de moel wrote:
    That works, but it means that instead of
    Write blob
    I have to
    For ii=1:1:$Length(blob) Write $Zchar($Ascii(blob,ii))

    Ed, I'm not sure exactly what you are trying to do, but hope this helps:

    for i=1:1:256 s $zextract(x,i)=$zchar(i-1)

    write $zlength(x)
    256
    set f="/tmp/tmp.tmp" open f:(newversion:chset="m")

    use f write x set $x=0 close f

    zsystem "ls -l /tmp/tmp.tmp"
    -rw-r--r-- 1 bhaskar gtc 256 Jan 25 10:58 /tmp/tmp.tmp

    write $zchset
    UTF-8


    Regards
    – Bhaskar

    --- SoupGate-Win32 v1.05
    * Origin: fsxNet Usenet Gateway (21:1/5)
  • From ed de moel@21:1/5 to All on Wed Jan 25 08:10:29 2023
    I'm building a jpg (image) file from a web-upload.
    I'll try your suggestion.

    Thanks,
    Ed

    --- SoupGate-Win32 v1.05
    * Origin: fsxNet Usenet Gateway (21:1/5)
  • From Sam Habiel@21:1/5 to ed de moel on Thu Jan 26 05:26:32 2023
    On Wednesday, January 25, 2023 at 11:10:31 AM UTC-5, ed de moel wrote:
    I'm building a jpg (image) file from a web-upload.
    I'll try your suggestion.

    Thanks,
    Ed
    Ed,

    Unfortunately, reading and writing a binary file is pretty difficult.

    I have code that is working and is in production:

    https://gitlab.com/YottaDB/Util/YDB-Web-Server/-/blob/master/src/_ydbwebapi.m#L118

    Read is:

    1. Open with "FIXED": O PATH:(REWIND:READONLY:FIXED:CHSET="M")
    2. Read length-limited reads: N X F R X#4079:0 S HTTPRSP(C)=X,C=C+1 Q:$ZEOF 3. Close file.

    https://gitlab.com/YottaDB/Util/YDB-Web-Server/-/blob/master/src/_ydbwebrsp.m#L239

    Write is:

    1. Open with STREAM, NOWRAP and CHSET of M: o file:(newversion:stream:nowrap:chset="M")
    2. Write Data
    3. Set $X to zero
    4. Close file.

    Keep in mind that if you are operating in UTF-8 mode (the default), you always need to use the byte operators $ZCHAR, $ZASCII, and $ZLENGTH and $ZPIECE.

    --Sam

    --- SoupGate-Win32 v1.05
    * Origin: fsxNet Usenet Gateway (21:1/5)
  • From ed de moel@21:1/5 to All on Thu Jan 26 09:25:42 2023
    It's tricky indeed.
    In my case, this is what ended up working best:

    -- code that builds the binary blob (this code works for both YottaDB and Iris, so, no $ZChar in there) --
    Open file:(newversion:stream:nowrap:nodelimiter:chset="M") Use file
    For ii=1:1:$Length(blob) Write *$Ascii(blob,ii)
    Set $X=0
    Close file

    (And I'm sure I could have used $ZChar($Ascii(blob,ii)) instead of *$Ascii(blob,ii),
    but with *$Ascii(blob,ii) the code remains just a tad closer to the standard.)

    Thanks for all the help,
    Ed

    --- SoupGate-Win32 v1.05
    * Origin: fsxNet Usenet Gateway (21:1/5)