How to set a character in C++? - c++

This is probably very obvious, in fact so obvious that no C++ reference I could find online cares to document it.
I need to know how to set the byte value of a char in C/C++. For example, if I want the byte value 233 in the char, how do I do?

Yes, it is too obvious:
char x = 233;

A char is a 1byte int. So you can set the value in the same way as you would an int.
char c = 123;
char d = 0x12;
etc...

char x = 233;
Yea, literally as simple as that.

Related

How can i get the char by having the value of that char in the ascii table?

I've been trying for a long time, searching a lot online, but I can't find anything about it...
I have an integer given by a function. This integer is the value of a char in the ascii table and I want to know what char it is. I've tried functions like toascii() or _itoa(), but none of these seems the right one...
Can you tell me what function should I use and with what parameteres?
Thanks.
It's very easy, no function needed, just assign it. In C++ chars are integers.
int char_value = ...;
char actual_char = char_value;
cout << actual_char << '\n';
You might add a cast to that assignment but it's not strictly necessary.
This integer is the value of a char in the ascii table and I want to know what char it is
you do not need any function
char theChar = (char) theNumber;
or better
char theChar = static_cast<char>(theNumber);
Thanks guys, I fixed it on my own, I don't know why but my IDE didn't let me do something like char CharVar = IntVar. Now somehow I fixed it and it works properly.

wchar_t* to short int conversion

