RTFM background: http://perldoc.perl.org/functions/sprintf.html Size: h interpret integer as C type "short" or "unsigned short" %u an unsigned integer, in decimal %hu -> unsigned C type "short" This is Exactly the contents of the Perl variable as verified by C #### Test program ouput: Running C:\bin\bb.pl Wed Jan 6 13:26:28 2016 Perl: Pack number 7360 into uint16 and dump to file pack.test.dat hex_dec_bin_dump.exe File pack.test.dat is 2 bytes long Read 2 Bytes from pack.test.dat Int 7360, uint16 7360, hex 0X1CC0, float 7360.0000, binary 1110011000000 BAD_PRINT, Show uint16 as chars: 7360 != +? Argument "+^\" isn't numeric in printf at C:\bin\bb.pl line 179, line 168. Number 7360 Packed into 'S': h 0, d 0, hu 0, b 0, s +? #### Perl Code: sub perl_pack_print() { $ofn='pack.test.dat'; # Output file Name $xres=7360; # X Resolution $pxres=pack("S", $xres); # Xres Packed into USHORT open(O, ">$ofn"); binmode O; print(O "$pxres"); close O; print("Perl: Pack number $xres into uint16 and dump to file $ofn\n"); # Verify that Perl wrote the correct data to disk system("hex_dec_bin_dump.exe $ofn"); # See if Perl can print the '%hu' it just wrote to disk printf("Number $xres Packed into 'S': h %x, d %d, hu %hu, b %b, s %s\n", $pxres, $pxres, $pxres, $pxres, $pxres); } #### C dump verification code: // Dump given file as hex, dec, float, string... // // gcc -Ofast -ffast-math -m64 -march=sandybridge -funroll-loops -fopenmp -flto -lm c:/bin/hex_dec_bin_dump.c -o c:/bin/hex_dec_bin_dump.exe #include "bpbfct.c" void main(int argc, char *argv[]) { FILE *ifp; // FILE pointers to Output and Input file struct stat statbuf; // STAT function buffer long bread=-1; // Bytes Read int fsize=0; // File size from STAT() uint16_t usbuf[99]; // Unsigned Short BUFfer uint8_t *char_8_ptr; // Bytes to bisect/mutilate short char bitb[254]={ 0 }; // Bit Buffer. Should hold ANY integer char ifn[255]={ 0 }; strcpy(ifn, argv[1]); // Input File Name arg stat(ifn, &statbuf); // Stat the file to get its size fsize=statbuf.st_size; // File size in bytes for .raw (RGB?) file printf("\n%s\nFile %s is %d bytes long\n", argv[0], ifn, fsize); ifp=fopen(ifn, "rb"); // Open input file in binary bread=fread((void *)usbuf, 1, 999, ifp); // Read argued file printf("Read %d Bytes from %s\n", bread, ifn); printf("Int %d, uint16 %hu, hex 0X%04X, float %.4f, binary %s\n", usbuf[0], usbuf[0], usbuf[0], (float)usbuf[0], itoa(usbuf[0], (char *)(&bitb), 2)); char_8_ptr=(uint8_t *) usbuf; // Cast ushort buf to uShortER printf("BAD_PRINT, Show uint16 as chars: %hu != %c%c\n\n", usbuf[0], char_8_ptr[0], char_8_ptr[1]); exit(0); } // End Main(). #### $hdr[16]=pack("S", 7360); $hdr[22]=pack("S", 4912); printf("FIXED? HDR[16]=$hdr[16], HDR[22]=$hdr[22]\n"); #### Read 1376 B from d:/pic/misc/gray.1376b.3689x2462.hdr RGB[16]=3689, [22]=2462 FIXED? HDR[16]=└∟, HDR[22]=0‼ #### @hdr=unpack("S*", $hdr); # RAW RGB Unsigned Short array printf("PrInTf:: RGB[16]=%hu, [22]=%d\n", $hdr[16], $hdr[22]); print ("pRInT :: RGB[16]=$hdr[16], [22]=$hdr[22]\n"); => PrInTf:: RGB[16]=3689, [22]=2462 pRInT :: RGB[16]=3689, [22]=2462 #### open(H, "<$hfn"); binmode H; printf("Read $bread B from $hfn\n"); @hdr=unpack("S*", $hdr); # RAW RGB Unsigned Short array #### printf("FIXED? HDR[16]=$hdr[16], HDR[22]=$hdr[22]\n"); => FIXED? HDR[16]=└∟, HDR[22]=0‼