Dashboard programs and the char variable

OK, a few quick questions…

  1. Do the dashboard programs (or any serial communications program for the matter) have to use char variables to read the bytes from the serial port?

  2. If you do indeed have to use char, can I use the int (char_variable) and char (int_variable) commands to insert data into the array? I’m assuming so, but I have some errors sometimes.

  3. Is a -1 in a char varaible the same as a 255? I’ve been testing my code, and when I want a 255 to come up, a -1 comes up instead. I think they’re the same, but once again, i’d like to check this out, because this may be causing errors to my program.

Thanks to anyone who can help.

*Originally posted by Ian W. *
3) Is a -1 in a char varaible the same as a 255? I’ve been testing my code, and when I want a 255 to come up, a -1 comes up instead. I think they’re the same, but once again, i’d like to check this out, because this may be causing errors to my program.

For a byte (which is what a char is), -1 is the same as 255, they are both stored as 0xFF in memory. If you print the variable out as a signed number (%d), it will be -1, if you print it out as an unsigned number (%u), it will be 255.

Mike

ah, ok. i was getting very confused, because of course, i learned none of this useful stuff in Comp Sci. oh well, thanks for the help, now i might be able to make this stupid program work :p.

Yet another question, how do i make 255 be couted, if the char variable is 0xFF? i think i need to know how to cout an unsigned integer, but i’m not sure if that’s what it’s called. anyone know?

Don’t worry about not knowing this kind of stuff, they don’t teach it in intro CS classes, they’re more focused on programming basics & syntax. 200 level classes usually introduce how variables are stored in memory and interpreted.

If you want to know more about how computers deal with negative numbers, search on google for ‘twos compliemnt.’ I’m not sure how much you know about computers, but all numbers are stored as hex values which don’t have +/- . Computers use twos compliment to store numbers because addition works. Using twos compliment, if you add a positive hex number and a negative number, the sum will be the correct value.

Now after making you read all that, I’ll answer your other question. I’m not very familiar with C++ and cout, but I found the solution on google groups:
cout << unsigned(my_byte) << endl;

I tested it and it works, but for some reason my compiler thinks a char is 4 bytes instead of 1 :confused:

Mike

strange, but if it works, i’ll take it. i’ll test and see what my compiler does.

[EDIT]
yeah, just tried it, and it makes my computer do some strange stuff. it gives me some huge number, which i believe is more than one byte. from your problem, i think it may be two bytes. if not, i have no idea whats wrong with it.
[/EDIT]

when you cast the char to an unsigned int, the char sign extends. This goes back to that twos compelement stuff and all that junk that you probably don’t want to understand. If you do want to understand, you know how to get a hold of me, though :-p

here is a hack that works, hopefully someone with more C/C++ experience then me knows a more elegant solution

#include <iostream>
using namespace std;

#define MAX <?

void main(void)
{
   unsigned char i = 255;

   cout << (i MAX 255) << endl;
}

I’ll leave it to you to see if you can figure out why it works :wink:

hmm, i think your hack only works for linux, or you have some file that i don’t have. it gives me an error about the std part, saying that there’s no namespace std…

E:\My Documents\C++\Dashboard\main.cpp(27) : error C2871: 'std' : does not exist or is not a namespace

so i’m not really sure what to do now :p. where’s those more elegant solutions? :smiley:

leave out that line. That was something that was needed when I took c++ a long time ago before the latest ANSI standard. It’s quite possible that it isn’t needed anymore. (My program compiles and runs it fine with and without the using namespace std; line.

When I took it, if you used namespace std, that kept you from having to type std::cout instead of just cout.

:stuck_out_tongue:

ok, thanks for the help.

*Originally posted by Mike Soukup *
**
I tested it and it works, but for some reason my compiler thinks a char is 4 bytes instead of 1 :confused:
**

It could be Unicode (aka UTF8) but I still find it odd as a Unicode character is usually only 2 bytes. The other possiblity is that because you’re using a 32-bit system, all variables are double-word aligned. As far as I know, Intel systems only word-align things but it’s a possiblity.

Matt

here is another solution using an union. This is better in that more people are probably able to figure out what it does, as opposed to the <?. However, it uses 4 bytes for each char, rather then just 1.

#include <iostream>

typedef union
{
	unsigned char byte;
	unsigned int print;
} chars;

void main(void)
{
	chars test1;
	test1.print = 0;	// must initialize all 4 bytes to 0

	test1.byte = 255;

	cout << test1.print << " " << test1.byte << endl;
}

Matt, I think when Mike said that a char was 4 bytes, he meant that when he cast the char to unsigned int, it printed out a number that could only be represented in 4 bytes, which would make sense becasue of the casting. To consider UTF-8, etc is just too much thinking :wink:

yeah, i ran that little program, see that it works, but how it works is rather beyond me. i know that you’re creating a new variable type, i believe of the type union, that has two other variables in it. but after that, i kinda get lost in how it works. care to explain joe? :stuck_out_tongue:

also, is it possible to take an array of char, and then turn it into an array of chars? i assume it is, through some use of casting, but i have no idea, as this is all new to me.

*Originally posted by Ian W. *
**…also, is it possible to take an array of char, and then turn it into an array of chars? i assume it is, through some use of casting, but i have no idea, as this is all new to me. **

Not a good idea. Since each “union chars” takes up 4 bytes, when you access the .print members, you will get only every 4th byte of the char array.