One of the function in a 3rd party class return awchar_t* that holding a resource id (I don't know why it uses wchar_t* type ) I need to convert this pointer to short int
This method, using AND operator works for me. but it seems like not the correct way. is there any proper way to do this?
wchar_t* s;
short int b = (unsigned long)(s) & 0xFFFF;
wchar_t* s; // I assume this is what you meant
short int b = static_cast<short int>(reinterpret_cast<intptr_t>(s))
You could also replace short int b with auto b, and it will be deduced as short int from the type of the right-hand expression.
It returns the resource ID as a wchar_t* because that is the data type that Windows uses to carry resource identifiers. Resources can be identified by either numeric ID or by name. If numeric, the pointer itself contains the actual ID number encoded in its lower 16 bits. Otherwise it is a normal pointer to a null-terminated string elsewhere in memory. There is an IS_INTRESOURCE() macro to differentiate which is the actual case, eg:
wchar_t *s = ...;
if (IS_INTRESOURCE(s))
{
// s is a numeric ID...
WORD b = (WORD) s;
...
}
else
{
// s is a null-terminated name string
...
}
Did you mean in your code wchar_t *s;?
I'd do the conversion more explicit using
short int b = reinterpret_cast<short int>(s);
If it fits your application needs, I suggest using a data type with a fixed nr of bits, e.g. uint16_t. Using short int means you only know for sure your variable has at least 16 bits. An additional question: Why do you not use unsigned short int, instead of (signed) short int?
In general, knowing the exact nr of bits make things a little more predictable, and makes it easier to know exactly what happens when you cast or use bitmasks.

C/C++ integer to hex to char array to char

Well I have been trying to convert this integer into hex and have successfully done so but I need to use this hex for setting something. Now for this I need to use a char not a char array. Nothing else has worked without manually setting it. Maybe the problem lies in the issue that I use sprintf for the conversion to hex but either way I am sure there is a way to complete this task. Now What I need to change is have the output be char z but I haven't found a way to get this to work. Any help is greatly appreciated. Thanks
EDIT: now thi code may not make sense directly because it is incomplete and I saw no purpose inputting unrelated code. int x will never be over 100 and the whole point is to convert this into a hex and write it to the memory of a setting I have. So I have been trying to figure out how to convert the integer into hex into a char. nonstring as someone pointed out even though sprintf converts it to a string stored in a char as I just noticed. But I need to take the int convert to hex and assign that to a char variable forbuse later on. And that is where I am stuck. I do not know the best way to go about completely all that in a format and way without going into a string and other things.
VOID WriteSetting(int x)
{
char output[8];
sprintf(output, "0x%X", x);
char z = 0x46
unsigned char y = z
}
Working Code:
VOID WriteSetting(int x)
{
unsigned char y = (unsigned char)x;
Settingdb.Subset.Set = y;
}
If what you want is for:
WriteSetting(70);
or
WriteSetting(0x46);
to do the same thing as
char z = 0x46;
unsigned char y = z;
then all you need to do is:
void WriteSetting(int x)
{
unsigned char y = x;
}
Integers don't have any inherent base - they're just numbers. There's no difference at all between unsigned char y = 0x46; and unsigned char y = 70;.
Have you tried
printf("0x%X", z);
While the C++ standards do not explicitly require it, the size of a character is sometimes a byte, and an integer 4 bytes. I presume that this is why you are using an array of 4 characters.
So when you have an integer type and try to cram it into a single character, you'll lose precision.
P.S. A more precise explanation of sizes of data types is here: What does the C++ standard state the size of int, long type to be?
void WriteSetting(unsigned char *y, int x){
if(x > 100){
fprintf(stderr, "x value(%d) is invalid at %s\n", x, __func__);
return ;
}
*y = x;//There is no need for conversion probably
}

For loop writing special signs

void printchars()
{
for (x=128;x<224;x++)
write(x);
I want the x to be a char in the write function. How can i change the x to be treated by the write functions as a char, but an int in the loop?
What is the point of making x an int if you're just going to strip away its range? That's what makes this a very strange request. You should just make x a unsigned char -- for(unsigned char x = 128; x <224; ++ x) { ....
If you just want to ensure you're calling the unsigned char template specialization of write<>, then call it like this:
write<unsigned char>(x);
If not, then you will have to use type casting:
write((unsigned char)x);
Edit: I just realized what you might be experiencing. My guess is that you originally used char but found something wrong with numbers over 127. You should probably be using unsigned char for x instead of either int or char. I edited my answer to accommodate this. char has a range of -128 to +127. unsigned char has a range of 0-255.
Cast x to a char:
write(static_cast<char>(x));
Note that it is ok for x to be a char as the loop counter as well.

Getting first byte in a char* buffer

I have a char* buffer and I am interested in looking at the first byte in the char* buffer, what is the most optimal way to go about this.
EDIT: Based on the negative votes I might want to explain why this question, I am aware of methods but in the code base that I have been looking for getting first byte people do all kinds of crazy things like do a copy of the buffer , copy it to a stream and then do a get.
Just use
char firstByte = buffer[0];
Or this:
char firstByte = *buffer;
For clarification, there's no difference between *buffer and buffer[0], since the latter is really just shorthand for *(buffer + 0*sizeof(char)), and any compiler is going to be smart enough to replace that with *(buffer+0) and then *buffer. So the choice is really whichever is clearest in the context you are using it, not how efficient each one is.
char *buffer = {'h','e','l','l','o','\0'};
or:
char *buffer = "hello";
or:
char buffer[6] = {'h','e','l','l','o','\0'};
and to get the first byte:
char firstChar = buffer[0];
or:
char firstChar = *buffer; // since the buffer pointer points to the first element in the array
If you're determined to micro-optimize, you should know that every compiler made in this millennium should produce exactly the same machine code for "c = *buffer" and "c = buffer[0]".
char first = someCharPtr[0];
or
char first = *someCharPtr;
Just as a clarification of what several people have mentioned--that:
buffer[0]
is equivalent to
*(buffer + 0*sizeof(char))
That's not technically true if you assume that's literal C code (i.e. not pseudo code), although that's what the compiler is doing for you.
Because of pointer arithmetic, when you add an integer to a pointer, it is automatically multiplied by sizeof(*pointer), so it should really be:
*(buffer + 0)
Although, since sizeof(char) is defined to be 1, it is actually equivalent in this case.
char* c_ptr;
char first_char;
first_char = c_ptr[0];
Good for x86 platforms...
char firstByte;
__asm {
mov al, [buffer]
mov [firstByte], al
}