pack

What's does “machine byte order” in PHP pack mean? [closed]

不羁岁月 提交于 2019-12-02 10:01:24
I understand little & big endian, but What's "machine byte order" mean? In pack the phrase "machine byte order" means that the endianess is determined by the current machine 1 PHP itself makes no guarantees as to which endianness such characters (e.g. S , L ) encode data, except as the ordering relates to the current machine . Therefor, be cautious with using "machine byte order" pack characters and consider the guaranteed-order counter-parts (e.g. n , v ) if there is every any doubt 1 . However, pay attention to the target data specification as some silly formats like [Microsoft] UUIDs are

Perl's pack function equivalent in Java

和自甴很熟 提交于 2019-12-01 23:57:55
I have some Perl code that I need to transpose in Java. In this code I have to deal with Perl's pack . Is there an equivalent function in Java? The Perl code looks something like this: $somevar = pack "H*", $vartopack; artaxerxe Perl's pack / unpack functions are a highly versatile conversion utility with its own format syntax (used in H* here, which makes it take an arbitrarily long hex string as input) of which there is no direct equivalent in the Java world. However, to translate... $somevar = pack "H*", $vartoconvert; ...to Java, you can for example use: byte[] somevar = javax.xml.bind

PHP Pack/unpack - can it handle variable length strings

倖福魔咒の 提交于 2019-12-01 21:40:44
I've been trying to figure out if the PHP implementation of Pack/Unpack can do something that the Perl version is able to do. The example I'd like to be able to do in PHP is: http://perldoc.perl.org/perlpacktut.html#String-Lengths # pack a message: ASCIIZ, ASCIIZ, length/string, byte my $msg = pack( 'Z* Z* C/A* C', $src, $dst, $sm, $prio ); # unpack ( $src, $dst, $sm, $prio ) = unpack( 'Z* Z* C/A* C', $msg ); What this Perl code does is described as: Combining two pack codes with a slash (/) associates them with a single value from the argument list. In pack, the length of the argument is

Reverting unpack('C*', “string”)

南笙酒味 提交于 2019-12-01 17:40:48
I would like to know how I can reverse what this unpack function bellow performed. I think the pack function is able to reverse what unpack performed, however I'm not sure. First I have a simple string which after unpacking it I would have an array of bytes representing such string. Now I would like to know how to reverse such array back to the original string. <?php $array = unpack('C*', "odd string"); /*Output: Array ( [1] => 111 [2] => 100 [3] => 100 [4] => 32 [5] => 115 [6] => 116 [7] => 114 [8] => 105 [9] => 110 [10] => 103 )*/ $string = pack("which format here?", $array); echo $string;

struct.unpack(struct.pack(float)) has roundoff error?

孤者浪人 提交于 2019-12-01 12:01:55
问题 When testing my library, Construct, I found out that tests fail when numbers are built then parsed back to a float. Should floats not be represented exactly as in-memory floats? In [14]: d = struct.Struct("<f") In [15]: d.unpack(d.pack(1.23)) Out[15]: (1.2300000190734863,) 回答1: Floating point is inherently imprecise, but you are packing a double-precision float ( binary64 ) into a single-precision ( binary32 ) space there. See Basic and interchange formats in the Wikipedia article on IEEE

Error in using `struct.pack` for writing data to file

给你一囗甜甜゛ 提交于 2019-12-01 08:41:15
I have a numpy.ndarray sample of numbers, each between 1 and 2**20 . I'd like to write it into a binary file, such that each element is represented by four bytes. However, the resulting file size is different from 4 times the size of the sample. This is the code I'm using: outputFile = open('testDS', 'w') print len(sample) if (outputFile is not None): for s in sample: assert(s < 2**20) r = struct.pack("i", s) assert(len(r) == 4) outputFile.write(r) outputFile.close() The output I'm getting (the size of the sample) is: 1000 However, the resulting file size is 4026 bytes. Any ideas why the file

Error in using `struct.pack` for writing data to file

有些话、适合烂在心里 提交于 2019-12-01 05:40:35
问题 I have a numpy.ndarray sample of numbers, each between 1 and 2**20 . I'd like to write it into a binary file, such that each element is represented by four bytes. However, the resulting file size is different from 4 times the size of the sample. This is the code I'm using: outputFile = open('testDS', 'w') print len(sample) if (outputFile is not None): for s in sample: assert(s < 2**20) r = struct.pack("i", s) assert(len(r) == 4) outputFile.write(r) outputFile.close() The output I'm getting

What is application's site of origin and when to use it

岁酱吖の 提交于 2019-11-30 17:22:58
What is application's site of origin When to use it How is the build action of a resource file associated with it What is the difference between pack://application:,,, and pack://siteoforigin:,,, The site of origin is the location (i.e. the physical folder) of the application executable assembly (i.e. the .exe that the user runs). The URI is thus relative to that folder. Example: You have C:\Programs\MyApp\MyApp.exe C:\Programs\MyApp\MyIcon.bmp C:\Programs\MyApp\Icons\MyOtherIcon.bmp The pack URIs are pack://siteoforigin:,,,/MyIcon.bmp and pack://siteoforigin:,,,/Icons/MyOtherIcon.bmp when

Lua - packing IEEE754 single-precision floating-point numbers

╄→гoц情女王★ 提交于 2019-11-30 09:08:36
I want to make a function in pure Lua that generates a fraction (23 bits), an exponent (8 bits), and a sign (1 bit) from a number, so that the number is approximately equal to math.ldexp(fraction, exponent - 127) * (sign == 1 and -1 or 1) , and then packs the generated values into 32 bits. A certain function in the math library caught my attention: The frexp function breaks down the floating-point value (v) into a mantissa (m) and an exponent (n), such that the absolute value of m is greater than or equal to 0.5 and less than 1.0, and v = m * 2^n. Note that math.ldexp is the inverse operation.

When would you use unpack('h*' …) or pack('h*' …)?

南楼画角 提交于 2019-11-30 08:53:30
In Perl, pack and unpack have two templates for converting bytes to/from hex: h A hex string (low nybble first). H A hex string (high nybble first). This is best clarified with an example: use 5.010; # so I can use say my $buf = "\x12\x34\x56\x78"; say unpack('H*', $buf); # prints 12345678 say unpack('h*', $buf); # prints 21436587 As you can see, H is what people generally mean when they think about converting bytes to/from hexadecimal. So what's the purpose of h ? Larry must have thought someone might use it, or he wouldn't have bothered to include it. Can you give a real-world example where