Thread: Populating MultiByte Datatypes With Void Pointers To Unsigned Char's?

  1. #1
    Registered User
    Join Date
    Feb 2003
    Posts
    265

    Populating MultiByte Datatypes With Void Pointers To Unsigned Char's?

    Im doing some binary datafile processing, and im reading the whole file in as a vector of unsigned bytes. If i were to populate a 4 byte object like an unsigned int one byte at a time how would i go about it?

    Let me illustrate what im trying to do:
    Code:
    std::vector<unsigned char> FileContents; 
    //Assume i populate FileContents here, and its correctly filled. 
    unsigned int test; //4 bytes, i want to set the first byte to FileContents[5] and the second to [6] etc 
    
    //obviously this is incorrect, im just fishing for the correct way to do it. 
    void * TheVoid; 
    TheVoid = &test; 
    TheVoid[0] = *FileContents[5]; 
    TheVoid[1] = *FileContents[6];
    ...
    Does this make sense? Am i missing some critical thing that will make my life easier?

    Thanks for your time.

    .

  2. #2
    Guest Sebastiani's Avatar
    Join Date
    Aug 2001
    Location
    Waterloo, Texas
    Posts
    5,708
    Code:
    int i;
    char * p = (char*)&i;
    p[0] = fc[6];
    p[1] = fc[7];
    p[2] = fc[8];
    p[3] = fc[9];
    of course a much easier way to do it would be:

    Code:
    int i = *((int*)(&fc[6]));
    just keep in mind that the file needs to originate from a system with the same endian-ness as the program working with it, otherwise anything larger than a byte will be read incorrectly.
    Code:
    #include <cmath>
    #include <complex>
    bool euler_flip(bool value)
    {
        return std::pow
        (
            std::complex<float>(std::exp(1.0)), 
            std::complex<float>(0, 1) 
            * std::complex<float>(std::atan(1.0)
            *(1 << (value + 2)))
        ).real() < 0;
    }

  3. #3
    Registered User
    Join Date
    Feb 2003
    Posts
    265
    Thanks for the fast reply SEB, as always your a great help. Im trying to read in this ugly binary datafile and the refrence i have for it says field length is one size but it turns out to be another in a hex editor =( I loathe bad datatype refrences.

    Thanks again,

    DBM

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. can someone help me with these errors please code included
    By geekrockergal in forum C Programming
    Replies: 7
    Last Post: 02-10-2009, 02:20 PM
  2. A Full Program to analyze.
    By sergioms in forum C Programming
    Replies: 2
    Last Post: 12-30-2008, 09:42 AM
  3. need help with handelling multiple source files
    By DarkMortar in forum C++ Programming
    Replies: 38
    Last Post: 05-26-2006, 10:46 PM
  4. Please STICKY this- vital to MSVC 6 dev - BASETSD.h
    By VirtualAce in forum Game Programming
    Replies: 11
    Last Post: 03-15-2005, 09:22 AM
  5. oh me oh my hash maps up the wazoo
    By DarkDays in forum C++ Programming
    Replies: 5
    Last Post: 11-30-2001, 12:54 PM