16

How to convert a string to Unsigned char in c++...

I have,

unsigned char m_Test[8];

I want to assign a string "Hello world" to m_Test.

how to do it?

3
  • I have changed the title of the question as in fact what you want to convert is a string literal. A string is a std class in c++ and the title was misleading. Hope you don't mind. Commented Jul 9, 2013 at 10:09
  • @IvayloStrandjev the current title is extremely misleading. The question is "how to assign a string literal to a unsigned char array" not "how to convert a string literal to unsigned char". And even if you had the direction correct, it is an unsigned char array, not an unsigned char. Commented Jun 1, 2016 at 16:55
  • @Mike correct I have fixed that now Commented Jun 2, 2016 at 7:22

8 Answers 8

26

Firstly, the array has to be at least big enough to hold the string:

 unsigned char m_Test[20];

then you use strcpy. You need to cast the first parameter to avoid a warning:

 strcpy( (char*) m_Test, "Hello World" );

Or if you want to be a C++ purist:

 strcpy( static_cast <char*>( m_Test ), "Hello World" );

If you want to initialise the string rather than assign it, you could also say:

 unsigned char m_Test[20] = "Hello World";
Sign up to request clarification or add additional context in comments.

1 Comment

Maybe worth mentioning that you can drop the size in the last example, since the compiler will figure it out.
6

For all practical purposes, the strcpy answers are correct, with the note that 8 isn't big enough for your string.

If you want to be really pedantic, you might need something like this:

#include <algorithm>

int main() {
    const char greeting[] = "Hello world";
    unsigned char m_Test[sizeof(greeting)];
    std::copy(greeting, greeting + sizeof(greeting), m_Test);
}

The reason is that std::copy will convert the characters in the original string to unsigned char. strcpy will result in the characters in the original string being reinterpreted as unsigned char. You don't say which one you want.

The standard permits there to be a difference between the two, although it's very rare: you'd need char to be signed, in an implementation with a 1s' complement or sign-magnitude representation. You can pretty much ignore the possibility, but IMO it's worth knowing about, because it explains the funny warnings that good compilers give you when you mix up pointers to char and unsigned char.

3 Comments

An array must have a constant value in between the [] and not using variables or functions. C++ rules.
Mostly true, but sizeof is not a function, and sizeof(greeting) is an integer constant expression. You can "use variables" in integer constant expressions in certain ways, and this is one of them, since the value of greeting isn't used, only the type, which is const char[12].
strlen(greeting) wouldn't be allowed in C++, for the reason you say.
5
strncpy(m_Test, "Hello world", sizeof(m_Test));

Here's Wikipedia on strncpy:

Comments

3

You can use c_str() function of std::string to get the char* out of string. This method basically returns a pointer to c-style string. After this you can use the normal string copying functions such as strcpy or strncpy to copy the value in to test.

Comments

2

You can use strcpy:

unsigned char m_Test[8];
strcpy((char*)m_Test, "Hello world");

Note that "Hello world" is too long for 8 bytes, so you will probably get a segfault.

Comments

2

You can use a string copy function like strcpy or even better strncpy, which has also some size checks:

strncpy ((char*) m_Test,"Hello World",8);

Comments

1

you can use strcpy function But have in mind that m_Test is only 8 size and there will be an overflow. Strcpy won't check that and you will get an exception

char * strcpy ( char * destination, const char * source );

4 Comments

But It won't copy the whole string anyway. I believe the main purpose is to copy the whole string.
"you will get an exception" - if you're lucky.
You will get an exception ... sooner or later :)
Not necessarily. You might just get the wrong output from your program. It might cause no problem at all. It really depends what it is on the stack that gets trashed. And this is assuming that hardware errors result in exceptions, which isn't certain.
0
string uInput;
cout << "Enter message" << endl;
getline(cin, uInput);

I'm adding 1 here because in c strings we have '\0' at the end

unsigned char dataS[uInput.size()+1];
strcpy(reinterpret_cast<char*>(dataS), uInput.c_str());

I think this example will help others more in the future.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.