|
-
April 6th, 2004, 01:21 AM
#1
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 ?
-
April 6th, 2004, 02:46 AM
#2
This is not possible, you lost 3 bytes from x, how are you going to recover them only by the first byte?
-
April 6th, 2004, 03:36 AM
#3
hi,
Sorry boss, but it is not possible. It is possible if u have value of x less than 255.
Ashish
-
April 6th, 2004, 04:40 AM
#4
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?
-
April 6th, 2004, 06:04 AM
#5
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 ?
-
April 6th, 2004, 06:17 AM
#6
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.
**** **** **** **** **/**
-
April 6th, 2004, 06:19 AM
#7
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.
-
April 6th, 2004, 06:31 AM
#8
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 ?
-
April 6th, 2004, 06:47 AM
#9
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...
-
April 6th, 2004, 09:23 AM
#10
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 ?
-
April 6th, 2004, 11:12 AM
#11
Use an unsigned char. The value 129 overflows the signed char variable.
-
April 6th, 2004, 11:18 AM
#12
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
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|