I'm getting crazy. Look at this code:
#include <string.h>
#include <stdio.h>
#include <iostream.h>
using namespace std ;
char ini_code[2] = {0xFF, 0xFE} ;
char line_sep[2] = {0x20, 0x28} ;
char para_sep[2] = {0x20, 0x29} ;
char end_code[2] = {0xFF, 0xFF} ;
char tab_code[2] = {0x00, 0x09} ;
char alf_code[2] = {0x00, 0x0A} ;
char acr_code[2] = {0x00, 0x0D} ;
int main ()
{
char code[2] ;
bool gotCR = false ;
cin.read(&code[0], 2) ;
code[0] = ini_code[1] ;
code[1] = ini_code[0] ;
printf("0x%02X% 02X\n",code[0],code[1]);
while (cin.read(&code[0], 2))
{
if (code[0] == tab_code[1] && code[1] == tab_code[0])
{
code[0] = line_sep[1] ;
code[1] = line_sep[0] ;
printf("0x%02X% 02X\n",code[0],code[1]);
}
else if (code[0] == acr_code[1] && code[1] == acr_code[0])
{
gotCR = true ;
}
else if (code[0] == alf_code[1] && code[1] == alf_code[0])
{
if (gotCR)
{
code[0] = para_sep[1] ;
code[1] = para_sep[0] ;
printf("0x%02X% 02X\n",code[0],code[1]);
}
else
{
gotCR = false ;
}
}
else
{
printf("0x%02X% 02X\n",code[0],code[1]);
}
}
code[0] = end_code[1] ;
code[1] = end_code[0] ;
printf("0x%02X% 02X\n",code[0],code[1]);
return 0 ;
}
I expect a list of
0xNNNN
0xNNNN
....
0xNNNN
Instead I obtain stuff like:
0x004B
0x006F
0x0072
0x0065
0x0061
0x006E
0x0009
0x00FFFFFFC6
0xFFFFFFC5FFFFF FC8
0xFFFFFFC5FFFFF FB5
0xFFFFFFC2FFFFF FC8
0xFFFFFFB2FFFFF FE4
0xFFFFFFB209
0x0009
0x0009
0x0000
0x4800
0x6500
0x6200
0x7200
0x6500
Why??????
~~~~~~~~~~~~~~~ ~~~~~~~~~~~~~~
Dr. Dario de Judicibus http://www.dejudicibus.it/
~~~~~~~~~~~~~~~ ~~~~~~~~~~~~~~ 16 3108
Dario de Judicibus escribió: char ini_code[2] = {0xFF, 0xFE} ; char line_sep[2] = {0x20, 0x28} ; char para_sep[2] = {0x20, 0x29} ; char end_code[2] = {0xFF, 0xFF} ; char tab_code[2] = {0x00, 0x09} ; char alf_code[2] = {0x00, 0x0A} ; char acr_code[2] = {0x00, 0x0D} ;
Use unsigned char.
Regards. char ini_code[2] = {0xFF, 0xFE} ; char line_sep[2] = {0x20, 0x28} ; char para_sep[2] = {0x20, 0x29} ; char end_code[2] = {0xFF, 0xFF} ; <= what should happen when you detect
this? char tab_code[2] = {0x00, 0x09} ; char alf_code[2] = {0x00, 0x0A} ; char acr_code[2] = {0x00, 0x0D} ;
int main () { char code[2] ; bool gotCR = false ;
cin.read(&code[0], 2) ;
code[0] = ini_code[1] ; code[1] = ini_code[0] ; printf("0x%02X% 02X\n",code[0],code[1]); while (cin.read(&code[0], 2)) { if (code[0] == tab_code[1] && code[1] == tab_code[0]) { code[0] = line_sep[1] ; code[1] = line_sep[0] ; printf("0x%02X% 02X\n",code[0],code[1]); } else if (code[0] == acr_code[1] && code[1] == acr_code[0]) { gotCR = true ; } else if (code[0] == alf_code[1] && code[1] == alf_code[0]) { if (gotCR) { code[0] = para_sep[1] ; code[1] = para_sep[0] ; printf("0x%02X% 02X\n",code[0],code[1]); } else { gotCR = false ; <= this is ALREADY false here! } } else { printf("0x%02X% 02X\n",code[0],code[1]); } }
code[0] = end_code[1] ; code[1] = end_code[0] ; printf("0x%02X% 02X\n",code[0],code[1]);
return 0 ; }
I expect a list of
0xNNNN 0xNNNN ... 0xNNNN
Instead I obtain stuff like:
0x004B 0x006F 0x0072 0x0065 0x0061 0x006E 0x0009 0x00FFFFFFC6 0xFFFFFFC5FFFFF FC8 0xFFFFFFC5FFFFF FB5 0xFFFFFFC2FFFFF FC8 0xFFFFFFB2FFFFF FE4 0xFFFFFFB209 0x0009 0x0009 0x0000 0x4800 0x6500 0x6200 0x7200 0x6500
Why??????
~~~~~~~~~~~~~~~ ~~~~~~~~~~~~~~ Dr. Dario de Judicibus http://www.dejudicibus.it/ ~~~~~~~~~~~~~~~ ~~~~~~~~~~~~~~
Without seeing your input, it's hard to tell. I have a question: why do
you define the codes in the reverse order that you expect to see them? Is
it intentional (for some reason I can't imagine), or is your code doing the
checks wrong?
I do see at least one clear problem: The handling of GotCR is not
correct. You never set it to false after the first time it gets set to
true. Your code to set it to false is in the else of an "if (GotCR)", which
means it only gets set to false when it is ALREADY false!
It also looks like you're getting those "end" codes, which probably
means you have to handle them differently, but there's no code to detect and
handle them. Same with the other codes, like the tab, etc..
But again, with no input to go by, we can't tell how the output gets
generated for sure. Try walking through your app in the debugger and see
what the variable values are at each step. You might also try doing it on
paper to check your design.
-Howard char ini_code[2] = {0xFF, 0xFE} ; char line_sep[2] = {0x20, 0x28} ; char para_sep[2] = {0x20, 0x29} ; char end_code[2] = {0xFF, 0xFF} ; char tab_code[2] = {0x00, 0x09} ; char alf_code[2] = {0x00, 0x0A} ; char acr_code[2] = {0x00, 0x0D} ;
Use unsigned char.
Why? The char type is neither unsigned nor signed unless explicitly
stated, and he's not doing any math or '>' or '<' comparisons where signed
vs. unsigned might make a difference.
The problems, I think, are that his logic is incorrect and incomplete.
(He's not handling all cases, and he's handling the CR incorrectly.)
-Howard
Howard escribió: char ini_code[2] = {0xFF, 0xFE} ; char line_sep[2] = {0x20, 0x28} ; char para_sep[2] = {0x20, 0x29} ; char end_code[2] = {0xFF, 0xFF} ; char tab_code[2] = {0x00, 0x09} ; char alf_code[2] = {0x00, 0x0A} ; char acr_code[2] = {0x00, 0x0D} ;
Use unsigned char. Why? The char type is neither unsigned nor signed unless explicitly stated, and he's not doing any math or '>' or '<' comparisons where signed vs. unsigned might make a difference.
The char type is a separated type at many effects, but or it has sign or
it has not. If is has sign, 0xFF when converted to int and outputted in
hex gives many more F, as the ouput of the OP shows. Them I suppose that
is the case,
Regards.
"Howard" <al*****@hotmai l.com> wrote in message news:bp******** @dispatch.conce ntric.net...
char ini_code[2] = {0xFF, 0xFE} ;
Use unsigned char.
Why? The char type is neither unsigned nor signed unless explicitly stated, and he's not doing any math or '>' or '<' comparisons where signed vs. unsigned might make a difference.
If char is 8 bits and signed, 0xFF isn't a defined initializer.
"Dario de Judicibus" <no****@nowhere .com> wrote in message news:bp******** ***@newsreader2 .mclink.it... 0x00FFFFFFC6 0xFFFFFFC5FFFFF FC8 0xFFFFFFC5FFFFF FB5 0xFFFFFFC2FFFFF FC8 0xFFFFFFB2FFFFF FE4 0xFFFFFFB209
Classic sign extension bug. Your signed char gets expanded to int (standard procedure for
vararg'd function like printf). For example 0xFF most likely initialized the char value as -1.
"%X", -1 prints 0xFFFFFFFFF.
You either should use unsigned char or you will have to mask off the sign extensions.
"Ron Natalie" <ro*@sensor.com > wrote in message
news:3f******** *************@n ews.newshosting .com... "Howard" <al*****@hotmai l.com> wrote in message
news:bp******** @dispatch.conce ntric.net...
> char ini_code[2] = {0xFF, 0xFE} ; Use unsigned char.
Why? The char type is neither unsigned nor signed unless explicitly stated, and he's not doing any math or '>' or '<' comparisons where
signed vs. unsigned might make a difference.
If char is 8 bits and signed, 0xFF isn't a defined initializer.
???
But I thought char was *neither* signed nor unsigned, unlike int, which
is signed by default. Are there some implementations that treat assigning
255 to a char as undefined behavior? (That would kind of screw up a lot of
code that uses "extended" ASCII characters, wouldn't it?)
-Howard
"Howard" <al*****@hotmai l.com> wrote in message news:bp******** @dispatch.conce ntric.net... But I thought char was *neither* signed nor unsigned,
It is a distinct type from signed char or unsigned char, but it will have
the representation of one of those two (it's clearly signed in the original
poster's case).
Are there some implementations that treat assigning 255 to a char as undefined behavior?
Implementation-defined. Attempting to convert numbers that
are larger than can be represented into signed values is implmentation
defined. Unsigneds on the hand are required to wrap module 2**number of bits.
(That would kind of screw up a lot of code that uses "extended" ASCII characters, wouldn't it?)
The problem is not the char representation of "FF" but the fact
that using an integer 0xFF to initialize a signed char may not yield
the right value.
"Ron Natalie" <ro*@sensor.com > wrote in message
news:3f******** *************@n ews.newshosting .com... "Dario de Judicibus" <no****@nowhere .com> wrote in message
news:bp******** ***@newsreader2 .mclink.it... 0x00FFFFFFC6 0xFFFFFFC5FFFFF FC8 0xFFFFFFC5FFFFF FB5 0xFFFFFFC2FFFFF FC8 0xFFFFFFB2FFFFF FE4 0xFFFFFFB209 Classic sign extension bug. Your signed char gets expanded to int
(standard procedure for vararg'd function like printf). For example 0xFF most likely initialized
the char value as -1. "%X", -1 prints 0xFFFFFFFFF.
You either should use unsigned char or you will have to mask off the sign
extensions.
Oh, I see, said the blind man! :-) That's pretty poor behavior, in my
opinion. I don't recall ever using unsigned char to store C-style arrays of
characters. I've always used just char. Of course, I don't think I've ever
used printf on such an array either, so I guess I wouldn't have noticed this
strange effect.
-Howard This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics |
by: Phil |
last post by:
Hi,
I don't understand this strange behaviour:
I compile this code :
#include <Python.h>
#include"Numeric/arrayobject.h"
static PyObject *
|
by: Ethel Aardvark |
last post by:
I am running a 9.0.1 database on a W2K server and have come across
some strange behaviour with a SQL query.
I have a query which runs in a PL/SQL cursor which has several PL/SQL
variables used to switch on and off certain rules. One idea I had was
to have two queries UNIONed together with a simple switch selecting
which half was to operate (I know it sounds like there are probably
better ways of doing this but I have my reasons).
To cut...
|
by: Torsten Reiners |
last post by:
Hi,
it might be a simple solution but I do not see it. The problem is that
I have the following file stored on my local harddrive. All references
are URL to a remote computer. It is working, i.e. the code is not
throwing an error (btw the field should link to other pages by
clicking them).
The strange behavior is, that I copy the file to the remote computer
and execute it by typing http://www.smartframe.de/dumm.html (everybody
|
by: Grzegorz Kaczor |
last post by:
Hello all,
I've got a VERY strange network problem with Win2k Server and .NET.
I've got one central server (hub) getting raw binary data (files) from
many locations. Both server and clients are written in C#
The server is quite simple: two threads, one accepts new connections
and decides whether the client is authenticated to send data or not, and
the other thread serves already connected clients: performs a
Socket.Select and then...
|
by: Sebastian C. |
last post by:
Hello everybody
Since I upgraded my Office XP Professional to SP3 I got strange
behaviour.
Pieces of code which works for 3 years now are suddenly stop to work
properly.
I have Office XP Developer (SP3 for Office, SP1 for developer,
JET40SP8) on Windows XP Home Edition (SP1). The same behaviour occurs
on Windows 98 too.
| |
by: DeltaOne |
last post by:
#include<stdio.h>
typedef struct test{
int i;
int j;
}test;
main(){
test var;
var.i=10;
var.j=20;
|
by: Michael Meckelein |
last post by:
Hello,
I run into trouble move down a selected item in a listbox. The code moving
down the item is the following one:
for (int j = lv.SelectedItems.Count-1; j >=0; j--)
{
ListViewItem moveItem = lv.SelectedItems;
selIdx = moveItem.Index;
// ignore movedown of last item
|
by: JoReiners |
last post by:
Hello, I have a really strange problem. I'm unable to figure it out on
my own.
I parse very simple xml documents, without any check for their form.
These files look very similar and are encoded in UTF-8.
Now minidom is always able to parse these files with
minidom.parse("file") .
Now when fetching I use this expression:
xmldoc.getElementsByTagName('DocNumb').firstChild.data.encode('latin1')
|
by: Dox33 |
last post by:
I ran into a very strange behaviour of raw_input().
I hope somebody can tell me how to fix this.
(Or is this a problem in the python source?)
I will explain the problem by using 3 examples. (Sorry, long email)
The first two examples are behaving normal, the thirth is
strange.......
I wrote the following flabbergasting code:
#-------------------------------------------------------------
|
by: Pilcrow |
last post by:
This behavior seems very strange to me, but I imagine that someone will
be able to 'explain' it in terms of the famous C standard.
-------------------- code -----------------------------------
#include <stdio.h>
int main (void)
{
char xx="abcd";
char * p1 = xx;
|
by: Hystou |
last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it.
First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
| |
by: jinu1996 |
last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth.
The Art of Business Website Design
Your website is...
|
by: Hystou |
last post by:
Overview:
Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
|
by: tracyyun |
last post by:
Dear forum friends,
With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
|
by: agi2029 |
last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own....
Now, this would greatly impact the work of software developers. The idea...
|
by: conductexam |
last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one.
At the time of converting from word file to html my equations which are in the word document file was convert into image.
Globals.ThisAddIn.Application.ActiveDocument.Select();...
|
by: TSSRALBI |
last post by:
Hello
I'm a network technician in training and I need your help.
I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs.
The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols.
I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
| |
by: adsilva |
last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
|
by: 6302768590 |
last post by:
Hai team
i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
| |