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 6697133
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T06:25:26+00:00 2026-05-26T06:25:26+00:00

I need to convert a huge file into the following format: for each byte,

  • 0

I need to convert a huge file into the following format: for each byte, print that byte as character and print its decimal value in brackets, so that “@” becomes “@[64]”. I’ve got the following:

var s = fs.createReadStream(path,  { encoding: 'binary' });  
var p = [];

s.addListener('data', function(data) {     
    for(var i = 0; i < data.length; i++) {
            p.push(data.charAt(i)  + "[" + data.charCodeAt(i) + "]");
    }
});

s.addListener('end', function(data) {     
    console.log(p.join(""));
});

This kind of works, however, is totally slow. Any ideas how this can be optimized?

upd. as per the comment, added some date statements and found out that the “p.push” line takes most of the time. So I guess the problem is not in file reading. Still, the question remains – how to speed this up.

  • 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-26T06:25:26+00:00Added an answer on May 26, 2026 at 6:25 am

    If the file is utf-8, you’re going to probably want to decode it first. Also, don’t use the binary encoding, it’s basically deprecated by now.

    Here’s an example:

    var fs = require('fs')
      , StringDecoder = require('string_decoder').StringDecoder;
    
    var stream = fs.createReadStream('/path/to/my/file')
      , decode = new StringDecoder('utf8')
      , out = [];
    
    stream.on('data', function(buff) {
      buff = decode.write(buff);
      for (var i = 0, l = buff.length; i < l; i++) {
        out.push(buff[i] + ' [' + buff.charCodeAt(i) + ']');
      }
    });
    
    stream.on('end', function() {
      console.log(out.join('\n'));
    });
    

    To make it simpler, you can also use the built-in decoding feature of read streams: stream.setEncoding('utf-8'); instead of making your own StringDecoder, but it can be useful using your own in some cases, e.g. you still have direct access to the buffer.

    Edit: Sorry, didn’t see you were asking for an optimization. What you’re doing is inherently slow. I thought you only wanted it for debugging purposes. There might just be a better way of doing what you’re trying to do in the first place. However, since you’re buffering everything anyway, you might as well just do an fs.readFile and walk over the buffer.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I need to convert a value which is in a DateTime variable into a
I need to convert a Word document into HTML file(s) in Java. The function
I need to convert incoming phone number strings to a standardized format that does
We need to convert huge numbers of UUIDS into xml-compatible strings. If we use
I need to convert latitude/longitude coordinates into Easting/Northing coordinates in the Alberta 10 TM
I need to convert HTML documents into valid XML, preferably XHTML. What's the best
I need to convert an arbitrary amount of milliseconds into Days, Hours, Minutes Second.
I need to convert → (&rarr) to a symbol I can type into a
Need to convert a string to normal datetime format so it can be correctly
need to convert a pdf file to a doc file. I found different type

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.