Solved

How do I convert a char into unsigned char?

Posted on 2010-09-20
4
566 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

Gigs: Get Your Project Delivered by an Expert

Select from freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely and get projects done right.

Question has a verified solution.

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

Have you thought about creating an iPhone application (app), but didn't even know where to get started? Here's how: ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ Important pre-programming comments: I’ve never tri…
Container Orchestration platforms empower organizations to scale their apps at an exceptional rate. This is the reason numerous innovation-driven companies are moving apps to an appropriated datacenter wide platform that empowers them to scale at a …
The goal of this video is to provide viewers with basic examples to understand and use switch statements in the C programming language.
The viewer will be introduced to the technique of using vectors in C++. The video will cover how to define a vector, store values in the vector and retrieve data from the values stored in the vector.

775 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