简体   繁体   English

我正在尝试将函数转移到 node.js 项目

[英]I'm trying to transfer a function to node.js project

I'm trying to transfer a function to node.js project.我正在尝试将函数转移到 node.js 项目。 I've succeeded in creating the function itself so far.到目前为止,我已经成功地创建了函数本身。 The JavaScript code is accepting it and everything works as it should. JavaScript 代码正在接受它,并且一切正常。 I'm not exactly certain on how to pass in any parameters nor return any value.我不确定如何传入任何参数或返回任何值。 I'm using nan.h for this.我为此使用 nan.h。

The Code:编码:

#include <iostream>
#include <string>
#include <bitset>
#include <math.h>
#include <nan.h>
// using namespace v8 ;
// using namespace std ;

NAN_METHOD( encript ) {
  const float stringLn = 11 ;

  std::string data = "Hello World" ;
  int bit8Data[ sizeof( data ) / sizeof( data[0] ) ];
  for ( std::size_t i = 0; i < data.size(); ++i )
  {
    bit8Data[i] = int( long( std::bitset<8>( data.c_str()[i] ).to_ulong()));
  }

  int bit6Data[ 15 ];
  int remainder = 0, remainderLn = 0;
  int j = 0 ;
  for ( std::size_t i = 0; i < data.size(); ++i )
  {
    if ( remainderLn == 6 ) {
      bit6Data[j] = remainder ;
      j++ ;
      remainder = 0 ;
      remainderLn = 0 ;
    };

    bit6Data[j] = int( remainder * pow( 2, 8 ) + bit8Data[i] ) / int( pow( 2, remainderLn + 2 ));
    remainder   = int( remainder * pow( 2, 8 ) + bit8Data[i] ) % int( pow( 2, remainderLn + 2 ));
    remainderLn += 2 ;

    j++ ;
  }
  if ( remainderLn > 0 ) bit6Data[j] = remainder * ( pow( 2, 6 - remainderLn ));

  for ( std::size_t i = 0; i < sizeof( bit6Data ) / sizeof( bit6Data[0] ); ++i )
  {
    std::cout << i << ": " << bit6Data[i] << std::endl ;
  }
}


NAN_MODULE_INIT( encriptInit ) {
  Nan::SetMethod( target, "encript", encript );
}

NODE_MODULE( encript, encriptInit );

NAN_METHOD creates a definition with a single info parameter that is an array of Local<Value> . NAN_METHOD创建一个带有单个info参数的定义,该参数是一个Local<Value>数组。 info[0] will be the first one, info.This() will be the this and you can call info.Length() to get the count. info[0]将是第一个, info.This()将是this ,您可以调用info.Length()来获取计数。 Call info.GetReturnValue().Set(value) to return a value.调用info.GetReturnValue().Set(value)以返回一个值。

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM