简体   繁体   English

Python ctypes.c_int到Perl XS(或非XS)的转换(JavaScript签名的32位按位移位运算符转换)

[英]Python ctypes.c_int to Perl XS (or non-XS) conversion (Javascript signed 32-bit bitwise shift operator conversion)

Background 背景

We need to convert a Javascript hashing algorithm into Perl code. 我们需要将Javascript哈希算法转换为Perl代码。 Therefore, we need to convert Javascript's bitwise shift operators <<, >>, and >>> into Perl. 因此,我们需要将Javascript的按位移位运算符<<,>>和>>>转换为Perl。 So far, we have the algorithms for doing the conversion, but since Javascript bitwise shift operators operate on 32-bit integers, we also need to emulate this in Perl. 到目前为止,我们已经具有进行转换的算法,但是由于Javascript逐位移位运算符可对32位整数进行操作,因此我们还需要在Perl中进行仿真。

Python solution Python解决方案

Based on this post https://stackoverflow.com/a/41610348 we learned that we can do this in Python using ctypes. 根据这篇帖子https://stackoverflow.com/a/41610348,我们了解到可以使用ctypes在Python中完成此操作。 For example, to left-shift an integer by x bits: 例如,要将整数左移x位:

import ctypes
print (ctypes.c_int(integer << x ^ 0).value)

Perl question Perl问题

My understanding is that we need to use XS to do this. 我的理解是,我们需要使用XS来做到这一点。 My question is whether anyone has a quick solution to implementing it. 我的问题是,是否有人可以快速实施该解决方案。 We don't know XS. 我们不知道XS。 We could start learning it, but from my impression of it, the learning curve is pretty high and it could take a while to gain any mastery of it. 我们可以开始学习它,但是从我的印象中,学习曲线相当高,可能需要一段时间才能精通它。 Of course, a non-XS solution would be ideal, if one exists. 当然,如果有的话,非XS解决方案将是理想的。 Any solutions or hints would be greatly appreciated. 任何解决方案或提示将不胜感激。

Workaround 解决方法

Since we have a Python solution already, we could implement this module in Python and then call it from Perl. 由于我们已经有了Python解决方案,因此可以在Python中实现此模块,然后从Perl中调用它。 Performance isn't really an issue, so this "hack" is acceptable, although somewhat undesirable. 性能并不是真正的问题,因此这种“ hack”是可以接受的,尽管有些不可取。 In other words, we would prefer to maintain the whole program (which consists of several modules) in Perl only. 换句话说,我们宁愿只在Perl中维护整个程序(由几个模块组成)。

sub lshr32 { ( $_[0] & 0xFFFFFFFF ) >> $_[1] }                           # >>> in JS
sub lshl32 { ( $_[0] << $_[1] ) & 0xFFFFFFFF }

sub ashr32 { ( $_[0] - ( $_[0] % ( 1 << $_[1] ) ) ) / ( 1 << $_[1] ) }   # >> in JS
sub ashl32 { unpack "l", pack "l", $_[0] * ( 1 << $_[1] ) }              # << in JS

It doesn't make sense to pass a negative number to a logical shift unless the number isn't really a number but a collection of bits. 除非数字不是真正的数字而是位的集合,否则将负数传递给逻辑移位是没有意义的。 Given that you are porting a hashing algorithm, this is very likely. 假设您要移植哈希算法,则很有可能。 It also means you're creating a lot of extra work for yourself by matching JavaScript this closely because you're recreating hacks used to address limits in JavaScript that don't exist in Perl. 这也意味着您将通过紧密匹配JavaScript来为自己创建很多额外的工作,因为您正在重新创建用于解决JavaScript限制(在Perl中不存在)的黑客。 It should be far simpler to use 32-bit unsigned values, << truncated using & 0xFFFFFFFF , and >> truncated using & 0xFFFFFFFF . 它应该是简单得多,使用32位无符号值, <<用截断& 0xFFFFFFFF ,并且>>截断使用& 0xFFFFFFFF

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 类型转换perl,javascript,JSON.parse JSON :: XS - 需要创意 - type conversion perl, javascript, JSON.parse JSON::XS - need ideas JavaScript中的32位有符号整数数学 - 32-bit signed integer math in JavaScript GraphQL 大整数错误:Int 不能表示非 32 位有符号整数值 - GraphQL large integer error: Int cannot represent non 32-bit signed integer value 在JavaScript中使用64位结果的32位带符号乘法 - 32-bit signed multiplication with a 64-bit result in JavaScript 确定从字符串到32位整数的转换是否会溢出 - Determine if conversion from string to 32-bit integer will overflow 32位无符号整数的按位运算? - Bitwise operations on 32-bit unsigned ints? JavaScript 左移 (&lt;&lt;) 运算符的转换规范在哪里 - Where is the specification for conversion in JavaScript's left shift (<<) operator 在使用JavaScript进行移位之前,逻辑上的右移是否总是将源代码裁剪为32位? - Does a logical right shift always clip the source to 32-bit before shifting it in JavaScript? 如何在 JavaScript 中做一个 8 位、16 位和 32 位线性反馈移位寄存器 PRNG? - How to do an 8-bit, 16-bit, and 32-bit Linear-Feedback Shift Register PRNG in JavaScript? 我如何使用 32 位有符号整数的补码来表示 javascript 中的整数 - How do I use 32-bit signed integer's complement to represent an integer in javascript
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM