Solved

How do I convert a char into unsigned char?

Posted on 2010-09-20
4
580 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
ID: 33720204
0
 
LVL 53

Expert Comment

by:Infinity08
ID: 33720631
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
ID: 33721059
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
ID: 33722821
Hi,
Please refer the following link,
http://www.cplusplus.com/forum/unices/10272/

0

Featured Post

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
C++ mouse_event mouse look 7 111
Outlook 13 84
Display a Float Variable in C without using the function printf. 2 37
C++ help/ Toy problem 19 52
Templates For Beginners Or How To Encourage The Compiler To Work For You Introduction This tutorial is targeted at the reader who is, perhaps, familiar with the basics of C++ but would prefer a little slower introduction to the more ad…
Go is an acronym of golang, is a programming language developed Google in 2007. Go is a new language that is mostly in the C family, with significant input from Pascal/Modula/Oberon family. Hence Go arisen as low-level language with fast compilation…
Video by: Grant
The goal of this video is to provide viewers with basic examples to understand and use nested-loops in the C programming language.
The viewer will learn how to pass data into a function in C++. This is one step further in using functions. Instead of only printing text onto the console, the function will be able to perform calculations with argumentents given by the user.

756 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