473,704 Members | 2,448 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

C++: Convert unsigned char to CString and char *

6 New Member
It is a C++ questions:
How to Convert unsigned char to CString and char *; and backward as well?
Jan 12 '07 #1
3 33620
Banfa
9,065 Recognized Expert Moderator Expert
unsigned char to char * is fairly simple

Expand|Select|Wrap|Line Numbers
  1. unsigned char uc = 'A';
  2.  
  3. char *p = (char *)&uc;
  4.  
  5. /* and back */
  6.  
  7. uc = *p;  /* this may produce a warning that can be removed with a cast */
  8.  
The cast is needed because you are loosing the unsigned qualifier. Of course this does not give you a pointer to a c string because a c string requires zero termination there for a single character requires an array of at least 2 characters so that the zero terminator can be included.


unsigned char to CString is a little more problematic, I can't remember if you can just assign a char to a CString, if so no problem. If not the easiest way is probably to clear the CString and add (+) the unsigned char probably casting it first.


Expand|Select|Wrap|Line Numbers
  1. unsigned char uc ='A';
  2.  
  3. CString str;
  4.  
  5. str = (char)uc;  /* Not sure if this works */
  6.  
  7. str = "";          /* fairly sure this will work */
  8. str += (char)uc;
  9.  
  10. /* and back */
  11.  
  12. uc = str[0];  /* Extract the first character of str */
  13.  
Jan 13 '07 #2
Banfa
9,065 Recognized Expert Moderator Expert
I just checked you can construct a CString from an unsigned char like this

Expand|Select|Wrap|Line Numbers
  1. unsigned char uc = 'A';
  2.  
  3. CString str((char)uc);
  4.  
Jan 13 '07 #3
codercoder
6 New Member
Thank you all very much!
Jan 16 '07 #4

Sign in to post your reply or Sign up for a free account.

Similar topics

30
5052
by: Tim Johansson | last post by:
I'm new to C++, and tried to start making a script that will shuffle an array. Can someone please tell me what's wrong? #include <iostream.h> #include <string.h> int main () { srand(time(0)); int array_length; int count;
1
2832
by: b83503104 | last post by:
When are they not consistent?
4
1042
by: ravinderthakur | last post by:
hi all experts, can anybody explain me the difference between the unsigned char and char in c/c++ langugage. specifically how does this affects the c library fucntion such as strcat,strtok etc and their implementation.the way compiler treats them and the scenarios where one
15
10828
by: Yifan | last post by:
Hi Does anybody know how to convert System::String* to char*? I searched the System::String class members and did not find any. Thanks Yifan
3
41937
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 0x%x\n",(unsigned char)b); }
2
35553
yabansu
by: yabansu | last post by:
Hi, I just want to know if there is a way of converting a string variable into a const unsigned char pointer. Alternatively, I expect a blabla() function as the following: string str = "Hello"; const unsigned char* ptr = blabla(str); Thanks... yabansu
26
11727
by: =?gb2312?B?wNbA1rTzzOzKpg==?= | last post by:
i wrote: ----------------------------------------------------------------------- ---------------------------------------- unsigned char * p = reinterpret_cast<unsigned char *>("abcdg"); sizeof(reinterpret_cast<const char *>(p)); ----------------------------------------------------------------------- ---------------------------------------- the compiler tells me that "reinterpret_cast from type "const char * " to type "unsigned char *"...
1
25443
by: bindiya182003 | last post by:
Is it posiible to convert unsigned char to char. If its possible how can I d the conversion. Please help me.
8
4795
by: AGRAJA | last post by:
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*)?
0
8764
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
8677
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
9266
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
8974
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
7879
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...
0
5926
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
4694
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
3130
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
2468
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.