Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 5848109
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 22, 20262026-05-22T12:48:47+00:00 2026-05-22T12:48:47+00:00

I am using node.js to build a tcp server, and I want to extract

  • 0

I am using node.js to build a tcp server, and I want to extract integers from the data received.

var net = require('net');
var server = net.createServer(function (socket) {
  socket.setEncoding('ascii');
  socket.addListener("data", function (data) {
    var pkgDataContent = data.substr(0, 2);
  });
});
server.listen(1337, "192.168.80.91");

The data received is string type, and the numbers are 1 byte, 2 bytes and 4 bytes. How to extract these 1-byte, 2-byte and 4-byte integers from a javascript string? Like the code above: pkgDataContent is a string of 2 bytes, but actually it is an integer, how to convert it to javascript number correctly?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-22T12:48:48+00:00Added an answer on May 22, 2026 at 12:48 pm

    Depends on endianness and on whether it’s signed or not.

    big endian 32-bit unsigned integer:

    pkgDataContent.charCodeAt(0) << (8*3) +
    pkgDataContent.charCodeAt(1) << (8*2) +
    pkgDataContent.charCodeAt(2) << (8*1) +
    pkgDataContent.charCodeAt(3) << (8*0)
    

    little endian 32-bit unsigned integer:

    pkgDataContent.charCodeAt(3) << (8*0) +
    pkgDataContent.charCodeAt(2) << (8*1) +
    pkgDataContent.charCodeAt(1) << (8*2) +
    pkgDataContent.charCodeAt(0) << (8*3)
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am using node.js to build a TCP server and I got the following
I'm attempting to check for the existence of a node using the following .NET
Using C# How do you remove a specific node from an XMLDocument using XPATH?
Using ASP.NET MVC there are situations (such as form submission) that may require a
I am working with Node.js to build a web socket server that uses mongodb.
We look to build a high-performance, scalable Comet server, and thought first about using
We look to build a high-performance, scalable Comet server, and thought first about using
I'm trying to build a simple web application using Node.js and CouchDb. The application
Anyone know how to get the position of a node using XPath? Say I
I'm using XPath to locate a node (or something close to it) in a

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.