Thread: 2 chars, one int

  1. #1
    Registered User
    Join Date
    Jul 2003
    Posts
    27

    2 chars, one int

    ok, little bit of a problem here.

    I am reading in chars, x and y. once read, i need to print them out as y, x.

    I have an int Z which i need to contain y AND x without adding them. so if x actually was 35 and y actually was 37, i would need Z to store 3735.

    once i figure this out i need to shift the int left 4 bits, but i figured i would get this chunk done first.

    any help is appreciated.

    thanks,
    keith

  2. #2
    eh ya hoser, got a beer? stumon's Avatar
    Join Date
    Feb 2003
    Posts
    323
    Instead of the 2 chars and 1 int. make 3 chars and use strcat() to put those 2 chars into the 1 char. Ex:
    Code:
    #include <iostream>
    #include <cstring>
    
    using namespace std;
    
    int main()
    {
    	char tempchar1[3];
    	char tempchar2[3];
    	char totalchars[5] = {"\0"};
    
    	cin >> tempchar1;
    	cin >> tempchar2;
    
    	strcat(totalchars, tempchar1);
    	strcat(totalchars, tempchar2);
    	strcat(totalchars, "\0");
    
    	cout << "Together: " << totalchars << endl;
    	return 0;
    }
    Last edited by stumon; 08-01-2003 at 08:36 AM.
    The keyboard is the standard device used to cause computer errors!

  3. #3
    Registered User
    Join Date
    Jul 2003
    Posts
    27
    hrm. maybe it would help if i posted code.

    int a, b;
    char xCha, xChb;
    int combine;

    fIn2.open(userfilename.c_str(), ios::in | ios::binary);
    fIn2.seekg(ios::beg);
    fIn2.seekp(1000);
    while(!fIn2.eof()) {
    fIn2.get(xCha);
    fIn2.get(xChb);
    a= (int)xCha;
    b= (int)xChb;
    combine = ???;
    fout.put(combine);
    }

    the int "combine" needs to take ints b and a and combine them. for example if "a" reads C3 and "b" reads 23, i would need "combine" to read 23C3.

    any help using the code i have?

    thanks,
    keith

  4. #4
    pronounced 'fib' FillYourBrain's Avatar
    Join Date
    Aug 2002
    Posts
    2,297
    an int is 4 bytes. so you should be able to store your bytes A and B in the low two bytes of your int.

    Z = ((int)A<<8) | (int)B;

    done.
    "You are stupid! You are stupid! Oh, and don't forget, you are STUPID!" - Dexter

  5. #5
    Registered User
    Join Date
    Jul 2003
    Posts
    27
    hrm. i tried both these methods and they don't seem to give me the results i'm looking for. i'm probably just dumb and am missing something basic, but here is what i DO have:

    i have two ints.

    i need to put these two ints together, without adding them mathematically. like
    int a = 12
    int b = 78

    and int c must = 1278, not 90.

    any ideas? is it possible?

    thanks again,
    keith

  6. #6
    pronounced 'fib' FillYourBrain's Avatar
    Join Date
    Aug 2002
    Posts
    2,297
    our methods did the same thing.

    ((int)A<<8) | (int)B

    gives the same results as

    A*256 + B

    0x12 * 256 + 0x78 is 0x1278
    "You are stupid! You are stupid! Oh, and don't forget, you are STUPID!" - Dexter

  7. #7
    Registered User KurtSurge's Avatar
    Join Date
    Aug 2003
    Posts
    25
    I wrote a simple program that does what your looking for, i hope it helps

    Code:
    #include <iostream>
    
    using namespace std;
    
    int main()  {
    
        int a = 12;
        int b = 78;
        int c = 0;
        char buffer[5];
    
        sprintf(buffer, "%d%d", a, b);
        c = atoi(buffer);
        cout << c << endl;
    
        system("pause");
    
    return 0;
    }
    It's too bad that stupidity isn't painful
    --Anton Szandor LaVey

  8. #8
    Registered User
    Join Date
    Jul 2003
    Posts
    27
    hrm. when i use this method:

    while(!fIn2.eof()) {
    fIn2.get(A);
    fIn2.get(B);
    combine = ((int)A<<8) | (int)B;
    fout.put(combine);

    it seems to be printing out every other int, rather than combining both A and B. i'm probably missing something.....also, what does | do in this program?

    keith

  9. #9
    pronounced 'fib' FillYourBrain's Avatar
    Join Date
    Aug 2002
    Posts
    2,297
    don't use put. Use write(). I think put just does one byte.

    fout.write(&combine,2);

    except you should probably understand that this writes out the low two bytes of combine and ignores the high two.
    "You are stupid! You are stupid! Oh, and don't forget, you are STUPID!" - Dexter

  10. #10
    pronounced 'fib' FillYourBrain's Avatar
    Join Date
    Aug 2002
    Posts
    2,297
    also, since intel is lil-endian, you're bytes will be reverse of what you expect.
    "You are stupid! You are stupid! Oh, and don't forget, you are STUPID!" - Dexter

  11. #11
    pronounced 'fib' FillYourBrain's Avatar
    Join Date
    Aug 2002
    Posts
    2,297
    "|" operator is the bitwise "OR" meaning if you have the following bytes

    01011000
    10000110
    the result is
    11011110

    as each bit is or'd
    "You are stupid! You are stupid! Oh, and don't forget, you are STUPID!" - Dexter

  12. #12
    Registered User
    Join Date
    Jul 2003
    Posts
    27
    oooh awesome, OR'ing was going to be a question later on!

    but i tried using write(&combine,2) and got the following error when compiling:

    'write' : cannot convert parameter 1 from 'int *' to 'const char *'
    Types pointed to are unrelated; conversion requires reinterpret_cast, C-style cast or function-style cast

    keith

  13. #13
    pronounced 'fib' FillYourBrain's Avatar
    Join Date
    Aug 2002
    Posts
    2,297
    write((char*)&combine,2)
    "You are stupid! You are stupid! Oh, and don't forget, you are STUPID!" - Dexter

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. NEED HELP READING FILE and PRINTING
    By geoffr0 in forum C Programming
    Replies: 4
    Last Post: 04-16-2009, 05:26 PM
  2. Replies: 3
    Last Post: 05-13-2007, 08:55 AM
  3. Working with random like dice
    By SebastionV3 in forum C++ Programming
    Replies: 10
    Last Post: 05-26-2006, 09:16 PM
  4. Quack! It doesn't work! >.<
    By *Michelle* in forum C++ Programming
    Replies: 8
    Last Post: 03-02-2003, 12:26 AM
  5. easy if you know how to use functions...
    By Unregistered in forum C Programming
    Replies: 7
    Last Post: 01-31-2002, 07:34 AM