Go to Post Its already begun folks...the Amanda you know is now one of us! mwahahahaha! - Andy Grady [more]
Home
Go Back   Chief Delphi > Technical > Programming
CD-Media   CD-Spy  
portal register members calendar search Today's Posts Mark Forums Read FAQ rules

 
Closed Thread
Thread Tools Rate Thread Display Modes
  #1   Spotlight this post!  
Unread 21-06-2002, 16:45
Ian W. Ian W. is offline
College? What?
no team (Gompei and the Herd)
Team Role: College Student
 
Join Date: Jan 2002
Rookie Year: 2002
Location: Worcester, MA | Smithtown, NY
Posts: 1,464
Ian W. is a name known to allIan W. is a name known to allIan W. is a name known to allIan W. is a name known to allIan W. is a name known to allIan W. is a name known to all
Send a message via AIM to Ian W.
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.
__________________
AIM --> Woloi
Email --> ian@woloschin.com
  #2   Spotlight this post!  
Unread 21-06-2002, 17:48
Mike Soukup's Avatar
Mike Soukup Mike Soukup is offline
Software guy
FRC #0111 (Wildstang)
Team Role: Engineer
 
Join Date: May 2001
Rookie Year: 1996
Location: Schaumburg, IL
Posts: 797
Mike Soukup has a reputation beyond reputeMike Soukup has a reputation beyond reputeMike Soukup has a reputation beyond reputeMike Soukup has a reputation beyond reputeMike Soukup has a reputation beyond reputeMike Soukup has a reputation beyond reputeMike Soukup has a reputation beyond reputeMike Soukup has a reputation beyond reputeMike Soukup has a reputation beyond reputeMike Soukup has a reputation beyond reputeMike Soukup has a reputation beyond repute
Re: Dashboard programs and the char variable

Quote:
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
  #3   Spotlight this post!  
Unread 21-06-2002, 21:11
Ian W. Ian W. is offline
College? What?
no team (Gompei and the Herd)
Team Role: College Student
 
Join Date: Jan 2002
Rookie Year: 2002
Location: Worcester, MA | Smithtown, NY
Posts: 1,464
Ian W. is a name known to allIan W. is a name known to allIan W. is a name known to allIan W. is a name known to allIan W. is a name known to allIan W. is a name known to all
Send a message via AIM to Ian W.
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 .
__________________
AIM --> Woloi
Email --> ian@woloschin.com
  #4   Spotlight this post!  
Unread 21-06-2002, 21:59
Ian W. Ian W. is offline
College? What?
no team (Gompei and the Herd)
Team Role: College Student
 
Join Date: Jan 2002
Rookie Year: 2002
Location: Worcester, MA | Smithtown, NY
Posts: 1,464
Ian W. is a name known to allIan W. is a name known to allIan W. is a name known to allIan W. is a name known to allIan W. is a name known to allIan W. is a name known to all
Send a message via AIM to Ian W.
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?
__________________
AIM --> Woloi
Email --> ian@woloschin.com
  #5   Spotlight this post!  
Unread 21-06-2002, 22:51
Mike Soukup's Avatar
Mike Soukup Mike Soukup is offline
Software guy
FRC #0111 (Wildstang)
Team Role: Engineer
 
Join Date: May 2001
Rookie Year: 1996
Location: Schaumburg, IL
Posts: 797
Mike Soukup has a reputation beyond reputeMike Soukup has a reputation beyond reputeMike Soukup has a reputation beyond reputeMike Soukup has a reputation beyond reputeMike Soukup has a reputation beyond reputeMike Soukup has a reputation beyond reputeMike Soukup has a reputation beyond reputeMike Soukup has a reputation beyond reputeMike Soukup has a reputation beyond reputeMike Soukup has a reputation beyond reputeMike Soukup has a reputation beyond repute
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   Spotlight this post!  
Unread 21-06-2002, 23:11
Ian W. Ian W. is offline
College? What?
no team (Gompei and the Herd)
Team Role: College Student
 
