Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

The implementation of Python Struct to Javascript

License

NotificationsYou must be signed in to change notification settings

TeskeVirtualSystem/struct.js

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

11 Commits
 
 
 
 
 
 

Repository files navigation

     _                   _      _      ___| |_ _ __ _   _  ___| |_   (_)___ / __| __| '__| | | |/ __| __|  | / __|\__ \ |_| |  | |_| | (__| |_ _ | \__ \|___/\__|_|   \__,_|\___|\__(_)/ |___/                             |__/

This is an implementation of Python Struct to Javascript.

The idea is to make an easy interface like python struct in javascript to parse strings as C Types.

What is done

  • Full unpack support. I implemented all types unpack from python struct
  • Big Endian and Little endian Support. You can choose the endianess like you do in python struct.
  • Make use of Typed Arrays, Array Buffer and DataView from HTML5 Spec

TODO

  • Packing functions.

How to use it

Inpython, you use something like that for an int1234:

importstructdata='\xd2\x04\x00\x00'struct.unpack("I",data)#   This will return (1234,)

So instruct.js you will do basicly the same:

vardata='\xd2\x04\x00\x00';struct.unpack("I",data);//   This will return [1234]

It works also for multiple packed data, inpython:

importstructdata='\xe0#\x00\x00\x00\x00(Aa'struct.unpack("Ifc",data)#   This will return (9184, 10.5, 'a')

Instruct.js:

vardata='\xe0#\x00\x00\x00\x00(Aa';struct.unpack("Ifc",data);//  This will return [9184, 10.5, "a"]

The function syntax:

struct.unpack(fmt,string)

Arguments:fmt a string containing the types and endianess:

First Character is endianess (Optional)

  • @ Little Endian
  • = Little Endian
  • < Little Endian
  • > Big Endian
  • ! Big Endian

First and/or other characters as the format:

  • Format - C Type - Size - Description
  • xPad Byte - 1 - This just skips one byte at the data
  • cchar -1 - String of Length 1
  • bsigned char - 1 - Integer
  • Bunsigned char - 1 - Integer
  • ?boolean - 1 - Boolean
  • hshort int - 2 - Int
  • Hunsigned short - 2 - Integer
  • iint - 4 - Integer
  • Iunsigned int - 4 - Integer
  • llonginteger - 4- Integer
  • Lunsigned long - 4 - Integer
  • qlong long - 8 - Integer
  • Qunsigned long long- 8 - Integer
  • ffloat - 4 - Float
  • ddouble - 8 - Double
  • schar[] - ? - String
  • pchar[] - ? - String
  • Pvoid * - 4 - Integer

Returns : array with the elements

About

The implementation of Python Struct to Javascript

Topics

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

[8]ページ先頭

©2009-2025 Movatter.jp