Thread: unsigned char .. atol ?

  1. #1
    Registered User C of Green's Avatar
    Join Date
    Jan 2002
    Location
    Calgary/Canada
    Posts
    59

    unsigned char .. atol ?

    hi .. i was just wonder if there is a function similar to atol, however if will convert a unsigned char to a long ?

  2. #2
    zeckensack
    Guest
    Something like

    long boom;
    unsigned char woot=5;

    boom=woot;

    ???

  3. #3
    Registered User C of Green's Avatar
    Join Date
    Jan 2002
    Location
    Calgary/Canada
    Posts
    59
    well ... im just tring to convert a unsigned char into a long int after some bit manip ...


    void Test::testFunction(ifstream &inFile)
    {
    long data;

    unsigned char buffer[4];

    inFile.seekg(6240, ios::beg);
    inFile.read(reinterpret_cast<char *>(&buffer[0]), 4);

    (buffer[0]<<8)|buffer[3];
    (buffer[1]<<8)|buffer[2];
    (buffer[2]<<8)|buffer[1];
    (buffer[3]<<8)|buffer[0];

    data = atol(buffer); // problem here
    }

    thanks for any help guys

  4. #4
    Registered User Liam Battle's Avatar
    Join Date
    Jan 2002
    Posts
    114
    Well you can achieve this by bitwise operators... or a by a strict copying. Remember that a char is 8bits and a long int a 32-bits...
    LB0: * Life once school is done
    LB1: N <- WakeUp;
    LB2: N <- C++_Code;
    LB3: N >= Tired : N <- Sleep;
    LB4: JMP*-3;

  5. #5
    Registered User
    Join Date
    Oct 2001
    Posts
    2,934
    If you change data to this:
    long *data;

    Then you can do this:
    data = (reinterpret_cast<long *> (buffer)); // problem here

    Of course, to refer to the value of data, you'll have to dereference it:
    cout << *data << endl;

    Or create another variable:
    Code:
    long *pdata;
    long data;
    .
    .
    pdata = (reinterpret_cast<long *> (buffer)); // problem here 
    data = *pdata;

  6. #6
    Registered User C of Green's Avatar
    Join Date
    Jan 2002
    Location
    Calgary/Canada
    Posts
    59
    hey guys, thanks for the input

    Anways, i tried what you said above swoopy
    "data = (reinterpret_cast<long *> (buffer));"
    however that did not work. The problem is i need to be able to put the whole contents of "buffer" into one variable of long type.
    When shifting bits of an array is there a way to shift everything into one variable ?

    ie.

    void Test::testFunction(ifstream &inFile)
    {
    long data;

    unsigned char buffer[4];

    inFile.seekg(6240, ios::beg);
    inFile.read(reinterpret_cast<char *>(&buffer), 4);

    data = (buffer[0]<<8)|buffer[3],
    (buffer[1]<<8)|buffer[2],
    (buffer[2]<<8)|buffer[1],
    (buffer[3]<<8)|buffer[0];


    }

    I try this but only get the first index of the array ? Any help would be appreciated

  7. #7
    Registered User
    Join Date
    Oct 2001
    Posts
    2,934
    It should store all four bytes into a long. Run this and see what you get. It should output all f's: ffffffff.
    Code:
    long data; 
    long *pdata;
    
    unsigned char buffer[4] = {0xff,0xff,0xff,0xff}; 
    
    pdata = (reinterpret_cast<long *> (buffer));
    data = *pdata;
    cout << "data:" << data << endl;
    cout << "data:" << hex << data << endl;
    Also, be sure you open your file in binary mode.

    ifstream inFile(filename,ios::binary);

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 12
    Last Post: 08-11-2008, 11:02 PM
  2. get keyboard and mouse events
    By ratte in forum Linux Programming
    Replies: 10
    Last Post: 11-17-2007, 05:42 PM
  3. How do i un-SHA1 hash something..
    By willc0de4food in forum C Programming
    Replies: 4
    Last Post: 09-14-2005, 05:59 AM
  4. Half-life SDK, where are the constants?
    By bennyandthejets in forum Game Programming
    Replies: 29
    Last Post: 08-25-2003, 11:58 AM
  5. Strings are V important...
    By NANO in forum C++ Programming
    Replies: 15
    Last Post: 04-14-2002, 11:57 AM