Convert int to char of int value [closed] - c++

Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 7 years ago.
Improve this question
I'm trying to find a way to convert an int to char of the corresponding value of the int (assuming int is one digit). (example 1='1' 5='5' 9='9') I've tried
int a=5;
char b=char(a+48);
whenever I try to run this the program crashes. How can I set up a system that works correctly?

It can be done using the following code:
char c = (char)(48 + a);
You can also use the '0' char value, instead of 48. It will improve code readability and let you not remember the value 48:
int a = 5;
char c = (char)((int)'0' + a);
As mentioned in comments, you can do this without explicit casts:
char c = '0' + a;

Related

Why char* Name = "string"; is not working in C++? [closed]

Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 2 years ago.
Improve this question
When I write a function like below and use it in main(), it doesn't work.
Why does this happen? (I am a beginner).
void addBst(char *name, char *num);
int main(void)
{
addBst("a", "b");
return 0;
}
In C++, a string literal is a const char[N] array, where N is the length of the string, including the null terminator.
Since C++11, it is illegal to assign a string literal to a non-const char* pointer, as your code is doing. You need to use a const char* pointer instead, eg:
void addBst(const char *name, const char *num);

Why ı cant print the string? [closed]

Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 2 years ago.
Improve this question
I am new at the c++. I made basic random string program but I cant print the string to console. These are my codes :
you did not include <string> and also you take the string by copy, to edit it you'll have to pass it by reference using &
#include <string>
void randomString(std::string& line)
you don't need to assign a number to int and then assign it to char, char is an integer value in c++ so you can:
char character = rand()%122 + 97;
with this method your random numbers will not be very good, you can look into How to generate a random number in C++?
you access your string with [] operator and it has not yet been defined, to add a character to a string just use
line += character;
also if you want a random number length, there is no need for the boolean and i++ stuff:
int charNumb = rand() % 8 + 4;
while(charNumb--)
will do just fine and it looks much cleaner.

How to copy char * to an array of 16 bytes [closed]

Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 4 years ago.
Improve this question
How to copy char * to an array of 16 bytes.
const char *SK = "1234456789999978";
sample_aes_gcm_128bit_key_t alice[16];
memcpy(alice, (sample_aes_gcm_128bit_key_t*)SK, 16); //gives error
Definition of sample_aes_gcm_128bit_key_t
typedef uint8_t sample_aes_gcm_128bit_key_t[16];
Error:
error: expected constructor, destructor, or type conversion before ‘(’ token
memcpy(alice, (sample_aes_gcm_128bit_key_t*)SK, 16);
This works for me:
#include <iostream>
#include <cstring>
int main()
{
typedef int sgx_aes_gcm_128bit_key_t;
const char *SK = "1234456789999978";
sgx_aes_gcm_128bit_key_t alice[16];
std::copy(SK, SK + strlen(SK), alice);
for(auto& i : alice){
std::cout << i - '0';
}
return 0;
}
Take care that your typedef name matches the name you use. (It didn't in question.) And you can remove the cast to sgx_aes_gcm_128bit_key_t. Hopefully this accomplishes what you're trying to do.
If you want to use memcpy with C++, that is fully possible. Inside memcpy you should use casts to (void*)
memcpy((void*)alice, (void*)SK, 16);
Make sure to get types and size right to prevent segmentation fault.

Need help whit c++ pointers [closed]

Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 6 years ago.
Improve this question
I'm studing about c++ semantics and syntax, I really don't know what is the problem with this code, it compile but stop working. I will apreciate your help, thanks.
#include <iostream>
#include <string.h>
using namespace std;
char* func(char* M)
{
int initval = 2;
char *x= new char[10];
x="idea";
strcpy(x, M+initval);
return x;
}
int main()
{
char* x;
char s[10]= "alguna";
x= func(s);
cout << *x << endl;
return 0;
}
Before this is closed, the x="idea"; is where your problem lies. You throw away your buffer and point it to a constant value, then try to assign to it, which almost always is illegal (should always be illegal, but apparently it is compiling for you...).

Char pointer in c++ [closed]

Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 8 years ago.
Improve this question
I am running the below code. I am getting run time error.
#include <iostream>
using namespace std;
int main() {
char *p="hello";
//p="Hi";
*p='G';
cout<<*p;
return 0;
}
if this is giving error then what is use of const char *p="hello";In this case my string should be constant not in char *p="hello"
char *p="hello";
*p='G';
You make p point to a constant, "hello". But then you try to modify what p points to. By definition, constants cannot be modified.