ctypes typecasting in perl

Abigail abigail at abigail.be
Mon Sep 9 17:08:54 BST 2013


On Mon, Sep 09, 2013 at 10:55:06PM +0700, Shantanu Bhadoria wrote:
> I have been playing around with talking to some IMU chips i.e gyroscopes,
> magnetometers and accelerometers from my raspberryPi and I am facing a
> slight problem which might seem trivial to those familiar more with C or XS
> code than I. I am trying to read registers off a chip which store data in
> int16_t format but since the data is in simple binary code, essentially
> when I read the register I get a simple unsigned int value from it. Is
> there a function or module in perl which would allow me to convert typecast
> something as int16_t?
> 
> esentially in python the following gives me int 64256 typecast into c_int16
> and returns -1280.
> from ctypes import *
> print c_int16(64256).value
> 
> Is there a equivalent function or module in perl that would do it? My
> thanks to whoever can help me solve this problem.


I think you want pack/unpack:

  $ perl -E 'say unpack s => pack S => 64256'
  -1280
  $


Abigail


More information about the london.pm mailing list