strip characters from char array pass as pointer in C++ - c++

any suggestion on how to strip characters from char array pass as pointer in C++. i must use memcpy function to copy.
void foo(char *test)
{
char a[1] = {0};
char b[1] = {0};
char c[1]= {0};
memcpy(&a,&test[0],1);
memcpy(&b,&test[1],1);
memcpy(&c,&test[2],1);
cout << a <<endl;
cout << b <<endl;
cout << c <<endl;
}
int main()
{
char uibuffer[4] = "ABC";
foo(uibuffer);
return 0;
}
the current output is:
ABC��
BC��
C��
desired output is:
A
B
C

void foo(char *test)
{
/* Please note, you need to add one extra byte here for a terminator */
char a[2] = {0};
char b[2] = {0};
char c[2]= {0};
memcpy(&a,&test[0],1);
memcpy(&b,&test[1],1);
memcpy(&c,&test[2],1);
cout << a <<endl;
cout << b <<endl;
cout << c <<endl;
}
int main()
{
char uibuffer[4] = "ABC";
foo(uibuffer);
return 0;
}
OR
Think about improving your code by getting rid of arrays and memory copying. Simple char a = buffer[x] will do the trick.

since you haven't created \0 terminated strings do:
cout << a[0] <<endl;
cout << b[0] <<endl;
cout << c[0] <<endl;

Don't make a, b and c an array. Probably won't compile but to illustrate.
void foo(char *test)
{
char a = 0;
char b = 0;
char c = 0;
memcpy(&a,&test[0],1);
memcpy(&b,&test[1],1);
memcpy(&c,&test[2],1);
cout << a <<endl;
cout << b <<endl;
cout << c <<endl;
}
int main()
{
char uibuffer[4] = "ABC";
foo(uibuffer);
return 0;
}

Don't make them arrays:
char a = test[0];
char b = test[1];
char c = test[2];
You can still use memcpy, if you must, just as you are now.

Related

Reference variables alias in c++

I am new in coding Please explain this code. I didn't get this logic.
#include<iostream>
void main()
{
int a = 32, *p = &a;
char c ='A', &ch = c;
ch += a;
*p += c;
cout << "\n" << a << " " << c << endl;
}
Since p is the location of a, *p – the int at that location – is the same int as a.
Similarly, since ch is a reference to c, ch is the same char as c.
Thus, what you have is equivalent to (after fixing the typos)
#include<iostream>
int main()
{
int a = 32;
char c ='A';
c += a;
a += c;
std::cout << "\n" << a << " " << c << std::endl;
}

C+ how to change array of chars in function

I have this header:
MvProjectQueue & operator >> (char *);
And I have to write a function meeting this spec. (my function should "return" an array of chars using >> operator)
I have to change the passed argument, i.e. I get an array of char when my function is called and I have to modify it (in place).
Normally I would use
MvProjectQueue & operator >> (char **);
got a pointer to char * and I would solved it easily, using something like this:
#include <iostream>
using namespace std;
class SimpleShowcaseObject
{
public:
SimpleShowcaseObject(){};
~SimpleShowcaseObject(){};
SimpleShowcaseObject & operator >> (char ** p_ch)
{
*p_ch = "changed";
cout << "in scope: " << *p_ch << endl;
return *this;
}
};
int main(void)
{
char *ch = new char[10];
ch = "hello";
SimpleShowcaseObject o = SimpleShowcaseObject();
cout << "original: " << ch << endl;
o >> &ch;
cout <<"changed: " << ch << endl;
delete[] ch;
cin.get();
return 0;
}
But this:
#include <iostream>
using namespace std;
class SimpleShowcaseObject
{
public:
SimpleShowcaseObject(){};
~SimpleShowcaseObject(){};
SimpleShowcaseObject & operator >> (char *ch)
{
ch = "changed";
cout << "in scope: " << ch << endl;
return *this;
}
};
int main(void)
{
char *ch = new char[10];
ch = "hello";
SimpleShowcaseObject o = SimpleShowcaseObject();
cout << "original: " << ch << endl;
o >> ch;
cout <<"changed: " << ch << endl;
delete[] ch;
cin.get();
return 0;
}
executes and prints:
original: hello
in scope: changed
changed: hello
and I would like to have
original: hello
in scope: changed
changed: changed
(edited several times, big thanks to everyone for trying to help!)
Your function receive what should be a buffer you can write to:
SimpleShowcaseObject & operator >> (char *ch)
{
//ch = "changed";// with this you are changing the value of the pointer, not what you want
strcpy (ch, "changed");
cout << "in scope: " << ch << endl;
return *this;
}
As other have already pointed out, this is not a good design: your operator >> has to write a string (char[]) in a buffer, without knowing the lenght of input buffer; not a nice scenario.

