View Single Post
  #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,573
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 ;-)