How to reverse an std::string? [duplicate]

I’m not sure what you mean by a string that contains binary numbers. But for reversing a string (or any STL-compatible container), you can use std::reverse(). std::reverse() operates in place, so you may want to make a copy of the string first: #include <algorithm> #include <iostream> #include <string> int main() { std::string foo(“foo”); std::string copy(foo); … Read more

Getting binary content in node.js with http.request

The accepted answer did not work for me (i.e., setting encoding to binary), even the user who asked the question mentioned it did not work. Here’s what worked for me, taken from: http://chad.pantherdev.com/node-js-binary-http-streams/ http.get(url.parse(‘http://myserver.com:9999/package’), function(res) { var data = []; res.on(‘data’, function(chunk) { data.push(chunk); }).on(‘end’, function() { //at this point data is an array of … Read more

Generate all binary strings of length n with k bits set

This method will generate all integers with exactly N ‘1’ bits. From https://graphics.stanford.edu/~seander/bithacks.html#NextBitPermutation Compute the lexicographically next bit permutation Suppose we have a pattern of N bits set to 1 in an integer and we want the next permutation of N 1 bits in a lexicographical sense. For example, if N is 3 and the … Read more

Convert string to binary then back again using PHP

You want to use pack and base_convert. // Convert a string into binary // Should output: 0101001101110100011000010110001101101011 $value = unpack(‘H*’, “Stack”); echo base_convert($value[1], 16, 2); // Convert binary into a string // Should output: Stack echo pack(‘H*’, base_convert(‘0101001101110100011000010110001101101011’, 2, 16));

Binary buffer in Python

You are probably looking for io.BytesIO class. It works exactly like StringIO except that it supports binary data: from io import BytesIO bio = BytesIO(b”some initial binary data: \x00\x01″) StringIO will throw TypeError: from io import StringIO sio = StringIO(b”some initial binary data: \x00\x01″)

How to convert text to binary code in JavaScript?

What you should do is convert every char using charCodeAt function to get the Ascii Code in decimal. Then you can convert it to Binary value using toString(2): function convert() { var output = document.getElementById(“ti2”); var input = document.getElementById(“ti1”).value; output.value = “”; for (var i = 0; i < input.length; i++) { output.value += input[i].charCodeAt(0).toString(2) … Read more

Override git’s choice of binary file to text

The way files are actually stored inside the Git repository is not relevant to how they are treated when displayed. So the git diff program, when asked to compare two files, first obtains both complete files from the repository and then runs a difference algorithm against them. Normally, git diff looks for non-printable characters in … Read more