C++: how do I convert hex char to unsigned char? - c++

I have a method that takes unsigned chars, but I want to pass it 0x01.
I know I can represent hex chars in strings by going "\x01"...
but that is still a signed char.
EDIT:
some code:
kennys_hash((unsigned char const *)"\x00"); // the method call
the error:
src/main.cpp:88: error: invalid conversion from ‘const unsigned char*’ to ‘unsigned char’
src/main.cpp:88: error: initializing argument 1 of ‘unsigned char kennys_hash(unsigned char)’
the method header:
unsigned char kennys_hash(unsigned char out)
ALso, when the cast is just to unsigned char, I get this error:
src/main.cpp:88: error: cast from ‘const char*’ to ‘unsigned char’ loses precision

0x01 is the same as 1, which is positive, and thus it doesn't matter if it's considered to be signed or unsigned, the value is the same.
If you want to have an unsigned type on the literal, use 0x01u.

Note that "\x00" is an string constant (read: array of char), and not a single character constant.
Use single quotes: '\x00' is a character constant.
The type might be char, but that is automatically converted to unsigned char when needed. Some compilers might issue a warning though.

You can use boost::lexical_cast:
unsigned char bar = boost::lexical_cast<unsigned char>( "\x71" );

void foo(unsigned char const * u);
...
foo( (unsigned char const *) "\x01");

You can pass it an array of unsigned chars, like this:
unsigned char data[5] = {1, 2, 3, 4, 5};
func_that_takes_unsigned_chars(data, sizeof data);

I bet you can't send it \x-01. Therefore you are sending it an unsigned int.
You can always test you your input before you send it to the func. If inp < 0 then send it back.

Related

How can I concatenate an uint64_t to unsigned char* in C++?

I want to concatenate a uint64_t to unsigned char*. I can not use sprintf(), strcpy(), and strcat() because they only accept char* as argument.
#include <cstdint>
// I want to concatenate these two:
unsigned char data[8] = "tJQVfvcj";
uint64_t num = 1732462110473334785;
The output should be equivalent to:
unsigned char concat[28] = "tJQVfvcj1732462110473334785";
The efficiency is also important, although it is the second priority. I highly appreciate any suggestions.
Edit: I used sprintf() like this:
unsigned char data[9] = "tJQVfvcj";
uint64_t num = 1732462110473334785;
unsigned char concat[28];
sprintf(concat, "%s%" PRId64 "", data, num);
However, it leads to this error:
error: invalid conversion from ‘unsigned char*’ to ‘char*’ [-fpermissive]

C++ how to convert from char * to unsigned char * in function call?

When building my C++ application the build fails at this line of code
if (!PyTuple_GetByte(poArgs, 0, &SourceCell.window_type))
with this error
error C2664: 'PyTuple_GetByte' : cannot convert parameter 3 from
'char *' to 'unsigned char *'
This is the called function:
bool PyTuple_GetByte(PyObject* poArgs, int pos, unsigned char* ret);
The third parameter &SourceCell.window_type is type char.
Is there a way to convert/cast the parameter inside the function call like
if (!PyTuple_GetByte(poArgs, 0, reinterpret_cast<unsigned char*>(&SourceCell.window_type)))
or do I have to deal with it in another way?
From the error, the signature of the PyTuple_GetByte function was expecting a third parameter of type unsigned char*, but you passed a variable of type char* at its invocation. I think you have two options here.
You can change the signature of function PyTuple_GetByte to expect a char* parameter.
You need to convert your input variable from type char* to type unsigned char*, before you can pass it into PyTuple_GetByte.
The conversion is normally like this:
unsigned char* convert_var = reinterpret_cast<unsigned char*>(&SourceCell.window_type); // (c++ way)
or
unsigned char* convert_var = (unsigned char*)(&SourceCell.window_type); // (c way)

Making strncmp() accept unsigned char * as an argument

I have a pointer to a string which is "unsigned char *" and i want to use it as an argument in strncmp() function.
But compiler complains:
invalid conversion from 'unsigned char*' to 'const char*'
How can i make:
strncmp(const char *a, unsigned char *b, size_t size);
work?
Is there any workaround?
You can safely cast unsigned into signed.
unsigned char * p2 = ...;
strncmp(px, (char *)p2, 5);
should work.
unsigned char * p2 = ...;
char * p3 = static_cast<char *>(p2);
This maybe overkill.
Not sure why you're using unsigned char*, but if you need to, use a cast. For example :
strncmp(a, (char*)b, 10);
use reinterpret_cast to convert it. Something like this -
strncmp(const char *a, reinterpret_cast<const char*>(unsigned char
*b), size_t size);

Conversion from 'const char *' to 'unsigned char *' in C++?

