unpack

PHP: 深入pack/unpack

十年热恋 提交于 2019-12-07 11:09:00
PHP作为一门为web而生的服务器端开发语言,被越来越多的公司所采用。其中不乏大公司,如腾迅、盛大、淘米、新浪等。在对性能要求比较高的项目中,PHP也逐渐演变成一门前端语言,用于访问后端接口。或者不同项目之间需要共享数据的时候,通常可以抽取出数据层,通过PHP来访问。 写在前面的话 本文介绍的是通过二进制数据包的方式通信,演示语言为PHP和Golang。PHP提供了pack/unpack函数来进行二进制打包和二进制解包。在具体讲解之前,我们先来了解一些基础知识。 什么是字节序 在不同的计算机体系结构中,对于数据(比特、字节、字)等的存储和传输机制有所不同,因而引发了计算机领域中一个潜在但是又很重要的问题,即通信双方交流的信息单元应该以什么样的顺序进行传送。如果达不成一致的规则,计算机的通信与存储将会无法进行。目前在各种体系的计算机中通常采用的字节存储机制主要有两种:大端(Big-endian)和小端(Little-endian)。这里所说的大端和小端即是字节序。 MSB和LSB MSB是Most Significant Bit/Byte的首字母缩写,通常译为最重要的位或最重要的字节。它通常用来表示在一个bit序列(如一个byte是8个bit组成的一个序列)或一个byte序列(如word是两个byte组成的一个序列)中对整个序列取值影响最大的那个bit/byte。 LSB是Least

How to create a .BAT file to download and unpack a zip file?

你离开我真会死。 提交于 2019-12-07 05:04:38
问题 How to create a .BAT file to download and unpack a zip file from HTTP server? We have links like http://example.com/folder.zip and absolute folder link like C:\Users\UserName\Some mixed Русский English Adress\ if files from zip exist in directory owerrite them. using only native windows (xp vista win7 etc) BAT functions and files. Could you add code example, please. 回答1: Try this hybrid bat/vbs script @echo off > %temp%\~tmp.vbs echo sUrl = "http://www.unicontsoft.com/file.zip" >> %temp%\~tmp

Including an unpacked War in the Assembly

柔情痞子 提交于 2019-12-07 03:26:57
问题 I have a project which builds a war (no problem). And, that war needs to be packaged with a few shell scripts. Because that war contains properties files that vary from site-to-site, we can't simply install the war as is, but munge the properties files in it. That's what the shell scripts do. I'd like to package my war in my assembly as a unpacked war. I see <unpacked> in the Assembly Descriptor, but I haven't been able to get that to work. Here's my first bin.xml where I just packed the war

How do I unpack a number larger than 64 bits in ruby?

无人久伴 提交于 2019-12-06 09:24:45
Let's say I have an arbitrary string that's ~1000 bytes long. (I'm studying crypto.) How can I unpack that into a BigNum? I understand how to pack it into 8-bit numbers, say, or 32-bit numbers. s='I am a grumpy potato' s.unpack('C*') [73, 32, 97, 109, 32, 97, 32, 103, 114, 117, 109, 112, 121, 32, 112, 111, 116, 97, 116, 111] s.upack('L*') => [1835081801, 1730175264, 1886221682, 1869619321, 1869898100] Or, is there a straightforward way to combine, say, the 8-bit numbers into a BigNum? I could probably unpack into an array of 8-bit numbers and multiply each element of the array by subsequent

Overload * operator in python (or emulate it)

雨燕双飞 提交于 2019-12-06 08:11:08
问题 I want to overload the * operator in python. In C++, you can overload the dereference operator, so that you can create a class with a custom way to respond to *alpha . Part of this question is that I don't know exactly, and I mean EXACTLY, what the * operator (unpacking operator as I call it) does. So how can I overload it, or emulate the overloading of it. Eventually I want to be able to do: *alpha with a custom response and return value. EDIT: I found the solution thanks to Joe Kington's

Packing data of different sizes into a list of unsigned ints

一曲冷凌霜 提交于 2019-12-06 06:40:24
I have a set of data that represents a hardware structure that I need to manipulate in python. The real structure is 4 KB in size...I'll just whip up a quick example: Byte(s) Value 0-1 0x0102 2-3 0x0304 4-23 "AStrWith20Characters" 24-63 "WoahThisStringHas40CharactersItIsHuge!!!" 64-71 "Only8Chr" 72-74 0x050607 74 0x08 75-127 0x00 (padding) The idea is I pack this structure up into a list of 32 bit Ints, pass the list off to another function, and that function then writes the whole shebang out to memory. The Memory Writing function accepts 64 Bytes at a time, so I'd have to make two calls. So,

Temporarily unpack dictionary

岁酱吖の 提交于 2019-12-06 05:43:18
问题 Say, I have a dic like this my_dictionary = {'a':1,'c':5,'b':20,'d':7} Now, I want to do this with my dic: if my_dictionary['a'] == 1 and my_dictionary['d'] == 7: print my_dictionary['c'] This looks ridiculous because I am typing my_dictionary 3 times! So is there any syntax which would allow me to do something like this: within my_dictionary: if a == 1 and d == 7: print c This would actually work if I didn't have anything more (in this case b) in my dic: def f(a,d,c): if a == 1 and d == 7:

simple reading of fortran binary data not so simple in python

喜你入骨 提交于 2019-12-05 23:35:39
I have a binary output file from a FORTRAN code. Want to read it in python. (Reading with FORTRAN and outputting text to read for python is not an option. Long story.) I can read the first record in a simplistic manner: >>> binfile=open('myfile','rb') >>> pad1=struct.unpack('i',binfile.read(4))[0] >>> ver=struct.unpack('d',binfile.read(8))[0] >>> pad2=struct.unpack('i',binfile.read(4))[0] >>> pad1,ver,pad2 (8,3.13,8) Just fine. But this is a big file and I need to do this more efficiently. So I try: >>> (pad1,ver,pad2)=struct.unpack('idi',binfile.read(16)) This won't run. Gives me an error and

Python custom mapping class **unpacking and 'keys' attribute

*爱你&永不变心* 提交于 2019-12-05 13:37:56
I'd like to use a SimpleNameSpace which can also act as a mapping so to be able to be used with ** unpacking. Here is what I've done: class MySimpleNameSpace(object): # my initial attempt subclassed SimpleNameSpace and Mapping, with # possibility to use MySimpleNameSpace as a dict as well as a normal SimpleNameSpace. def __init__(self, **kw): self.__dict__.update(kw) def __getitem__(self, item): return getattr(self, item) def keys(self): return self.__dict__.keys() So far so good: def f(**kw): print(kw) ns = MySimpleNameSpace(a=42) f(**ns) Gives: {'a': 42} More tricky: ns.__getitem__ = "what"

How to create a .BAT file to download and unpack a zip file?

非 Y 不嫁゛ 提交于 2019-12-05 09:15:20
How to create a .BAT file to download and unpack a zip file from HTTP server? We have links like http://example.com/folder.zip and absolute folder link like C:\Users\UserName\Some mixed Русский English Adress\ if files from zip exist in directory owerrite them. using only native windows (xp vista win7 etc) BAT functions and files. Could you add code example, please. Try this hybrid bat/vbs script @echo off > %temp%\~tmp.vbs echo sUrl = "http://www.unicontsoft.com/file.zip" >> %temp%\~tmp.vbs echo sFolder = "c:\temp\unzip" >> %temp%\~tmp.vbs (findstr "'--VBS" "%0" | findstr /v "findstr")