Home Forums Gamescan Chat42 About
* Login   * Register * FAQ    * Search
It is currently Fri 04-19-2024 9:11AM

All times are UTC - 6 hours




Post new topic Reply to topic  [ 6 posts ] 
Author Message
 Post subject: C++ Question: Convert from char[4] to uint32
PostPosted: Wed 09-20-2006 12:24PM 
Offline
Colonel
User avatar

Joined: Wed 08-24-2005 2:44PM
Posts: 620

Source: EE Building
I'm trying to read data from binary files. I can do this easily, but it only stores data into a char*. I would like to take 4 chars, and convert them into an unsigned integer-they have the same length so it should be possible. 4chars=32bits=1 int.

Any ideas how?

_________________
It seems the white trash of the internet has arrived.
http://rsaxvc.net


Top
 Profile  
    
 Post subject:
PostPosted: Wed 09-20-2006 12:49PM 
Offline
Spanish
User avatar

Joined: Wed 08-21-2002 10:10AM
Posts: 1898
Location: A-Frames bitches!

Source: VPN
If you are trying to read 1 field in at a time.. try this
Code:
unsigned long i;
char *buffer;

... read into buffer ....

i = *(unsigned long *)buffer;


if you are trying to read multiple fields try something like this
Code:
struct fields
{
  unsigned long field1;
  unsigned long field2;
  unsigned long field3;
} __attribute__ (( packed ));

struct fields * f;
char * buffer;
.. read in buffer ...

f = (struct fields *)buffer;

f->field1 ...
f->field2 ...
f->field3 ...

_________________
KOK - 011, Pullin rank on bitches since 2005


Top
 Profile E-mail  
    
 Post subject:
PostPosted: Wed 09-20-2006 5:28PM 
Offline
Captain
User avatar

Joined: Sun 08-17-2003 8:06PM
Posts: 144
Location: 127.0.0.1

Source: Fidelity
Don't forget about the byte order, that might give you some problems.

_________________
It is impossible to make anything foolproof because fools are so ingenious.


Top
 Profile E-mail  
    
 Post subject:
PostPosted: Thu 09-21-2006 5:29AM 
Offline
Sergeant

Joined: Sun 03-09-2003 9:34PM
Posts: 47

Source: Fidelity
You sould just use read (and write) to read the binary data directly into
the desired data type. For example, to read do something like:

Code:
#include <iostream>
#include <fstream>
using namespace std;

int main(void)
{
  float test;
  ifstream file ("example.txt", ios::in|ios::binary);
  if (file.is_open())
  {
    file.read((char*) &test, sizeof(float));
    file.close();

    cout << "the float: " << test << endl;
  }
  return 0;
}


The corresponding write is:
Code:
#include <iostream>
#include <fstream>
using namespace std;


int main(void)
{
  float test=123.456;
  ofstream file ("example.txt", ios::out|ios::binary|ios::ate);
  if (file.is_open())
  {
    file.write((char *)&test, sizeof(float));
    file.close();
  }
  return 0;
}


See: http://www.cplusplus.com/doc/tutorial/files.html

If you're only working on one platform you probably don't have to worry
about byte order.

Bill


Top
 Profile E-mail  
    
 Post subject:
PostPosted: Thu 09-28-2006 7:41PM 
Offline
Sergeant

Joined: Mon 03-03-2003 8:43PM
Posts: 17

Source: Theta Xi
Back and Forth.

Here's an example I wrote to show how to convert both ways.

Code:
#include <stdlib.h>
#include <unistd.h>

int main()
{
   unsigned int number = 0;
   unsigned int * numptr;   

   unsigned char before[2];
   unsigned char *ptr;

   before[0] = 0xee;
   before[1] = 0xff;

   ptr = before;
   number = *(unsigned int*)ptr;     

   //Note the byte position change!
   printf("Char: %X%X, Int: %X \n", before[1], before[0], number);

   before[0] = 0xAA;  //Just to prove it changes
   before[0] = 0xBB;   
   number = 0x1111;
   
   numptr = &number;

   ptr = numptr;

   before[0] = (unsigned char)ptr[0];
   before[1] = (unsigned char)ptr[1];

   printf("Char: %X%X, Int: %X \n", before[0], before[1], number);

   return 0;
}



Top
 Profile  
    
 Post subject:
PostPosted: Fri 09-29-2006 8:12AM 
Offline
Colonel
User avatar

Joined: Wed 08-24-2005 2:44PM
Posts: 620

Source: TJ South
Heretic's way seemed the sneakiest and least memory intensive, that and I didn't know you could cast pointer types and not pointers.

so here's what I used:

#include <iostream>
#include <fstream>
using namespace std;
char *buffer2=new char[2]; //(unsigned short)
char *buffer4=new char[4]; //(unsigned int)
char *buffer6=new char[6]; //(unsigned bssid)
char *buffer8=new char[8]; //(unsigned long)
stream inputline("netstumbler.ns1,ios::in|ios::binary);
...blah blah blah...
inputline.read(buffer4,4);
cout<<"Number of access points: "<<*(unsigned int *)buffer4;
...blah blah blah...
inputline.close();

delete <everything>

_________________
It seems the white trash of the internet has arrived.
http://rsaxvc.net


Top
 Profile  
    
Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 6 posts ] 

All times are UTC - 6 hours


Who is online

Users browsing this forum: No registered users and 8 guests


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum

Search for:
Jump to:  
cron
Powered by phpBB © 2000, 2002, 2005, 2007 phpBB Group