CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 12 of 12
  1. #1
    Join Date
    Aug 2003
    Posts
    90

    Lightbulb how can i solve this ?

    Code:
    	int x = 129;
    	char m = (char)x & 255;
    	cout<<m<<endl;
    now let us say, given m , how can i retrieve x ? how can i do the opposite thing ?

    i tried this way,...
    Code:
    int y;
        if(m>127)  y = 256 + (int)m;
    	else
    	 y = (int)m;
    
    	cout<<y<<endl;
    ....but its not working.can u suggest some way ?

  2. #2
    Join Date
    Jun 2003
    Location
    Armenia, Yerevan
    Posts
    720
    This is not possible, you lost 3 bytes from x, how are you going to recover them only by the first byte?

  3. #3
    Join Date
    Mar 2004
    Posts
    137
    hi,

    Sorry boss, but it is not possible. It is possible if u have value of x less than 255.

    Ashish

  4. #4
    Join Date
    Jun 2001
    Location
    Switzerland
    Posts
    4,443
    On the other hand, if x < 255 then x & 255 == x. IOW, you can spare the effort of ANDing it...what exactly do you want to do?
    Gabriel, CodeGuru moderator

    Forever trusting who we are
    And nothing else matters
    - Metallica

    Learn about the advantages of std::vector.

  5. #5
    Join Date
    Aug 2003
    Posts
    90
    It is possible if u have value of x less than 255.
    ------
    it seems you can retrieve the value if i give you the value of m . how ? of course all my values are <255


    i am trying to convert image data (.pgm) from P2 to P5 format...my image data contains only decimal valus. all of them are less than 256.

    hi guys , is my question clear ?

    ok, let me put my question simple way,

    i am converting a data this way....

    Code:
                    int x = 129;
    	char m = (char)x & 255;     
    	cout<<m<<endl;                 // this is in P5 format .
    
    // just run it and  you will see this value in a ASCII chart.

    now if i give you m how can you do the reverse ? // i.e P5 to P2 format .

    i want to get a file which will be in P5 fromat. then again i want to get back into P2 format. thats why i am doing this.but unfortunately i could not do the reverse thing.


    i could not retrieve the value. something i have to do .....can you give me x if i give you m ( i.e x will be 129).

    what is the reverse thing of AND operation ?

  6. #6
    Join Date
    Mar 2004
    Location
    Israel
    Posts
    638
    I dont think there is a reverse for AND since
    in bits level:

    a & b = c

    1 & 0 = 0
    0 & 0 = 0

    giving c=0,b=0 you can't know what was 'a' value...

    On the other hand, if x < 255 then x & 255 == x. IOW, you can spare the effort of ANDing it...what exactly do you want to do?
    right. if you use 'AND 255' just for a safe cast from integer<255
    into a char, I think its safe to use it without bitwise operator:
    int x = ... // as long as 0 >= x <= 255
    unsigned char c = x;

    the opposite is safe too:
    x = c;
    Last edited by Guysl; April 6th, 2004 at 06:33 AM.
    **** **** **** **** **/**

  7. #7
    Join Date
    Apr 2000
    Posts
    118
    If you have an integer which is four bytes long, and you do

    x & 255

    You are effectively stripping off the last three bytes and hence losing that data. And yes, that means that there is no way of doing the reverse (getting it back).

    I'm sure you have misunderstood the P2/P5 formats; the P5 format must contain the same data as its predecessor format. It's like saving a MS Word document in word97 format, then converting it to wordXP format, re-opening it and seing that it contains three times less pages as what you saved.

  8. #8
    Join Date
    Aug 2003
    Posts
    90
    I'm sure you have misunderstood the P2/P5 formats;
    ..........you may be right. i surfed net to find the algorithm how to convert a P2 image into P5 format.

    i did not get any good resource.

    can you tell me the algorithm how it is done ?

    my aim is to convert my P2 data into P5 format...and agin P5 to P2 ..... i want to know the algorithm.

    i have tried a lot but nowhere i got the algorithm.


    can anybody give me algorithm how it is done ?

  9. #9
    Join Date
    Apr 2000
    Posts
    118
    A quick search on Google brings up
    http://netpbm.sourceforge.net/doc/pgm.html

    According to that, P2 is a text version, whereas P5 is a raw binary data. If you want to perform the conversion from P5->P2, you first read the maxval from the header to determine the size of the each pixel (which will be either 1/2 bytes). If each are one byte, then it is simple;

    char pixel = read_a_byte();

    If it is two bytes you need to piece them together (least significant first). When you have the final number output that into the P2 file as a ascii number, not a binary character....

    HTH, prolly not 100% correct...

  10. #10
    Join Date
    Aug 2003
    Posts
    90
    the link that you gave does not explain the thing clearly . i am not interested about headers . i need only the data for processing.

    ok, AND operator was creating some problem in the earlier case.

    so, i am doing other way...

    Code:
    #include<iostream>
    using namespace std;
    
    int main()
    {
    
        int x = 129;
    	char m = (char)x ;     
    	cout<<m<<endl;    // printing a raw weired character            
    
    
    /*****trying to retrieve *******/
    
    	int d =  m;
    	cout<<d<<endl; // is there no way to get that number ?
    
                                               // it is printing -127 !! 
    }
    i have not usued AND operator....if i can do it, i can get a raw file.


    this method, sould give me back the number as the character fits into four byte integer.


    i think here i have to use some trick. can you help ?

  11. #11
    Join Date
    Jun 2003
    Location
    Gjøvik, Norway
    Posts
    204
    Use an unsigned char. The value 129 overflows the signed char variable.

  12. #12
    Join Date
    Apr 2000
    Posts
    118
    Use
    unsigned char m = (unsigned char)x;
    for 0-255 range...

    The data format in the P5 data files is dictacted by the headers. The P5 data is just a sequence of bytes; sequences of either 2 or 1 bytes. The actual amount of bytes each pixel takes up is stated in one of the headers; if you do not read the bytes in the correct chunks you will be reading the wrong data.

    so
    Code:
    char byte1, byte2;
    //..
    //Get the colour of the pixel if in 2 byte format
    int pixel = byte1 + (byte2 << 8);
    //Or if it is in 1 byte format
    int pixel = byte1; 
    //...
    Last edited by NeilH; April 6th, 2004 at 11:22 AM.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured