|
|
|
![]() |
|
|||||||
|
||||||||
![]() |
|
|
Thread Tools | Rate Thread | Display Modes |
|
|
|
#1
|
||||
|
||||
|
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. |
|
#2
|
||||
|
||||
|
Re: Dashboard programs and the char variable
Quote:
Mike |
|
#3
|
||||
|
||||
|
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
. |
|
#4
|
||||
|
||||
|
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?
|
|
#5
|
||||
|
||||
|
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 Mike |
|
#6
|
||||
|
||||
|
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] Last edited by Ian W. : 21-06-2002 at 23:14. |
|
#7
|
||||||
|
||||||
|
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 Code:
#include <iostream>
using namespace std;
#define MAX <?
void main(void)
{
unsigned char i = 255;
cout << (i MAX 255) << endl;
}
Last edited by Joe Ross : 22-06-2002 at 01:56. |
|
#8
|
||||
|
||||
|
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...
Code:
E:\My Documents\C++\Dashboard\main.cpp(27) : error C2871: 'std' : does not exist or is not a namespace . where's those more elegant solutions? ![]() |
|
#9
|
||||||
|
||||||
|
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. ![]() Last edited by Joe Ross : 22-06-2002 at 11:44. |
|
#10
|
||||
|
||||
|
ok, thanks for the help.
|
|
#11
|
|||||
|
|||||
|
Quote:
Matt |
|
#12
|
||||||
|
||||||
|
try a union
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.
Code:
#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;
}
|
|
#13
|
||||
|
||||
|
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?
![]() 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. Last edited by Ian W. : 24-06-2002 at 12:57. |
|
#14
|
|||||
|
|||||
|
Quote:
|
![]() |
| Thread Tools | |
| Display Modes | Rate This Thread |
|
|
Similar Threads
|
||||
| Thread | Thread Starter | Forum | Replies | Last Post |
| Dashboard Decoder Release Version 1.0 Now Available! | archiver | 2000 | 1 | 23-06-2002 22:50 |