Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

C++ Get binary value of a buffer

Tags:

c++

binary

buffer

I'm really sorry for the wording of the question, it's a bit confusing.

So lets say I have a buffer:

char buffer[4] = {0, 0, 2, 0};

if we convert it all to binary, we get one 32 bit unsigned int:

00000000 00000000 00000010 00000000

The value of it is 512.

So my question is, how do I get to the answer of 512 using c++? Preferably with some library function.

Thanks for any answer, and I'm very sorry if this has been asked before, I couldn't find it.

like image 834
Daniel Avatar asked Apr 23 '19 16:04

Daniel


1 Answers

You can perform some bitwise operations:

unsigned int converter(const char buffer[4]) {
  return (buffer[0] << 24) |
         (buffer[1] << 16) |
         (buffer[2] << 8) |
         (buffer[3]);
}

Example Here

like image 190
BiagioF Avatar answered Oct 17 '22 05:10

BiagioF