in reply to Write 2 uint16_t numbers to file in Perl
That is hard to read and interpret, but basically
use autodie ; open my($outfh),'>:raw', 'stuff'; print $outfh pack 'S<*', @rgb;
@rgb only contains numbers, when you write them to file you do the packing
Also don't use printf as a replacement for print
|
|---|
| Replies are listed 'Best First'. | |||
|---|---|---|---|
|
Re^2: Write 2 uint16_t numbers to file in Perl
by BrowserUk (Patriarch) on Jan 05, 2016 at 09:40 UTC | |||
Also don't use printf as a replacement for print With the rise and rise of 'Social' network sites: 'Computers are making people easier to use everyday'
Examine what is said, not who speaks -- Silence betokens consent -- Love the truth but pardon error.
"Science is about questioning the status quo. Questioning authority". I knew I was on the right track :)
In the absence of evidence, opinion is indistinguishable from prejudice.
| [reply] | ||
by Athanasius (Archbishop) on Jan 05, 2016 at 10:23 UTC | |||
Possibly an allusion to the closing sentence in the documentation of the printf function? Don't fall into the trap of using a printf when a simple print would do. The print is more efficient and less error prone.
| [reply] [d/l] [select] | ||
by BrianP (Acolyte) on Jan 05, 2016 at 17:28 UTC | |||
Root cause of problem found:
Perl misprints a ushort with %hu format as %c%c
Does Perl have a PrintC function which prints C datatypes
EXACTLY as C would print the same BINARY DIGITS without
dismembering multi-byte integers and misprinting as
single-byte character data? %hu means %hu, !%c%c!
This behavior contradicts sprintf documentation:
Size: h interpret integer as C type "short"
It contradicts C behavior; same data, same printf, wrong result
It fails the intelligent assistant test:
What is the value of $Xres? "+^\" is !correct
Working with uint16 image data without a printf which
understands the difference between characters and ushorts
is a problem. Does one keep 2, parallel sets of data,
1 real set for Photoshop and a second, "doctored" set
to print correctly in the log files?
-------------------------------------------- Test case: 1) pack number into 'S', unsigned short, store in $pxres 2) write $pxres to disk in binary mode 3) dump this file in C to verify contents 4) Include misprint of uint16 as 2 uint8s in C output 5) print $pxres variable in Perl with explicit %d, %hu, %s... Results: Perl does pack and dump uint16 containing number 7360 correctly. Contents verified via C program Perl prints the known good uint16 as 2 uint8 chars Perl printf of 7360 with explicit "%hu" format is identical to C printf of uint16 buffer with %c%c I asked a "2 foot section of rope". Assistant delivered 2, "1 foot sections of rope".--------------------------------------------------------
--------------------------------------------------------
-------------------------------------------------------- ------------------------------------------------ =============================================================== =============================================================== Athanasius, I tried the print instead of printf (and changed my variable name from rgb->hdr)
This is NOT a print/f problem:
It read, unpacked and printed raw, binary UINT16 data correctly
All I have been trying to do is: 1) document current value of uint16_t at some_uint16_array16 Try to imagine square brackets around the "<16>" above. 2) Convert ordinary integer <<7360>> to uint16_t 3) Overwrite old, documented value (the 3689) with new 7360 value 4) Confirm in log file that new value prints EXACTLY like old value 5) Write binary blob back to disk with only 2 uint16s changedHow does one PACK the "S"hort value "'7360'" into an array item created with:
A) It was asked to create an Unsigned SHORTfrom reasonable data: $hdr[\16\]=pack("S", 7360); B) It was asked to print it as an Unsigned SHORT as "%hu" C) It was asked to print the Unsigned SHORT as it saw fit D) How can it print this as: The EXTRACTION part of the Pathological Extraction Report Lang is working! The DATA CREATION seems to have fallen less on the PRACTICAL side :) | [reply] [d/l] [select] | ||
by AnomalousMonk (Archbishop) on Jan 05, 2016 at 18:02 UTC | |||
by BrianP (Acolyte) on Jan 05, 2016 at 10:12 UTC | |||
This solution works PERFECTLY as documented
It's Alive! </code> | [reply] [d/l] [select] | ||
by Anonymous Monk on Jan 05, 2016 at 10:25 UTC | |||
What does this mean? This part use print printf("Read $bread B from $hfn\n"); | [reply] [d/l] | ||
by BrowserUk (Patriarch) on Jan 05, 2016 at 12:28 UTC | |||
Right. I missed that. Then again, it wouldn't be that hard for printf to realise that it only had a single parameter and bypass inspecting the template and just print it. (Maybe that is already done.) With the rise and rise of 'Social' network sites: 'Computers are making people easier to use everyday'
Examine what is said, not who speaks -- Silence betokens consent -- Love the truth but pardon error.
"Science is about questioning the status quo. Questioning authority". I knew I was on the right track :)
In the absence of evidence, opinion is indistinguishable from prejudice.
| [reply] | ||