C++Talk.NET Forum Index C++Talk.NET
C++ language newsgroups
 
Archives   FAQFAQ   SearchSearch   MemberlistMemberlist   UsergroupsUsergroups   RegisterRegister 
 ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

Problem in transfering binary data to dec

 
Post new topic   Reply to topic    C++Talk.NET Forum Index -> C++ Language (Moderated)
View previous topic :: View next topic  
Author Message
jiing
Guest





PostPosted: Mon Apr 18, 2005 11:06 am    Post subject: Problem in transfering binary data to dec Reply with quote



I have some problem in transfer the unsigned char in a file to the
decimal value
The following is my code:

Code:

ifstream fin("usb1_8192k_out.dat", ios::binary | ios::in );
if (!fin.is_open() ){
cout<<"input file is not open."<<endl;
}
char ch ;
vector<int> buffer;
vector<double> result;

while( fin.get( ch ) ){
buffer.push_back( (unsigned int)ch );
}
fin.close();

for(i=1; i<=10; i++){
cout<<buffer[i]<<endl;
}


The output is: -121 117 103 -80 -93 57 120 -36
108 47

But when I see the binary file, they are
?7F 87 75 67 B0 A3 39 78 DC 6C
?g除9x釿

The real decimal value I wanted should be
127 135 117 103 176 163 57 120 220 108

7F(hex) is 127(dec)
Can anyone tell me, what mistake I made. I can not figure out it.
Thank you in advance.


[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]

Back to top
Conrad Z.
Guest





PostPosted: Wed Apr 20, 2005 6:08 am    Post subject: Re: Problem in transfering binary data to dec Reply with quote




"jiing" <jiing.deng (AT) gmail (DOT) com> schreef in bericht
news:1113819550.330232.40580 (AT) g14g2000cwa (DOT) googlegroups.com...
Quote:
I have some problem in transfer the unsigned char in a file to the
decimal value
The following is my code:

Code:

ifstream fin("usb1_8192k_out.dat", ios::binary | ios::in );
if (!fin.is_open() ){
cout<<"input file is not open."<<endl;
}
char ch ;
vector<int> buffer;
vector<double> result;

while( fin.get( ch ) ){
buffer.push_back( (unsigned int)ch );
[/quote:731e22f9b2]
This cast has no effect.

[quote:731e22f9b2]}
fin.close();

for(i=1; i<=10; i++){
cout<<buffer[i]<<endl;
[/quote:731e22f9b2]
You're starting at the second number in the buffer. Not the first.

[quote:731e22f9b2]}


The output is: -121 117 103 -80 -93 57 120 -36
108 47

But when I see the binary file, they are
?7F 87 75 67 B0 A3 39 78 DC 6C
?ïŠâ?ºgéâ"¢Â¤9xéâ?¡Â¿


The second byte in the input sequence is 0x87 which is a negative number
because
char is a signed type. You are also storing it in a (signed) int vector,
therefore the
C-style cast to unsigned int has no effect because the result of this cast
is
immediately cast again (implicit) to a (signed) int.

[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]


Back to top
tonci.tomic@mireo.hr
Guest





PostPosted: Wed Apr 20, 2005 9:08 am    Post subject: Re: Problem in transfering binary data to dec Reply with quote



"jiing" <jiing.deng (AT) gmail (DOT) com> wrote

Quote:
I have some problem in transfer the unsigned char in a file to the
decimal value

integer value, I suppose

Quote:
The following is my code:

Code:

ifstream fin("usb1_8192k_out.dat", ios::binary | ios::in );
if (!fin.is_open() ){
cout<<"input file is not open."<<endl;
}
char ch ;
vector<int> buffer;
vector<double> result;

while( fin.get( ch ) ){
buffer.push_back( (unsigned int)ch );
}
[/quote:01c55eeb1b]
ch is first converted to int, than to unsigned int (I think).
try:
buffer.push_back((unsigned char)ch);
you will get desired result

[quote:01c55eeb1b]fin.close();

for(i=1; i<=10; i++){
[/quote:01c55eeb1b]
for(i = 0; i < 10; i++)

that will fix off-by-one error

[quote:01c55eeb1b]cout<<buffer[i]<<endl;
}


The output is: -121 117 103 -80 -93 57 120 -36
108 47

But when I see the binary file, they are
?7F 87 75 67 B0 A3 39 78 DC 6C
?�g�9x�

The real decimal value I wanted should be
127 135 117 103 176 163 57 120 220 108

7F(hex) is 127(dec)
Can anyone tell me, what mistake I made. I can not figure out it.
Thank you in advance.

[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]


Back to top
kanze@gabi-soft.fr
Guest





PostPosted: Wed Apr 20, 2005 9:27 am    Post subject: Re: Problem in transfering binary data to dec Reply with quote

jiing wrote:
Quote:
I have some problem in transfer the unsigned char in a file to
the decimal value
The following is my code:

[code]
[...]
char ch ;
vector<int> buffer;
vector<double> result;

