Converting string of 1s and 0s into binary value
#include <stdio.h>
#include <stdlib.h>
int main(void) {
char * ptr;
long parsed = strtol("11110111", & ptr, 2);
printf("%lX\n", parsed);
return EXIT_SUCCESS;
}
For larger numbers, there as a long long
version, strtoll
.
Convert string of 0 and 1 to it's binary equivalent python
Try this:
int(s, base=2)
for example,
for i in ['10101', '11100', '11010', '00101']:
print(int(i, base=2))
@metatoaster also mentioned this.
Is there a way of converting a string of 1's and 0's to its binary counterpart, i.e. not ASCII
Note on terminology: a binary literal is a piece of text, normally appearing in source code, so I've shown code that results in the same text, in a string object. A binary literal is not a number, but it evaluates to a number in Python code, so I've also shown code to calculate that number. 0b01101101
and 109
are different literals (one binary, one decimal), but when evaluated they give exactly the same result, an integer equal to one hundred and nine.
If you want to write the integer 109 as a single byte over a serial connection, then what you want to write is the single character with code 109, not a binary literal.
my_string = "01101101"
my_binary_literal = "0b" + my_string
my_string_converted_to_integer = int(my_string, 2)
my_single_character = chr(my_string_converted_to_integer)
assert my_single_character == 'm'
Need to convert string of 1s and 0s as a binary representation into a decimal number
Because your string:
"11000000101010001100100000100000"
contains only ASCII characters, each character is one byte long. That allows you to use a bitstring comprehension to extract 8 characters (= 8 bytes) at a time from your string:
defmodule A do
def split(str) do
for <<chunk::binary-size(8) <- str>> do
String.to_integer(chunk, 2)
end
end
end
In iex:
iex(13)> c "a.ex"
warning: redefining module A (current version defined in memory)
a.ex:1
[A]
iex(14)> A.split "11000000101010001100100000100000"
[192, 168, 200, 32]
have been able to convert the octets to a concatenated binary representation
You should post what you started with and then we can show you how to dispense with all the unnecessary stuff you probably did.
Converting string of 1s and 0s into binary value, then compress afterwards ,PHP
Take a look at the bindec() function.
Basically you'll want something like (dry-coded, please test it yourself before blindly trusting it)
function binaryStringToBytes($binaryString) {
$output = '';
for($i = 0; $i < strlen($binaryString); $i += 8) {
$output .= chr(bindec(substr($binaryString, $i, 8)));
}
return $output;
}
to turn a string of the format you specified into a byte string, after which you can gzcompress()
it at will.
The complementary function is something like
function bytesToBinaryString($byteString) {
$out = '';
for($i = 0; $i < strlen($byteString); $i++) {
$out .= str_pad(decbin(ord($byteString[$i])), 8, '0', STR_PAD_LEFT);
}
return $out;
}
Converting a string of '1's and '0's whose length is exactly a multiple of 8 to a certain number of bytes
You could iterate backwards through the string, but reversing it like you suggest might be easier. From there, you can just build the bytes one at a time. A nested for loop would work nicely:
unsigned char bytes[8]; // Make sure this is zeroed
for (int i=0, j=0; i<str.length(); j++) {
for (int k=0; k<8; k++, i++) {
bytes[j] >>= 1;
if (str[i] == '1') bytes[j] |= 0x80;
}
}
i
is the current string index, j
is the current byte array index, and k
counts how many bits we've set in the current byte. We set the bit if the current character is 1, otherwise we leave it unset. It's important that the byte array is unsigned since we're using a right-shift.
Convert Binary file to strings of 0s and 1s
You can use this for a list of strings:
>>> [f"{n:08b}" for n in open("random.dat", "rb").read()]
['01000001', '01100010', '01000010', '10011011', '01100111', ...
or if you want a single string:
>>> "".join(f"{n:08b}" for n in open("random.dat", "rb").read())
'010000010110001001000010100110110110011100010110101101010111011...
The :08b
format specifier will format each byte as binary, having exactly 8 digits.
Convert string to binary zeros and ones
Here is an example:
foreach (char c in "Test")
Console.WriteLine(Convert.ToString(c, 2).PadLeft(8, '0'));
Related Topics
Non-Static Const Member, Can't Use Default Assignment Operator
Is Std::Pair<Int, Std::String> Ordering Well-Defined
Check If Member Exists Using Enable_If
Near Constant Time Rotate That Does Not Violate the Standards
What Is Half Open Range and Off the End Value
Standard Library Containers with Additional Optional Template Parameters
How Non-Member Functions Improve Encapsulation
Child Process Receives Parent's Sigint
How to Guarantee Order of Argument Evaluation When Calling a Function Object
C++ For-Loop - Size_Type VS. Size_T
Trouble with Inheritance of Operator= in C++
Forward Declaration with Unique_Ptr
Accessing Environment Variables in C++
Why Does This Delay-Loop Start to Run Faster After Several Iterations with No Sleep