I have an encryption function declared as follows: int encrypt(unsigned char* keydata, int keydata_len, unsigned char *plaintext, int plaintext_len, unsigned char *ciphertext). This works perfectly and now I call it according to the given snippet.
const char *password = "password";
len = encrypt(password, (int)strlen(password), (unsigned char*)(content.c_str()), (int)strlen(content.c_str()), ciphertext);
On compiling the C++ code I get an error as:
crest.cc:52:13: error: no matching function for call to 'encrypt'
len = encrypt(password, (int)strlen(password), (unsigned char*)(content.c_str()), (int)strlen(content.c_str()), ciphertext);
^~~~~~~
./aes.h:10:5: note: candidate function not viable: no known conversion from 'const char *' to 'unsigned char *' for 1st argument
int encrypt(unsigned char* keydata, int keydata_len, unsigned char *plaintext, int plaintext_len, unsigned char *ciphertext);
What is the correct way to typecast in C++ to get around this error ?
There are several problems with this code, and the only (correct) way to fix them is to ensure that you use the correct data types in the first place.
So, instead of const char *password = "password", use unsigned char password[] = "password"; (of course, this will probably give you trouble with strlen, since it won't like unsigned char) - using sizeof(password)-1 will work in THIS instance, but is ill advised as a general solution, since password may well be not be directly available as an array - not sure quite what you should do as an "ideal" solution, really.
Now, the question can be asked whether it is actually correct to have a non-const input to the function. If you have the source for encrypt, you may want to change the function to encrypt(const unsigned char* keydata, size_t keydata_len, const unsigned char* plaintext, size_t plaintext_len, unsigned char* ciphertext) - still doesn't fix the problem with strlen of unsigned char of course, but it's what I'd expect the prototype to be for a function like this.
An alternative would be to rewrite the encrypt function to make casts to unsigned only internally, and use char * inputs (with const where relevant).
Note that it's NOT valid to cast away constness EXCEPT for cases where you know that the original content is not const (which I think is NOT guaranteed to be the case for std::string::c_str(), but std::string::data() should work)

C code in C++ compiler

I have following code, it's code from tomcrypto's manual and it won't work on MS VC++ 2008 EE. Any help? Also can I ask replace char* by std::string object?
int main(void)
{
hash_state md;
unsigned char *in = "hello world", out[16];
/* setup the hash */
md5_init(&md);
/* add the message */
md5_process(&md, in, strlen(in));
/* get the hash in out[0..15] */
md5_done(&md, out);
return 0;
}
Errors:
\main.cpp(7) : error C2440: 'initializing' : cannot convert from 'const char [12]' to 'unsigned char *'
Types pointed to are unrelated; conversion requires reinterpret_cast, C-style cast or function-style cast
.\main.cpp(11) : error C2664: 'strlen' : cannot convert parameter 1 from 'unsigned char *' to 'const char *'
Types pointed to are unrelated; conversion requires reinterpret_cast, C-style cast or function-style cast
EDIT: Now code looks like:
int main(void)
{
register_hash(&md5_desc);
hash_state md;
char* p = "hello wordl";
unsigned char *in = reinterpret_cast<unsigned char*>(p);
char* out[16];
/* setup the hash */
md5_init(&md);
/* add the message */
md5_process(&md, const_cast<char*>(in), strlen(in));
/* get the hash in out[0..15] */
md5_done(&md, out);
return 0;
}
Errors:
\main.cpp(21) : error C2440: 'const_cast' : cannot convert from 'unsigned char *' to 'char *'
Types pointed to are unrelated; conversion requires reinterpret_cast, C-style cast or function-style cast
.\main.cpp(21) : error C2664: 'strlen' : cannot convert parameter 1 from 'unsigned char *' to 'const char *'
Types pointed to are unrelated; conversion requires reinterpret_cast, C-style cast or function-style cast
.\main.cpp(23) : error C2664: 'md5_done' : cannot convert parameter 2 from 'char *[16]' to 'unsigned char *'
Types pointed to are unrelated; conversion requires reinterpret_cast, C-style cast or function-style cast
unsigned char *in = "hello world"
This is incorrect in C++: "hello world" is a string literal and is of type const char[12]. In C it is of type char[12], but the const here doesn't matter because in C++ there is an implicit (but deprecated) conversion that allows a string literal to be converted to a char*.
The problem is that char and unsigned char are different types. It doesn't matter whether char is unsigned; the three char types (char, unsigned char, and signed char) are all distinct and in C++ you cannot convert between pointers to those three types without a cast.
This works in C because in C you can convert any pointer-to-object type to any other pointer-to-object type without a cast. That isn't the case in C++.
In C++ you would need to use:
// use the implicit conversion to 'char*' to cast away constness:
char* p = "hello world";
// explicitly cast to 'unsigned char*'
unsigned char* in = reinterpret_cast<unsigned char*>(p);
The removal of constness is usually a bad idea since string literals are not modifiable, but sometimes it is necessary when dealing with legacy libraries that are not const-correct.
The conversion from char* to unsigned char* is safe because all objects can be treated as an array of char, unsigned char, or signed char in C++.
char is a different type to signed char or unsigned char; string literals are always of type (const) char *; so you cannot assign them to a (const) signed char * or a (const) unsigned char *. To fix this, remove the unsigned from line 4.
If your md5_process() function explicitly takes an unsigned char * as an argument, then you should perform a cast at that point:
md5_process(&md, reinterpret_cast<unsigned char*>(in), strlen(in));
[As others have said, you should really define in as const char *in as it's pointing to a string literal, but that is not the issue here.]
Let's try again:
int main(void)
{
register_hash(&md5_desc);
hash_state md;
const char* p = "hello wordl";
const unsigned char* in = reinterpret_cast<const unsigned char*>(p);
unsigned char out[16];
/* setup the hash */
md5_init(&md);
/* add the message */
md5_process(&md, in, strlen(p));
/* get the hash in out[0..15] */
md5_done(&md, out);
return 0;
}
Does this work?
This is because litteral strings are const in C++, while you initialize it with a non-const pointer:
const char* in = "hello world";
char * out[16];
However it might cause a problem if md5_process takes a non-const char*, in this case you'll have to cast to a non-const:
md5_process(&md, const_cast<char*>(in), strlen(in));