473,903 Members | 3,114 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

convert unsigned to char

how to convert unsigned to char?

Ref: http://www.thescripts.com/forum/thread477545.html

how do I print without the leading ffffff (yet the result should be
char*)?
Dec 14 '07 #1
8 4810
AGRAJA wrote:
how to convert unsigned to char?

Ref: http://www.thescripts.com/forum/thread477545.html
Don't reference a webpage, ask us a question.
Dec 14 '07 #2
AGRAJA wrote:
>
how to convert unsigned to char?
int main (void)
{
unsigned u = 0;
char c = (char)u;

return 0;
}

--
pete
Dec 14 '07 #3
AGRAJA wrote:
how to convert unsigned to char?
Use the cast operator: (char)
Ref: http://www.thescripts.com/forum/thread477545.html

how do I print without the leading ffffff (yet the result should be
char*)?
Printing a pointer type requires the "%p" format specifier. On the
machines I use most often, getting lots of leading 'f's is pretty much
unavoidable when using %p with valid pointer values. However, are you
sure you want the result to be char*? In context, I'd have expected that
you wanted to print a char, not a char*.

If that's the case, then you should use either "%d" or "%u" format
specifier, depending upon whether or not char is signed (if CHAR_MIN <
0, then char is a signed type). Either way, you're absolutely guaranteed
to not get any 'f's in the output. :-)

However, I suspect that what you really want is to get no 'f's despite
using the "%x" specifier. On most implementations where 'char' is
unsigned, you're extremely unlikely to get leading f's when printing a
char value.

Therefore, I assume that you're using an implementation where 'char' is
signed. You still won't get many leading 'f's on most implementations ,
unless the value you're converting to 'char' is greater than CHAR_MAX.
Don't do that! Performing such a conversion will either generate an
implementation-defined result, or cause an implementation-defined signal
to be raised. Either way, it's generally not a good thing to do. It's
likely to result in a negative number; if you convert that number to
unsigned, so that it can be printed using the "%x" format, then the
conversion will generally result in lots of leading 'f's.

I'd recommend using 'unsigned char' rather than 'char' for such purposes.
Dec 14 '07 #4
pete wrote:
AGRAJA wrote:
>how to convert unsigned to char?

int main (void) {
unsigned u = 0;
char c = (char)u;

return 0;
}
int main (void) {
unsigned u = 0;
char c;

c = u; /* provided that u value <= CHAR_MAX */
return 0;
}

No cast needed. The error also happens with a cast.

--
Chuck F (cbfalconer at maineline dot net)
<http://cbfalconer.home .att.net>
Try the download section.

--
Posted via a free Usenet account from http://www.teranews.com

Dec 14 '07 #5
James Kuyper <ja*********@ve rizon.netwrites :
AGRAJA wrote:
>how to convert unsigned to char?

Use the cast operator: (char)
A cast is neither necessary nor helpful. Just assign it; the
conversion is implicit.
>Ref: http://www.thescripts.com/forum/thread477545.html

how do I print without the leading ffffff (yet the result should be
char*)?

Printing a pointer type requires the "%p" format specifier. On the
machines I use most often, getting lots of leading 'f's is pretty much
unavoidable when using %p with valid pointer values. However, are you
sure you want the result to be char*? In context, I'd have expected
that you wanted to print a char, not a char*.
Neither the original post nor the cited web page mentions char* or
"%p".

[...]

--
Keith Thompson (The_Other_Keit h) <ks***@mib.or g>
Looking for software development work in the San Diego area.
"We must do something. This is something. Therefore, we must do this."
-- Antony Jay and Jonathan Lynn, "Yes Minister"
Dec 15 '07 #6
Keith Thompson wrote:
Neither the original post nor the cited web page mentions char* or
"%p".
The OP did indeed mention char* (and you quoted him):
>>how do I print without the leading ffffff (yet the result should be
char*)?
What he meant by this, I have no idea.

Phil
Dec 15 '07 #7
Keith Thompson wrote:
James Kuyper <ja*********@ve rizon.netwrites :
>AGRAJA wrote:
>>how to convert unsigned to char?
Use the cast operator: (char)

A cast is neither necessary nor helpful. Just assign it; the
conversion is implicit.
While that is true in an assignment context, the question as posed was
about conversion in general.
>>Ref: http://www.thescripts.com/forum/thread477545.html

how do I print without the leading ffffff (yet the result should be
char*)?
Printing a pointer type requires the "%p" format specifier. On the
machines I use most often, getting lots of leading 'f's is pretty much
unavoidable when using %p with valid pointer values. However, are you
sure you want the result to be char*? In context, I'd have expected
that you wanted to print a char, not a char*.

Neither the original post nor the cited web page mentions char* or
"%p".
???
"... (yet the result should be char*)?"

I don't know what the OP intended that to mean. The result of a
conversion to char is char, by definition. The result of a C expression
that prints something by calling printf() is a count of the characters
printed. The result, in a more informal sense, of printing something is
the appearance of a set of characters on the display device. I can't
figure out any meaning of "the result" which fits this context, for
which char* is the appropriate type.