while( fin.get( ch ) ){
buffer.push_back( (unsigned int)ch );

This is strange, to say the least. If you want an integer
value, then you should convert to int (although this is
implicit). Formally, the conversion of an unsigned int to an
int is implementation defined, and may raise a signal; in
practice, the implementation will wrap, resulting in negative
values.

Quote:
From the rest of your post, I think what you actually want it to
convert the numeric value of the character (in whatever the

character code). For historical reasons, char is often signed,
and when using 8 bit encodings, some of the encodings will
appear negative when the value is stored in a char. The correct
solution here is to convert the char to an unsigned char (not
unsigned int), which will result in the correct numeric value;
this value then gets converted to int. So if you just change
the cast in your expression to (unsigned char), you'll get the
results that I think you want.

If you are really interested in the numeric values of the
characters, you might be interested in using the parameterless
version of get :

int ch = fin.get() ;
while ( ch != EOF ) {
buffer.push_back( ch ) ;
ch = fin.get() ;
}

This version of get returns an int, not a char -- the value of
the int is either EOF (which is guaranteed to be negative) or in
the range 0...UCHAR_MAX (which is 255 on all but the most exotic
architectures).

For reference, the way a negative signed value is converted to
an unsigned value by adding it to the maximum + 1 -- if you
convert a -1 to an unsigned char, this will typically result in
256-1 == 255. If you convert it to an unsigned int, however,
you will get 4294967295. When you attempt to convert this value
to an int, it doesn't fit. The results are implementation
defined (and can provoke a signal), but typically, will be -1;
converting from char to unsigned int to int will result in the
same value as that of just converting directly to int.

Logically, a type designed to hold characters would always be
unsigned, but C++ derives from C, and so it has to occasionnally
forgo logic.

--
James Kanze GABI Software
Conseils en informatique orientée objet/
Beratung in objektorientierter Datenverarbeitung
9 place Sémard, 78210 St.-Cyr-l'École, France, +33 (0)1 30 23 00 34


[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]


Back to top
kanze@gabi-soft.fr
Guest





PostPosted: Wed Apr 20, 2005 12:23 pm    Post subject: Re: Problem in transfering binary data to dec Reply with quote

Conrad Z. wrote:
Quote:
"jiing" <jiing.deng (AT) gmail (DOT) com> schreef in bericht
news:1113819550.330232.40580 (AT) g14g2000cwa (DOT) googlegroups.com...

I have some problem in transfer the unsigned char in a
file to the decimal value The following is my code:

Code:

ifstream fin("usb1_8192k_out.dat", ios::binary | ios::in );
if (!fin.is_open() ){
cout<<"input file is not open."<<endl;
}
char ch ;
vector<int> buffer;
vector<double> result;

while( fin.get( ch ) ){
buffer.push_back( (unsigned int)ch );

This cast has no effect.
[/quote:b097e43f4e]
It has the effect of making the code implementation defined.
The conversion char to unsigned int is well defined, but the
conversion unsigned int to int is implementation defined if the
value of the unsigned int is not representable in an int.

Most implementations just reinterpret the bits, without changing
them.  With anything but signed magnitude representation (and I
don't think that there are any machines using signed magnitude
in production today), this will result in the int having the
same value as the original char, IF char's are signed.  (If
char's are unsigned, and int is larger than char, all of the
conversions involved are guaranteed to preserve the value.)

[quote:b097e43f4e]}
fin.close();

for(i=1; i<=10; i++){
cout<<buffer[i]<<endl;

You're starting at the second number in the buffer. Not the first.
[/quote:b097e43f4e]
And accessing one past the end:-).  Well spotted.

[quote:b097e43f4e]}


The output is: -121 117 103 -80 -93 57 120
-36
108 47

But when I see the binary file, they are
?7F 87 75 67 B0 A3 39 78 DC 6C
?ïŠâ?ºgéâ"¢Â¤9xéâ?¡Â¿

The second byte in the input sequence is 0x87 which is a
negative number because char is a signed type.

Again, implementation defined -- logically, one would expect
char to be unsigned, but for historical reasons... Given his
output, of course, we can conclude that char's are signed in his
implementation. (Most implementations have a compiler option to
control this, although signed is almost universally the
default. Historical reasons, again.)

Quote:
You are also storing it in a (signed) int vector, therefore
the C-style cast to unsigned int has no effect because the
result of this cast is immediately cast again (implicit) to a
(signed) int.

It's a bit more complicated than that, but that pretty much sums
up the results on most modern machines.

--
James Kanze GABI Software
Conseils en informatique orientée objet/
Beratung in objektorientierter Datenverarbeitung
9 place Sémard, 78210 St.-Cyr-l'École, France, +33 (0)1 30 23 00 34


[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]


Back to top
Display posts from previous:   
Post new topic   Reply to topic    C++Talk.NET Forum Index -> C++ Language (Moderated) All times are GMT
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum


Powered by phpBB © 2001, 2006 phpBB Group
SEO toolkit © 2004-2006 webmedic.