Solved

How do I convert a char into unsigned char?

Posted on 2010-09-20
4
543 Views
Last Modified: 2012-05-10
Hi Experts,
I have an array of unsigned char
static unsigned char m_ucTxBuffer[255];
I use memcpy to initialize the first element:
memcpy(&m_ucTxBuffer[0],"\55");
But In another part of the program I want to intialize the second element.  I use Borland C++ and I have an edit box that the user types in the value that will go into the second element.  The way Borland handles EditBoxes is
char normalBuf [10];
strcpy(normalBuf,Edit->Text.c_str())
when I try to copy normalBuf to m_ucTxBuffer[1] it complains that it can't convert char to unsigned char.
How would I put the value in normalBuf into m_ucTxBuffer?
Thanks
0
Comment
Question by:farcuri
4 Comments
 
LVL 12

Accepted Solution

by:
Mohamed Abowarda earned 125 total points
Comment Utility
0
 
LVL 53

Expert Comment

by:Infinity08
Comment Utility
Depends what you want the unsigned char buffer to contain.

But if I understand what you want correctly, just do the cast (ie. ignore the compiler warning) :

        char c;
        unsigned char uc = (unsigned char) c;
0
 
LVL 13

Expert Comment

by:Superdave
Comment Utility
I'm not sure what you're doing makes sense.  memcpy should have three arguments; the third should be 1 if you want to copy one char but you could just as well say
m_ucTxBuffer[0]='\55'; for the first example.
Then, how are you copying the text buffer?  If you want to copy it to positions 1-10 do
memcpy(&m_ucTxBuffer[1],normalBuf,10);

Signed vs. unsigned shouldn't matter much; memcpy should be declared to use void * anyway so I'm surprised if it gives a warning.  If you use strcpy instead, you could just say (char *)&m_ucTxBuffer[1], or (char *)(m_ucTxBuffer+1) which is the same thing.
0
 
LVL 5

Expert Comment

by:greatsubash
Comment Utility
Hi,
Please refer the following link,
http://www.cplusplus.com/forum/unices/10272/

0

Featured Post

IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

Suggested Solutions

An Outlet in Cocoa is a persistent reference to a GUI control; it connects a property (a variable) to a control.  For example, it is common to create an Outlet for the text field GUI control and change the text that appears in this field via that Ou…
This is a short and sweet, but (hopefully) to the point article. There seems to be some fundamental misunderstanding about the function prototype for the "main" function in C and C++, more specifically what type this function should return. I see so…
The goal of the tutorial is to teach the user how to use functions in C++. The video will cover how to define functions, how to call functions and how to create functions prototypes. Microsoft Visual C++ 2010 Express will be used as a text editor an…
The viewer will be introduced to the member functions push_back and pop_back of the vector class. The video will teach the difference between the two as well as how to use each one along with its functionality.

771 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

11 Experts available now in Live!

Get 1:1 Help Now