However, since he DID mention char*, I decided to say something about
char*, even if what I said probably had nothing to do with what he
actually wanted. That was my (probably overly subtle) way of pointing
out to him that his comment about char* was unclear.

Dec 15 '07 #8
James Kuyper <ja*********@ve rizon.netwrites :
Keith Thompson wrote:
>James Kuyper <ja*********@ve rizon.netwrites :
>>AGRAJA wrote:
how to convert unsigned to char?
Use the cast operator: (char)

A cast is neither necessary nor helpful. Just assign it; the
conversion is implicit.

While that is true in an assignment context, the question as posed was
about conversion in general.
Yes, but in most cases conversion from one arithmetic type to another
doesn't require a cast.
>>>Ref: http://www.thescripts.com/forum/thread477545.html

how do I print without the leading ffffff (yet the result should be
char*)?
Printing a pointer type requires the "%p" format specifier. On the
machines I use most often, getting lots of leading 'f's is pretty much
unavoidable when using %p with valid pointer values. However, are you
sure you want the result to be char*? In context, I'd have expected
that you wanted to print a char, not a char*.

Neither the original post nor the cited web page mentions char* or
"%p".

???
"... (yet the result should be char*)?"
Whoops, I missed that. D'oh!
I don't know what the OP intended that to mean. The result of a
conversion to char is char, by definition. The result of a C
expression that prints something by calling printf() is a count of the
characters printed. The result, in a more informal sense, of printing
something is the appearance of a set of characters on the display
device. I can't figure out any meaning of "the result" which fits this
context, for which char* is the appropriate type.

However, since he DID mention char*, I decided to say something about
char*, even if what I said probably had nothing to do with what he
actually wanted. That was my (probably overly subtle) way of pointing
out to him that his comment about char* was unclear.
I suspect the OP wanted a string.

--
Keith Thompson (The_Other_Keit h) <ks***@mib.or g>
Looking for software development work in the San Diego area.
"We must do something. This is something. Therefore, we must do this."
-- Antony Jay and Jonathan Lynn, "Yes Minister"
Dec 15 '07 #9

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

Similar topics

3
31524
by: Siemel Naran | last post by:
Hi. Is there a way to convert the type signed int to the type unsigned int, char to unsigned char, signed char to unsigned char, and so on for all the fundamental integer types? Something like template <> struct to_unsigned<signed int> : public std::unary_function<signed int, unsigned int> { unsigned int operator()(signed int x) const { return x; } };
13
7936
by: Bryan Parkoff | last post by:
I have two variables: "char A" and "short B". I can be able to convert from A to B using explicit case conversion with no problem like "B = short (A);". Right now, I have two variables: "char T" and "short A". T has an array of six elements. I desire to capture first element and second element as two bytes into word as short. The problem is that "A" captures only one element instead of two elements. I have looked at machine language...
7
13095
by: Golan | last post by:
Hi, I need to convert a Binary value to Decimal. I've been told that the value is an unsigned one. How can I do this? I use memcpy into an unsigned char variable, but when I print the value I got a negative value. For example if I'm using the xd -c (Unix) on the file, I can see the value FFFFFFFFFFFFFFA2 which using the memcpy as described above I get -94. But the real value that I'd expect to get is a positive one.
7
7134
by: whatluo | last post by:
Hi, all I'm now working on a program which will convert dec number to hex and oct and bin respectively, I've checked the clc but with no luck, so can anybody give me a hit how to make this done without strtol or s/printf function. Thanks, whatluo.
16
10145
by: Dave | last post by:
Hi all, I have a 4 byte char array with the binary data for two 16-bit signed integers in it like this: Index 3 2 1 0 Data Bh Bl Ah Al Where Bh is the high byte of signed 16-bit integer B and so on.
65
21541
by: kyle.tk | last post by:
I am trying to write a function to convert an ipv4 address that is held in the string char *ip to its long value equivalent. Here is what I have right now, but I can't seem to get it to work. #include <string.h> #include <stdio.h> /* Convert an ipv4 address to long integer */ /* "192.168.1.1" --> 3232235777 */ unsigned long iptol(char *ip){
24
14954
by: cedarson | last post by:
I am having trouble writing a simple code that will convert an int ( 487 for example ) to binary form just for the sake of printing the binary. Can someone please help? Thanks!
10
26675
by: sposes | last post by:
Im very much a newbie but perhaps somehone can help me. Ive been searching for a way to convert a std::string to a unsigned char* The situation is I have a function that wants a unsigned char* and I want to give it a std::string no matching function for call to `MD5::update(std::string&, size_t)' candidates are: void MD5::update(unsigned char*, unsigned int) void PrintMD5(string str){
14
13574
by: rtillmore | last post by:
Hello, I did a quick google search and nothing that was returned is quite what I am looking for. I have a 200 character hexadecimal string that I need to convert into a 100 character string. This is what I have so far: #include <stdio.h> #include <stdlib.h> #include <string.h>
0
10003
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
10882
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
10988
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
9692
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, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
8055
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 presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
7213
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5897
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 last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
6099
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
2
4312
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.