How to use the binary.Binary function in binary

To help you get started, we’ve selected a few binary examples, based on popular ways it is used in public projects.

Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.

github kriskowal / narwhal-lib / tests / commonjs / bytearray-encodings-tests.js View on Github external
var assert = require("test/assert");

var Binary = require("binary").Binary,
    ByteString = require("binary").ByteString,
    ByteArray = require("binary").ByteArray;

exports.testByteArrayConstructorEncodings = function() {
    // ByteString(string, charset)
    // Convert a string. The ByteString will contain string encoded with charset.
    var testString = "hello world";
    var b = new ByteArray(testString, "US-ASCII");
    assert.isEqual(testString.length, b.length);
    b.length = 678;
    assert.isEqual(678, b.length);
    assert.isEqual(testString.charCodeAt(0), b.get(0));
    assert.isEqual(testString.charCodeAt(testString.length-1), b.get(testString.length-1));
    assert.isEqual(0, b.get(677));
};
github kriskowal / narwhal-lib / tests / commonjs / bytestring-tests.js View on Github external
var assert = require("test/assert");

var Binary = require("binary").Binary,
    ByteString = require("binary").ByteString,
    ByteArray = require("binary").ByteArray;

exports.testByteStringConstructor = function() {
    var testArray = [1,2,3,4];
    
    // ByteString()
    // Construct an empty byte string.
    var b1 = new ByteString();
    //assert.isTrue(b1 instanceof Binary, "not instanceof Binary");
    assert.isTrue(b1 instanceof ByteString, "not instanceof ByteString");
    assert.isEqual(0, b1.length);
    b1.length = 123;
    assert.isEqual(0, b1.length);
    
    // ByteString(byteString)
github kriskowal / narwhal-lib / tests / commonjs / bytestring-encodings-tests.js View on Github external
var assert = require("test/assert");

var Binary = require("binary").Binary,
    ByteString = require("binary").ByteString,
    ByteArray = require("binary").ByteArray;

exports.testByteStringConstructorEncodings = function() {
    // ByteString(string, charset)
    // Convert a string. The ByteString will contain string encoded with charset.
    var testString = "hello world";
    var b4 = new ByteString(testString, "ASCII");
    assert.isEqual(testString.length, b4.length, "6543");
    b4.length = 123;
    assert.isEqual(testString.length, b4.length, "asdf");
    assert.isEqual(testString.charCodeAt(0), b4.get(0), "234");
    assert.isEqual(testString.charCodeAt(testString.length-1), b4.get(testString.length-1), "zxcv");
};

exports.testToByteStringConstructorUnicode = function() {
github kriskowal / narwhal-lib / tests / commonjs / bytearray-tests.js View on Github external
var assert = require("test/assert");

var Binary = require("binary").Binary,
    ByteString = require("binary").ByteString,
    ByteArray = require("binary").ByteArray;

exports.testByteArrayConstructor = function() {
    var testArray = [1,2,3,4],
        b;
    
    // ByteArray()
    // New, empty ByteArray.
    b = new ByteArray();
    //assert.isTrue(b instanceof Binary, "not instanceof Binary");
    assert.isTrue(b instanceof ByteArray, "not instanceof ByteArray");
    assert.isEqual(0, b.length);
    b.length = 123;
    assert.isEqual(123, b.length);
    assert.isEqual(0, b.get(4));
github deanlandolt / commons / lib / commons / binary / standard / binary-b.js View on Github external
// normalize to node encodings
var encodings = {
    "utf8": "utf-8",
    "utf16": "utf-16"
};

var normalizeEncoding = exports.normalizeEncoding = function(encoding) {
    encoding = encoding || "utf-8";
    if (typeof encoding !== "string") return encoding;
    encoding = encoding.toLowerCase();
    return encodings[encoding] || encoding;
};


var binaryModule = require("binary");
var Binary = exports.Binary = binaryModule.Binary;
var ByteArray = binaryModule.ByteArray;
var ByteString = binaryModule.ByteString;


exports.shim = function () {
    
    // Binary
    
    if (!String.prototype.getBytes) {
        String.prototype.getBytes = function(encoding) {
            return NativeBinary(this.toString(), normalizeEncoding(encoding));
        };
    }
    
    // TODO Array.prototype.getBytes
github deanlandolt / commons / lib / commons / binary / standard / binary-b.js View on Github external
exports.isBinary = function(target) {
    return target instanceof binaryModule.Binary;
};
github olegp / common-utils / strings.js View on Github external
var binary = require("binary");
var Binary = binary.Binary, ByteString = binary.ByteString;

/**
 * Returns true if string starts with the given substring
 * 
 * @param {String}
 *          string the string to search in
 * @param {String}
 *          substring pattern to search for
 * @returns {Boolean} true in case it matches the beginning of the string, false
 *          otherwise
 */
var startsWith = exports.startsWith = function(string, substring) {
	return string.indexOf(substring) == 0;
};

/**
github ringo / ringojs / modules / net.js View on Github external
function toDatagramPacket(arg) {
        if (arg instanceof binary.Binary) {
            return new net.DatagramPacket(arg, arg.length);
        } else if (typeof arg === "string") {
            return new net.DatagramPacket(arg.toByteString(), arg.length);
        } else {
            throw new Error("Unsupported argument to send: " + arg);
        }
    }
}