473,434 Members | 1,820 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,434 software developers and data experts.

pack FF in a unsigned char ?

hello,

is there any way that I can represent FF (11111111) as an unsigned char
?

unsigned char ch;
so if the user enters FF
and i have printf("%c",ch);
it should spit out FF

thanks

Nov 14 '05 #1
8 3992
ar****************@hotmail.com scribbled the following:
hello, is there any way that I can represent FF (11111111) as an unsigned char
? unsigned char ch;
so if the user enters FF
and i have printf("%c",ch);
it should spit out FF thanks


Numbers are numbers are numbers. They don't carry information about how
they are represented when written down. So, what you ask can't be done
as easily like that. You have to use the %x specifier with scanf() when
entering the number in and %x with printf() when printing it out.

--
/-- Joona Palaste (pa*****@cc.helsinki.fi) ------------- Finland --------\
\-------------------------------------------------------- rules! --------/
"To doo bee doo bee doo."
- Frank Sinatra
Nov 14 '05 #2
ar****************@hotmail.com wrote on 28/03/05 :
hello,

is there any way that I can represent FF (11111111) as an unsigned char
?
unsigned char c = 0xFF;
unsigned char ch;
so if the user enters FF
fgets() + strtoul() (base 16)
and i have printf("%c",ch);
it should spit out FF


You want the "%X" formatter.

printf ("%X", (unsigned) ch);

--
Emmanuel
The C-FAQ: http://www.eskimo.com/~scs/C-faq/faq.html
The C-library: http://www.dinkumware.com/refxc.html

"Clearly your code does not meet the original spec."
"You are sentenced to 30 lashes with a wet noodle."
-- Jerry Coffin in a.l.c.c++

Nov 14 '05 #3
To answer the question you actually asked, 0xff already fits in an
unsigned char data type.

However, to display it, you'll need to using putc(), fputc(), or
putchar(), assuming your printf() implementation doesn't support "%uc"
the way you want (or if you're looking for portability, I suppose).

unsigned char ch;
/* user enters a 0xff somehow into ch */
putchar(ch); /* because printf("%uc",ch); didn't do what I wanted
*/

Nov 14 '05 #4
Steven K. Mariner wrote:

To answer the question you actually asked, 0xff already fits in an
unsigned char data type.

However, to display it, you'll need to using putc(), fputc(), or
putchar(), assuming your printf() implementation doesn't support
"%uc" the way you want (or if you're looking for portability,
I suppose).
There is no such thing as "%uc". What are you expecting it to do?
unsigned char ch;
/* user enters a 0xff somehow into ch */
putchar(ch);


I doubt that your system will output "FF" when you execute that code.
It will actually display character number 255 in your execution
character set (if that character is displayable).

Nov 14 '05 #5
On 28 Mar 2005 00:58:18 -0800, ar****************@hotmail.com wrote:
hello,

is there any way that I can represent FF (11111111) as an unsigned char
?
unsigned char ch = 0xff;

If your compiler issues an obnoxious warning about storing an int in a
char, then change it to (char)0xff

unsigned char ch;
so if the user enters FF
If the user enters FF, then you will need to use one of the scanf
functions.
and i have printf("%c",ch);
it should spit out FF


%c will print only one character.

You can use the %X format specifier.
<<Remove the del for email>>
Nov 14 '05 #6
do*@dot.dot wrote:
On 28 Mar 2005 09:41:07 GMT, Joona I Palaste <pa*****@cc.helsinki.fi> wrote:
is there any way that I can represent FF (11111111) as an unsigned char
?

unsigned char ch;
so if the user enters FF
and i have printf("%c",ch);
it should spit out FF

thanks


Numbers are numbers are numbers. They don't carry information about how
they are represented when written down. So, what you ask can't be done
as easily like that. You have to use the %x specifier with scanf() when
entering the number in and %x with printf() when printing it out.


And to prevent overflows he has to store it as something other than an
unsigned char (0 - FF).


No, he doesn't. He wants the user to be able to enter FF, and read (and
redisplay) this as the hex value FF, which is representable in unsigned
char in all implementations.

Richard
Nov 14 '05 #7
> From: "Old WOlf" <oldw...@inspire.net.nz>
[...]
There is no such thing as "%uc".
Not in the standard, which is why I warned him -- twice -- that even
it if works on his compiler it's not the "right" answer, and then
proceeded to give him the "right" answer.
What are you expecting it to do?
Where I've seen it implemented, it displayed the character, even if it
was in the extended-ASCII set, as a single character.

Where it was not implemented, I think it has always generated a
diagnostic. It's been a long time since I've tried to use it.
unsigned char ch;
/* user enters a 0xff somehow into ch */
putchar(ch);


I doubt that your system will output "FF" when you execute
that code.


The OP did not ask for "FF" to be displayed.
The OP asked for FF to be displayed.

The absence of quotes suggests to me that he wants character number
255 from the character set to be displayed, not the literal string
"FF".
It will actually display character number 255 in your
execution character set (if that character is displayable).


You sure went a long way around the barn to agree with me.

_________________
Steven K. Mariner
ma*******@earthlink.net
http://home.earthlink.net/~marinersk/
http://www.whirlyjigmusic.com/

Nov 14 '05 #8
do*@dot.dot wrote:


Why do you have your postings set for non-archival? The information
you provide could be useful for years to come.

Just curious.

_________________
Steven K. Mariner
ma*******@earthlink.net
http://home.earthlink.net/~marinersk/
http://www.whirlyjigmusic.com/

Nov 14 '05 #9

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
by: hs | last post by:
Is the following correct? void foo(int len, char* val) { .. .. .. unsigned char* string = (unsigned char*) new char ; memcpy(string, val, len); ..
2
by: Tom | last post by:
Hi newsgroup, I have read a lot af articles about marshalling in C#, but none of them could help me to solve the following problem: There is a C-DLL with the following header-file: ++++++++...
3
by: QQ | last post by:
Hello, Here is my simple program int main() { unsigned char a =0x81; char b = 0x81; printf("unsigned char = 0x%x(%d), char = 0x%x(%d)\n",a,a,b,b); printf("cast char to unsigned...
5
by: Stephen Cawood | last post by:
I'm trying to use a C++ .lib from C# (I tried the Interop group will no results). I have a working wrapper DLL (I can get back simple things like int), but I'm having issues dealing with an array...
5
by: ryanlee101 | last post by:
I am getting a exception error when I complie my code. The error is: - imageData 0x00000000 <Bad Ptr> type unsigned char * I think it is from when I declare some of my char variables
26
by: =?gb2312?B?wNbA1rTzzOzKpg==?= | last post by:
i wrote: ----------------------------------------------------------------------- ---------------------------------------- unsigned char * p = reinterpret_cast<unsigned char *>("abcdg");...
0
by: Simon Posnjak | last post by:
Hi! I have a C module for which I created a wrapper with swig. The function def is: C: int some_thing(unsigned char * the_str); eg:
29
by: Kenzogio | last post by:
Hi, I have a struct "allmsg" and him member : unsigned char card_number; //16 allmsg.card_number
8
by: KYAW KYAW OO | last post by:
Dear All, Now, I am stuck above question so long and I would like to get Red, Green and Blue as unsigned char * each from unsigned char * of 24 bit BMP Color images. e.g unsigned char *...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
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...
0
agi2029
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,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
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...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.