I have a file reg.txt
which has 32 bit binary values like below
00000000000000000000000000001000
00000000000000000000010100100000
00000000000000000010000000000000
00000000000000000000000000000000
00000001000000000001000010010000
00000000010001000010000100100100
00000000001001000100010000000000

is there a way to read the 32bit binary value and store in an int?

## All 6 Replies

Here is some C++ (CLI/.NET) code to convert those to In32 values:

``````// BinaryStringFile.cpp : main project file.

#include "stdafx.h"

using namespace System;
using namespace System::Collections::Generic;
using namespace System::IO;

Int32 BinStringToInt32(String^ strBinary)
{
Int32 intRetVal = 0;
Int32 intStart = 1;

for(int intLoop=(strBinary->Length-1); intLoop >=0  ; intLoop--)
{
intRetVal += ((strBinary[intLoop]-'0') * intStart);
intStart *= 2;
}

return intRetVal;
}

int main(array<System::String ^> ^args)
{
List<Int32>^ lstInt32 = gcnew List<Int32>();
String^ strData = "";

while(!fileIn->EndOfStream)
{
}

fileIn->Close();

//Dump
for each(Int32 int32 in lstInt32)
{
Console::WriteLine(int32);
}

return 0;
}``````

If you can read them in OK, then you can just do the math on them to get the values. Do you understand how different number bases work?

Each of those 32 bit strings is just representative of a power of two

i.e. 2^x where x is the place of the bit from 0 at the right most LSB to 31 at the left most MSB. For each bit, right to left, add that power of two if the bit is HI (1).

For example, say you've got the binary string 00000000000000000000010100111001

The right-most bit represents place 0 and the left-most bit represents place 31. HI bits are in positions 0, 3, 4, 5, 8, and 10.

So you take the sum of 2^0 + 2^3 + 2^4 + 2^5 +2^8 + 2^10 = 1 + 8 + 16 + 32 + 256 + 1024 = 1337

commented: Very good information involving binary logic in programming +1

Some very good basic helpful input on pertaining binary logic in programming. Binary logic is always funny even when designed simple logic gate circuits. Also to answer the OP, simply read the integers into an array of integers however in this case as donald stated binary logic is read from right to left, not left to right like many things. So instead of iterating through a loop starting at the array element 0 you start at 32 if the case is you know that each line will always be 32 bits, to me in this case perhaps declaring some sort of struct so that you can have multiple objects containing each 32 bit array you would do something like this, Id type you up a sample code but I am multitasking many things right now

``````#include <iostream>
#include <fstream>
#include <cstdlib>

int main()
{
std::ifstream file("reg.txt");
char text[80];
while ( file.getline(text, sizeof text) )
{
unsigned long value = strtoul(text, NULL, 2);
std::cout << "text[] = \"" << text << "\", value = " << value << "\n";
}
return 0;
}

/* my output
text[] = "00000000000000000000000000001000", value = 8
text[] = "00000000000000000000010100100000", value = 1312
text[] = "00000000000000000010000000000000", value = 8192
text[] = "00000000000000000000000000000000", value = 0
text[] = "00000001000000000001000010010000", value = 16781456
text[] = "00000000010001000010000100100100", value = 4464932
text[] = "00000000001001000100010000000000", value = 2376704
*/``````

Right, my post may not have been crystal clear on what the position numbers I was talking about represent. "Place 0," being the rightmost (LSB, least significant bit) would really, in your case be array index 31 and "Place 31" would be the leftmost bit (MSB, most significant bit), or array index 0.

Are you OK with reading the values in from the file? One possibility would be to do something like:

read in full line as a string, binStr
int decimalSum = 0
for (i=binStr.length; i>0; i--)
{ if(binStr = '1') decimalSum += 2^(31-i)}

That should be the basic gist of it.

ETA: Too slow. Nice button, Dave

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, learning, and sharing knowledge.