Midterm 2 Question

7 views
Skip to first unread message

Robert

unread,
Nov 11, 2011, 2:01:39 PM11/11/11
to SJSUcmpe130
I have a question about the midterm. The ip addresses consisted 12
bits, we were required to stored these numbers into the Hash table.
How do we do that? I got stuck on this part. I could not convert a
string (read from a file) to an unsigned long long.

Konstantin Vyshetsky

unread,
Nov 11, 2011, 2:09:57 PM11/11/11
to rober...@gmail.com, SJSUcmpe130
We were actually storing the last 6 numbers of the ip address or half of the IP address, this would make it 16 bits.
Since I read the whole number as an 8 byte unsigned long, to get the last 6 numbers I took mod 1,000,000, and then modded the result with the size of the hash table.
To convert a number into a string, use sstream:
stringstream ss;
ss << number;
string numberString = ss.str();
To convert a string into a number:
string numberString;
istringstream(numberString) >> number;

- Kon

Robert Wang

unread,
Nov 11, 2011, 2:17:59 PM11/11/11
to Konstantin Vyshetsky, SJSUcmpe130
Thanks, I tested and it worked.  This was the code I was looking for:


    unsigned long long number;

    string numberString = "234201157163";
    istringstream(numberString) >> number;   

    cout << number << endl;
Reply all
Reply to author
Forward
0 new messages