Join Date: Jan 2002
Rookie Year: 2002
Location: Worcester, MA | Smithtown, NY
Posts: 1,464
Ian W. is a name known to allIan W. is a name known to allIan W. is a name known to allIan W. is a name known to allIan W. is a name known to allIan W. is a name known to all
Send a message via AIM to Ian W.
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]
__________________
AIM --> Woloi
Email --> ian@woloschin.com

Last edited by Ian W. : 21-06-2002 at 23:14.
  #7   Spotlight this post!  
Unread 22-06-2002, 01:54
Joe Ross's Avatar Unsung FIRST Hero
Joe Ross Joe Ross is offline
Registered User
FRC #0330 (Beachbots)
Team Role: Engineer
 
Join Date: Jun 2001
Rookie Year: 1997
Location: Los Angeles, CA
Posts: 8,562
Joe Ross has a reputation beyond reputeJoe Ross has a reputation beyond reputeJoe Ross has a reputation beyond reputeJoe Ross has a reputation beyond reputeJoe Ross has a reputation beyond reputeJoe Ross has a reputation beyond reputeJoe Ross has a reputation beyond reputeJoe Ross has a reputation beyond reputeJoe Ross has a reputation beyond reputeJoe Ross has a reputation beyond reputeJoe Ross has a reputation beyond repute
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;
}
I'll leave it to you to see if you can figure out why it works ;-)

Last edited by Joe Ross : 22-06-2002 at 01:56.
  #8   Spotlight this post!  
Unread 22-06-2002, 10:23
Ian W. Ian W. is offline
College? What?
no team (Gompei and the Herd)
Team Role: College Student
 
Join Date: Jan 2002
Rookie Year: 2002
Location: Worcester, MA | Smithtown, NY
Posts: 1,464
Ian W. is a name known to allIan W. is a name known to allIan W. is a name known to allIan W. is a name known to allIan W. is a name known to allIan W. is a name known to all
Send a message via AIM to Ian W.
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
so i'm not really sure what to do now . where's those more elegant solutions?
__________________
AIM --> Woloi
Email --> ian@woloschin.com
  #9   Spotlight this post!  
Unread 22-06-2002, 11:33
Joe Ross's Avatar Unsung FIRST Hero
Joe Ross Joe Ross is offline
Registered User
FRC #0330 (Beachbots)
Team Role: Engineer
 
Join Date: Jun 2001
Rookie Year: 1997
Location: Los Angeles, CA
Posts: 8,562
Joe Ross has a reputation beyond reputeJoe Ross has a reputation beyond reputeJoe Ross has a reputation beyond reputeJoe Ross has a reputation beyond reputeJoe Ross has a reputation beyond reputeJoe Ross has a reputation beyond reputeJoe Ross has a reputation beyond reputeJoe Ross has a reputation beyond reputeJoe Ross has a reputation beyond reputeJoe Ross has a reputation beyond reputeJoe Ross has a reputation beyond repute
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   Spotlight this post!  
Unread 22-06-2002, 13:48
Ian W. Ian W. is offline
College? What?
no team (Gompei and the Herd)
Team Role: College Student
 
Join Date: Jan 2002
Rookie Year: 2002
Location: Worcester, MA | Smithtown, NY
Posts: 1,464
Ian W. is a name known to allIan W. is a name known to allIan W. is a name known to allIan W. is a name known to allIan W. is a name known to allIan W. is a name known to all
Send a message via AIM to Ian W.
ok, thanks for the help.
__________________
AIM --> Woloi
Email --> ian@woloschin.com
  #11   Spotlight this post!  
Unread 22-06-2002, 15:07
Unsung FIRST Hero
Matt Leese Matt Leese is offline
Been-In-FIRST-Too-Long
FRC #1438 (The Aztechs)
Team Role: Engineer
 
