Thread: Hex n' junk

  1. #1
    Registered User
    Join Date
    Jan 2005
    Posts
    183

    Unhappy Hex n' junk

    Hi

    I know there's been loads of threads about this, but I searched for like an hour straight, an' couldn't find anythin'.

    Anyway, I have a txt document saved in C: . It consists of many hex values, all strung together. I wish to simply read the file, take the first 14 characters, ( or use 14 characters to the middle of the document ) change them to ascii values, and store them to a string.

    How can this be achieved ?
    Many thanks.

  2. #2
    Registered User
    Join Date
    Aug 2005
    Posts
    1,267
    not possible if there is nothing separating the hex values, unless you know that they were stored as pairs of hex values, ffff is really ff and ff. In that case it is pretty simple. read the first 14 characters into a buffer and convert them in memory.
    Code:
    char buf[] = "fc";
    int a;
    sprintf(buf,"%x",&a);
    You might be able to do it with stringstream c++ class, but I'm not proficient enough to show you how.

  3. #3
    Registered User
    Join Date
    Jan 2005
    Posts
    183
    Never really understood what buffers are / do.
    Would it help if I say they were all separated by a full stop ?

  4. #4
    Lurking whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    9,613
    You can read in different numbers from streams and stuff after you include a header called iomanip In that header is a function called setbase that allows you to change between number systems. You can still store them in normal variables too.
    It works something like this:
    Code:
    int num = 255;
    cout <<"The number 255 in hexadecimal is " <<setbase(16) <<num <<"\n";
    cout<<"Enter a new hex number: ";
    cin>> setbase(16)>> num;

  5. #5
    Registered User
    Join Date
    Aug 2005
    Posts
    1,267
    >>would it help if I say they were all separated by a full stop
    nope -- I have no idea what a "full stop" is. They should be separated by spaces, tabe or CR/LF (Windows) or LF (*nix).

    A buffer is nothing more than a consecutive set of memory that is used for odds and ends. See the code I posted for an example. There are all sorts of uses for them.

  6. #6
    Hardware Engineer
    Join Date
    Sep 2001
    Posts
    1,398
    A buffer is simply temporary storage. In this case, it's a Character Array (C-style string).

    You might find a hex editor helpful. I use XVI32 (FREE !!!). A hex editor will allow you to "look at" (and optionally edit) the data in any file. Most hex editors will also show the ASCII characters for any value that's in the ASCII range (i.e. 32 - 126). So, you can easily see if your program is altering the file as you expect.

    ...change them to ascii values, and store them to a string.
    You don't really change the value. You display the value's associated ASCII character. If you read-in 41 hex, that's an ASCII 'A'. If you if you increment it to 42 hex, it now represents an ASCII 'B'.

    Everything in the file (and your computer's memory) is a binary numerical value. That "number" might represent an ASCII character, the numerical value, or something else. You can display that number in decimal, octal, hex, as an ASCII character, or even in binary with a little more work.

    You do need to read the data in as type char, or else you will get more than one character packed-into each integer.
    Last edited by DougDbug; 05-17-2006 at 03:19 PM.

  7. #7
    Registered User
    Join Date
    Oct 2001
    Posts
    2,934
    take the first 14 characters, ( or use 14 characters to the middle of the document ) change them to ascii values, and store them to a string.
    Try this.
    Code:
    #include <iostream>
    #include <fstream>
    #include <string>
    using namespace std;
    
    int main()
    {
       string value("0123456789ABCDEF");
       char array[14];
       
       ifstream in("file.txt");
       in.read(array, 14);
    
       string hex(array, array+14);
       string result;
       for (string::size_type i=0; i<hex.length(); i+=2)
       {
          char ascii = value.find(hex[i]) * 16;
          ascii += value.find(hex[i+1]);
          cout << (int) ascii << endl;
          result = result + ascii;
       }
       in.close();
       cout << result << endl;
       
       return 0;
    }

  8. #8
    Registered User
    Join Date
    Jan 2005
    Posts
    183
    Also, is there a simple way to invert strings ?

    Code:
    #include <iostream>
    #include <string>
    
    using namespace std;
    
    int main()
    {
            string invertstring = "123456789"
    
            invertstring.invert();
    
    }
    Thanks for all your help.

  9. #9
    Lurking whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    9,613
    Code:
    // Reversing a string
    #include <algorithm>
    #include <iostream>
    #include <string>
    
    int main(void) {
       std::string palindrome = "You can't reverse racecar!";
       reverse(palindrome.begin(), palindrome.end());
       std::cout<< palindrome <<std::endl;
       return 0;
    }
    By far the easiest way is to use the reverse algorithm on the string. It's great.

    There are previous threads on this, a simple search of the boards would have answered your question.

  10. #10
    Been here, done that.
    Join Date
    May 2003
    Posts
    1,164
    Quote Originally Posted by Necrofear
    Never really understood what buffers are / do.
    Would it help if I say they were all separated by a full stop ?
    No. But what would help is posting a small portion of the file so we can actually see what you are talking about. And maybe an example of what you want based on what you post.

    A picture is worth a thousand poorly explained questions and alleviates two thousand suggestions based on misinterpretation.
    Definition: Politics -- Latin, from
    poly meaning many and
    tics meaning blood sucking parasites
    -- Tom Smothers

  11. #11
    Registered User
    Join Date
    Jan 2005
    Posts
    183
    Oh, right yeah. Forgot to search first.
    But problems solved. Cheers people !

  12. #12
    Registered User
    Join Date
    Jan 2005
    Posts
    183
    Oh, one more question :

    What if I want to store the hex equivalent ( again I think that's the correct terminology, ) of a string inputted by the user into a completely different string.

    Code:
    string string;
    
    getline ( cin, string );
    
    // Compute the hex equivalent of string.
    
    // Store it in another string.
    If any methods require any other headers than the ones already needed, please tell me what the headers are.

  13. #13
    Registered User
    Join Date
    Oct 2001
    Posts
    2,934
    If you just want to print it, you can do:
    Code:
    for (string::size_type i=0; i<my_string.length(); i++)
       cout << hex << static_cast<unsigned int> (my_string[i]) << endl;
    If you want to store it in another string then one idea is:
    Code:
    #include <sstream>
    .
    .
       string my_string;
    
       getline ( cin, my_string );
    
       ostringstream ss;
       for (string::size_type i=0; i<my_string.length(); i++)
       {
          ss << hex << uppercase << static_cast<unsigned int> (my_string[i]);
       }
       string hex_out = ss.str();
       cout << hex_out << endl;

  14. #14
    Registered User
    Join Date
    Jan 2005
    Posts
    183
    Oh, but is there a way to do it in lower case. I just tried to switch the uppercase to lowercase, but it said that lowercase was undeclared. Why's this ?

    You people are amazing.
    Cheers for ALL your help
    Last edited by Necrofear; 05-19-2006 at 09:24 AM.

  15. #15
    Registered User
    Join Date
    Oct 2005
    Posts
    271
    The function for changing uppercase characters to lowercase characters is called tolower:
    Code:
    int main()
    {
    	locale loc;
    	string mystr("THIS IS ALL IN UPPERCASE");
    	cout << mystr.c_str() << endl;
    	for(string::size_type i = 0; i < mystr.length(); i++)
    		mystr[i] = tolower(mystr[i], loc);
    	cout << mystr.c_str() << endl;

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Ascii to hex and hex to Ascii
    By beon in forum C Programming
    Replies: 1
    Last Post: 12-26-2006, 06:37 AM
  2. Hex Editing help and information please...
    By SG57 in forum C Programming
    Replies: 9
    Last Post: 06-25-2006, 12:30 AM
  3. Replies: 11
    Last Post: 03-24-2006, 11:26 AM
  4. Replies: 3
    Last Post: 01-23-2006, 07:25 PM
  5. Is binary HEX?
    By Budgiekarl in forum Tech Board
    Replies: 11
    Last Post: 11-23-2003, 09:02 AM