I have an unsigned char variable that contains, for example, the value 40. I want the int variable to receive this value. What is the easiest and most effective way to do this? Thank you very much.
unsigned char c = 40; int i = c;
Presumably your question should be more than that ...
Try one of the following: it works for me. If you require a more specific selection, you can check Boost lexical_cast and reinterpret_cast .
unsigned char c = 40; int i = static_cast<int>(c); std::cout << i << std::endl;
or
unsigned char c = 40; int i = (int)(c); std::cout << i << std::endl;
Actually, this is an implicit cast. This means that your value is automatically cast as it does not overflow or end.
This is an example:
unsigned char a = 'A'; doSomething(a); // Implicit cast double b = 3.14; doSomething((int)b); // Explicit cast neccesary! void doSomething(int x) { ... }
Depends on what you want to do:
to read the value as ascii code you can write
char a = 'a'; int ia = (int)a; /* note that the int cast is not necessary -- int ia = a would suffice */
to convert the character '0' → 0, '1' → 1, etc., you can write
char a = '4'; int ia = a - '0'; /* check here if ia is bounded by 0 and 9 */
Google is a useful tool, but the answer is incredibly simple:
unsigned char a = 'A' int b = a
char *a="40"; int i= a;
The value in 'a' will be an ASCII value of 40 (not 40).
Instead, try using the strtol () function defined in stdlib.h
Just be careful, because this function is for the string. This will not work for the symbol.
Source: https://habr.com/ru/post/1412143/More articles:Deploying an ear with an application client on Glassfish 3.1.2 throws exceptions - javaRotate multiple images in a circle using jquery - jqueryHow to create an orbit of three elements around a logo - jquerySimple sum of jquery - jqueryhttps://translate.googleusercontent.com/translate_c?depth=1&rurl=translate.google.com&sl=ru&sp=nmt4&tl=en&u=https://fooobar.com/questions/1412142/how-can-i-get-search-word-when-a-visitor-comes-from-google-search-to-my-site&usg=ALkJrhj9hjur_zeuQ8aWaUCkhvhKEB5ztAHow can you zoom out using JavaScript in MobileSafari? - javascriptprolog, passing the function as a variable, how to add arguments? - declarativehttps://translate.googleusercontent.com/translate_c?depth=1&rurl=translate.google.com&sl=ru&sp=nmt4&tl=en&u=https://fooobar.com/questions/1412146/resolving-compile-time-references-to-different-versions-of-the-same-net-assembly-in-the-same-application&usg=ALkJrhiB1-k6uowQhLxmjd7DCgmvZ_LLYQBlock selection in TextBox / RichTextBox - wpfExtend your social_auth user model in Django - djangoAll Articles