pack

Changing on-screen component for JFrame is clunky and messy

亡梦爱人 提交于 2019-12-07 21:36:15
问题 I haven't been able to find a solution to this problem so far through research and may in fact be a problem with JFrame as a whole. Like any desktop application, most times the clicking of a button or "object" presents an action of changing what is currently being displayed. So for example, if I currently have an "input form" on the display frame and then the user clicks a button labeled " submit ," the screen changes to a congratulations screen saying " your form has correctly been submitted

PHP: pack/unpack补遗

江枫思渺然 提交于 2019-12-07 20:47:31
pack/unpack的介绍和使用加上这篇就第三篇了。确实知识点比较多,这篇算是收尾之作吧。仔细去文档上看pack/unpack的格式化字符说明,就会发现s, S, i, I, l, L, f, d都没有对应的大端序和小端序的格式化字符,所以有需要的时候必须自己实现。这个真不知道PHP开发项目组是怎么想的! 而且确实有人在 ‍ ‍ stackoverflow ‍ ‍ 上这么问了,详见: php-pack-format-for-signed-32-int-big-endian 。stackoverflow上的答案比较巧妙,所以我在这里进行借鉴。 L表示无符号长整型,按主机字节序。N表示无符号长整型,大端序。它们都是32位的,所以如果用L和N对同一个整数进行打包,如果结果相等,则本机字节序就是大端序,否则就是小端序。代码如下: <?php define('BIG_ENDIAN', pack('L', 1) === pack('N', 1)); if (BIG_ENDIAN) { echo "大端序"; } else { echo "小端序"; } echo "\n"; $ php -f test.php 小端序 大端序和小端序事实上是相反的字节序,比如要实现无符号短整型的大端序和小端序,可以用s格式化字符先进行打包,再判断大小端来决定是否需要反转字符串,代码如下: <?php

PHP: chr和pack、unpack那些事

眉间皱痕 提交于 2019-12-07 20:47:17
PHP是一门很灵活的语言。正因为它太灵活了,甚至有些怪异,所以大家对它的评价褒贬不一。其实我想说的是,任何一门语言都有它自身的哲学,有它存在的出发点。PHP为Web而生,它以快速上手、快速开发而著称,所以它也常被冠以简单、新手用的语言等标签。我倒不这么认为,所谓选对的工具去做对的事,没有包打天下的语言。而至于说其简单,却也未必。 引子 我之前有篇文详细介绍过pack和unpack: PHP: 深入pack/unpack ,如果有不明白的地方,建议再回过头去看多几遍。现在应该能够写出以下代码: <?php echo pack("C", 97) . "\n"; $ php -f test.php a 但是,为什么会输出'a'呢?虽然我们知道字符'a'的ASCII码就是97,但是pack方法返回的是二进制字符串,为什么不是输出一段二进制而是'a'?为了确认pack方法返回的是一段二进制字符串,这里我对官方的pack的描述截了个图: 确实如此,pack返回包含二进制字符串的数据,接下来详细进行分析。 程序是如何显示字符的 这里所说的'程序',其实是个宏观的概念。 对于在控制台中执行脚本(这里是指PHP作为cli脚本来执行),脚本的输出会写入标准输出(stdin)或标准错误(stderr),当然也有可能会重定向到某个文件描述符。拿标准输出来说,暂且忽略它是行缓冲、全缓冲或者是无缓冲

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

compute CRC of struct in Python

ⅰ亾dé卋堺 提交于 2019-12-07 06:10:29
问题 I have the following struct, from the NRPE daemon code in C: typedef struct packet_struct { int16_t packet_version; int16_t packet_type; uint32_t crc32_value; int16_t result_code; char buffer[1024]; } packet; I want to send this data format to the C daemon from Python. The CRC is calculated when crc32_value is 0 , then it is put into the struct. My Python code to do this is as follows: cmd = '_NRPE_CHECK' pkt = struct.pack('hhIh1024s', 2, 1, 0, 0, cmd) # pkt has length of 1034, as it should

What is the suggested way to cron-automate ZODB packs for a production Plone instance?

微笑、不失礼 提交于 2019-12-06 17:14:25
问题 Looking at plone.org to find a way to periodically pack my instance's ZODB I could only find http://plone.org/documentation/faq/how-do-i-pack-the-zodb that doesn't talk about automated packs, but just manually initiated ones. I know I can simulate the manual pack with wget or curl, but I'd like to know if that is the best practice in use for production sites. 回答1: If you are using ZEO you can add the following to your Crontab to do this: 0 1 * * 6 <path-to-buildout>/bin/zeopack If you don't

What is the meaning of the letters in the output from struct.pack?

浪尽此生 提交于 2019-12-06 14:58:46
问题 when i change number into hex in struct module of python, >>> import struct >>> struct.pack("i",89) 'Y\x00\x00\x00' >>> struct.pack("i",890) 'z\x03\x00\x00' >>> struct.pack("i",1890) 'b\x07\x00\x00' what is the meaning of "Y,z,b" in the output? 回答1: You're not converting to hex. You're packing the integer as binary data... in this case, little-endian binary data. The first characters are just the corresponding ASCII characters to the raw bytes; e.g. 89 is Y , 122 is z , and 98 is b . The

Packing int and float to byte arrays in Julia

巧了我就是萌 提交于 2019-12-06 14:34:17
I am trying to find out how I can pack an integer or float value in to a byte array in Julia. In Python I would just do the following: struct.pack("<q",1) Which would give me '\x01\x00\x00\x00\x00\x00\x00\x00' or for a float: struct.pack("<f",0.1) Is there any package in Julia that provides this? Thanks! See the StrPack package: https://strpackjl.readthedocs.org/en/latest/ This is actually one of the oldest Julia packages around. A couple other options to StrPack.jl : julia> function num2byteA{T<:Union(Float16, Float32, Float64, Signed, Unsigned)}(x::T) iob = IOBuffer() write(iob, x) seekstart

PHP Convert int to HEX

谁说我不能喝 提交于 2019-12-06 12:22:29
How can I get a similar function with pack/unpack (or other short function)? function getHEX($number) { switch($number) { case 0: $ret = "\x00\x00\x00\x00"; break; case 1: $ret = "\x00\x00\x00\x01"; break; case 2: $ret = "\x00\x00\x00\x02"; break; case 3: $ret = "\x00\x00\x00\x03"; break; // (...) default: $ret = "\x00\x00\x00\x00"; } return $ret; } Nambi You could do it with dechex in PHP: <?php echo dechex(10) . "\n"; echo dechex(47); ?> This function has solved my problem pack("H*", sprintf("%08X", $number)); Here is a hint: str_pad(dechex($number), 4, "0", STR_PAD_LEFT) 来源: https:/

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,