Join Date: May 2001
Rookie Year: 1998
Location: Long Beach, CA
Posts: 937
Matt Leese has a reputation beyond reputeMatt Leese has a reputation beyond reputeMatt Leese has a reputation beyond reputeMatt Leese has a reputation beyond reputeMatt Leese has a reputation beyond reputeMatt Leese has a reputation beyond reputeMatt Leese has a reputation beyond reputeMatt Leese has a reputation beyond reputeMatt Leese has a reputation beyond reputeMatt Leese has a reputation beyond reputeMatt Leese has a reputation beyond repute
Send a message via AIM to Matt Leese
Quote:
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
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
  #12   Spotlight this post!  
Unread 24-06-2002, 02:03
Joe Ross's Avatar Unsung FIRST Hero
Joe Ross Joe Ross is offline
Registered User
FRC #0330 (Beachbots)
Team Role: Engineer
 
Join Date: Jun 2001
Rookie Year: 1997
Location: Los Angeles, CA
Posts: 8,562
Joe Ross has a reputation beyond reputeJoe Ross has a reputation beyond reputeJoe Ross has a reputation beyond reputeJoe Ross has a reputation beyond reputeJoe Ross has a reputation beyond reputeJoe Ross has a reputation beyond reputeJoe Ross has a reputation beyond reputeJoe Ross has a reputation beyond reputeJoe Ross has a reputation beyond reputeJoe Ross has a reputation beyond reputeJoe Ross has a reputation beyond repute
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;
}
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 ;-)
  #13   Spotlight this post!  
Unread 24-06-2002, 12:54
Ian W. Ian W. is offline
College? What?
no team (Gompei and the Herd)
Team Role: College Student
 
Join Date: Jan 2002
Rookie Year: 2002
Location: Worcester, MA | Smithtown, NY
Posts: 1,464
Ian W. is a name known to allIan W. is a name known to allIan W. is a name known to allIan W. is a name known to allIan W. is a name known to allIan W. is a name known to all
Send a message via AIM to Ian W.
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.
__________________
AIM --> Woloi
Email --> ian@woloschin.com

Last edited by Ian W. : 24-06-2002 at 12:57.
  #14   Spotlight this post!  
Unread 26-06-2002, 02:07
Greg Ross's Avatar
Greg Ross Greg Ross is offline
Grammar Curmudgeon
AKA: gwross
FRC #0330 (Beach 'Bots)
Team Role: Mentor
 
Join Date: Jun 2001
Rookie Year: 1998
Location: Hermosa Beach, CA
Posts: 2,245
Greg Ross has a reputation beyond reputeGreg Ross has a reputation beyond reputeGreg Ross has a reputation beyond reputeGreg Ross has a reputation beyond reputeGreg Ross has a reputation beyond reputeGreg Ross has a reputation beyond reputeGreg Ross has a reputation beyond reputeGreg Ross has a reputation beyond reputeGreg Ross has a reputation beyond reputeGreg Ross has a reputation beyond reputeGreg Ross has a reputation beyond repute
Send a message via AIM to Greg Ross Send a message via Yahoo to Greg Ross
Quote:
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.
__________________
Greg Ross (The Grammar Curmudgeon formerly known as gwross)
S/W Engineer, Team 330, the Beach 'Bots
<--The Grammar Curmudgeon loves this cartoon.
“Life should not be a journey to the grave with the intention of arriving safely in a pretty and well preserved body, but rather to skid in broadside in a cloud of smoke, thoroughly used up, totally worn out, and loudly proclaiming "Wow! What a Ride!" Hunter S. Thompson
"Playing a practical joke means doing something mean and calling it funny." Me
Closed Thread


Thread Tools
Display Modes Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump

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


All times are GMT -5. The time now is 20:04.

The Chief Delphi Forums are sponsored by Innovation First International, Inc.


Powered by vBulletin® Version 3.6.4
Copyright ©2000 - 2017, Jelsoft Enterprises Ltd.
Copyright © Chief Delphi