Converting char to unsigned integer

I read a huge binary file into a vector of chars.
I need to treat every byte as an unsigned integer(from 0 to 255); and do some arithmetics. How can I convert vector to vector?
char a = 227;
cout << a;
prints ?
char a = 227;
int b = (int) a;
cout << b << endl;
prints -29
char a = 227;
unsigned int b = (unsigned int) a;
cout << b << endl;
prints 4294967267
char a = 227;
unsigned char b = (unsigned char) a;
cout << b << endl;
prints ?
std::vector<char> source;
// ... read values into source ...
// Make a copy of source with the chars converted to unsigned chars.
std::vector<unsigned char> destination;
for (const auto s : source) {
destination.push_back(static_cast<unsigned char>(s))
}
// Examine the values in the new vector. We cast them to int to get
// the output stream to format it as a number rather than a character.
for (const auto d : destination) {
std::cout << static_cast<int>(d) << std::endl;
}

Questions regarding Libsodium

I am trying to use the lib-sodium cryptography library in Visual Studio 2015.
Following is my piece of code
unsigned char pk[crypto_box_PUBLICKEYBYTES];
unsigned char sk[crypto_box_SECRETKEYBYTES];
crypto_box_keypair(pk, sk);
cout << "\nPrivate Key: " << pk;
cout << "\nSecret Key: " << sk;
cout << "\n";
int m_len = 6, c_len = crypto_box_SEALBYTES + m_len;
unsigned char *m=NULL, *c=NULL;
m = (unsigned char *)sodium_malloc(m_len);
c = (unsigned char *)new unsigned char(c_len);
unsigned char *m2=NULL;
m2 = (unsigned char *) new unsigned char(m_len);
//Message to be encrpted
m[0] = 'M';
m[1] = 'C';
m[2] = '2';
m[3] = '2';
m[4] = '5';
m[5] = '\0';
cout << "\nMessage is: " << m;
if (crypto_box_seal(c, m, m_len, pk) != 0) {
cout << "\nFail in cypto";
return 1;
}
cout << "\nCrypted is: " << c;
if (crypto_box_seal_open(m2, c, c_len, pk, sk) != 0) {
printf("crypto_box_seal_open() failure\n");
return 1;
}
cout << "\nDecrypt is: " << m2;
Questions are as follows:
Every time I run the code I get different sizes of public and private key, but the size of variable I use is constant. How can this be possible?
I am trying to use sodium_malloc function to allocate memory for crypted message and decrypted message. But it throws a violation of accessing wrong memory space. Is this a problem because i am coding in C++ or is there any other reason?
Please let me know if you need any other information regarding the problem.

Writing value to c style string in struct

For the life of me I can't figure out why the I can't write to a c style string inside of a struct.
College student - can't use string class, haven't learned pointers.
Help? 2 hours at trying to figure this out.
#include <iostream>
using namespace std;
void strCopy(char from[], char to[])
{
for (int i = 0; i < 255; i++)
{
to[i] = from[i];
}
}
struct card
{
char suit[20];
char rank[20];
int cvalue;
char location[20];
};
void printCard(card card)
{
cout << card.rank << " of " << card.suit << endl;
}
int main()
{
// I don't think strCopy()'s the problem, I've used it with my last project.
cout << "Test strCopy()" << endl;
char str1[14] = "abcdefghijklm";
char str2[14];
strCopy(str1, str2);
cout << " " << str2 << endl << endl;
// Now the negative.
card one;
one.cvalue = 2;
strCopy("Somewhere", one.location);
strCopy("Two", one.rank);
strCopy("Hearts", one.suit);
printCard(one);
}
// I don't think strCopy()'s the problem, I've used it with my last
project.
Wrong
for (int i = 0; i < 255; i++)
{
to[i] = from[i];
}
copies 255 characters, however that's not what you meant.
If here :
strCopy(str1, str2);
cout << " " << str2 << endl << endl;
Your're getting "correct" output, then you're just unlucky, since that invokes an undefined behavior, an you're writing off the end of the array.