Skip to content
This repository was archived by the owner on Apr 22, 2023. It is now read-only.

buffer.toString() not working correctly #8859

Closed
zetxx opened this issue Dec 11, 2014 · 3 comments
Closed

buffer.toString() not working correctly #8859

zetxx opened this issue Dec 11, 2014 · 3 comments
Assignees

Comments

@zetxx
Copy link

zetxx commented Dec 11, 2014

Hi there,
following your example on: http://nodejs.org/api/buffer.html#buffer_buf_write_string_offset_length_encoding im trying to make buffer.toString() as follows

buf = new Buffer(256); len = buf.write('\u00bd + \u00bc = \u00be', 0); buf.toString('utf8', 0, len); buf.toString(0, len); buf.toString(undefined, 1, len);
it works as expected but when i try to do following:
buf.toString(1, len);
it ends with error TypeError: Unknown encoding: 1

@Spekpannenkoek
Copy link

Buffer checks the encoding with if (!encoding) encoding = 'utf8'; (source).
That means that:

buf.toString('utf8', 0, len);    // OK, everything is specified
buf.toString(0, len);            // Encoding is false-y, thus defaults to UTF-8, 
                                 // "start" - not "end" - is equal to "len"!
buf.toString(undefined, 1, len); // OK, everything is specified, encoding defaults to UTF-8.
buf.toString(1, len);            // Encoding is set to "1". That's not a valid value. 

If anything, I'd call it a documentation error, rather than a proper bug.

@zetxx
Copy link
Author

zetxx commented Dec 11, 2014

so maybe it should be fixed in docs :)

@jasnell
Copy link
Member

jasnell commented Dec 11, 2014

While the docs could likely be a bit clearer, looking it over now the error you're getting appears to be the correct behavior. toString is working as it is supposed to.

jasnell added a commit to jasnell/node-joyent that referenced this issue Dec 16, 2014
jasnell added a commit to jasnell/node-joyent that referenced this issue Dec 16, 2014
jasnell added a commit to jasnell/node-joyent that referenced this issue Dec 16, 2014
jasnell added a commit to jasnell/node-joyent that referenced this issue Dec 17, 2014
Improve documentation for buffer.toString and buffer.copy per issues
nodejs#8859 and
nodejs#8857.
jasnell added a commit to jasnell/node-joyent that referenced this issue Dec 19, 2014
Improvements to document for buffer.toString and buffer.copy per issues
nodejs#8859 and
nodejs#8857

(rebased and on joyent/v0.10 and improved slightly)
tjfontaine pushed a commit that referenced this issue Dec 22, 2014
Better wording for start and end parameters, also document .length
should be considered readonly.

RE: #8857, #8859, #8913
PR: #8910
PR-URL: #8910

Signed-off-by: Timothy J Fontaine <[email protected]>
jasnell added a commit to jasnell/node-joyent that referenced this issue Jan 5, 2015
Better wording for start and end parameters, also document .length
should be considered readonly.

RE: nodejs#8857, nodejs#8859, nodejs#8913
PR: nodejs#8910
PR-URL: nodejs#8910

Signed-off-by: Timothy J Fontaine <[email protected]>
jasnell added a commit to jasnell/node-joyent that referenced this issue Jan 5, 2015
Better wording for start and end parameters, also document .length
should be considered readonly.

RE: nodejs#8857, nodejs#8859, nodejs#8913
PR: nodejs#8910
PR-URL: nodejs#8910

Signed-off-by: Timothy J Fontaine <[email protected]>
@trevnorris trevnorris self-assigned this Feb 11, 2015
@zetxx zetxx closed this as completed Jun 1, 2015
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

No branches or pull requests

4 participants