/    Sign up×
Community /Pin to ProfileBookmark

How do you insert 0xFF into a string

How do you create a string that contains a single byte 0xFF? This is easy in other languages but appears impossible in JavaScript.

to post a comment
JavaScript

5 Comments(s)

Copy linkTweet thisAlerts:
@rootMar 22.2019 — Why? I fail to see why it would benefit JavaScript.

There are languages that use that as a null or del...

What are you attempting to do in what string and where is the string being implemented?

its use determines what type of character needs generating.

Copy linkTweet thisAlerts:
@charlieskidsauthorMar 24.2019 — I just need to know whether it is possible or not. My JavaScript application needs to interface to an existing system via a socket that requires messages to be prefixed with a single byte 0xFF (which indicates new message format).
Copy linkTweet thisAlerts:
@rootMar 25.2019 — JavaScript has not got that capability, it is browser based, if you want to have that capability, you need Java, a totally different language to JavaScript.
Copy linkTweet thisAlerts:
@SempervivumMar 25.2019 — Hi charlieskids, you might try this:
var str = String.fromCharCode(255) + "hello world";
for (var i = 0; i < str.length; i++) {
console.log(str.charCodeAt(i))
}
Copy linkTweet thisAlerts:
@charlieskidsauthorMar 25.2019 — Thanks for all the responses. I have now found that it is possible using the 'binary' parameter on the socket write:

let d = 'xFF0014SOR999999';


console.log("*****VAL d: " + d); // ÿ0014SOR999999

console.log("*
**
**LEN d: " + d.length); // 14

console.log("**
***just d[0]: " + d[0]); // ÿ

var client = new net.Socket();

client.connect(800, '10.89.109.27', function() {

console.log('Connected');


client.write('xFF0014SOR999999','binary'); // Using 'binary' prevents byte conversion
client.destroy(); // kill client after server's response
// careful for large transmissions of chunks of data
});
×

Success!

Help @charlieskids spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 3.28,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @darkwebsites540,
tipped: article
amount: 10 SATS,

tipper: @Samric24,
tipped: article
amount: 1000 SATS,

tipper: Anonymous,
tipped: article
amount: 10 SATS,
)...