Mr. Starck
Mr. Starck

Reputation: 232

Convert char to byte c++

I'm trying to convert a char arry to a Byte (the microsoft type, I'm working with visual studio). The fact is that I want to fill in a DCB structure for serial port communication and I get back the informations from a .ini file. The datas are saved in a char temporary buffer. Some values in the DCB port settings require to be in byte. I tried many ways without success. The datas are well retrieved from the .ini but I have some problem to convert them to byte.

I could convert the numeric values to integer but they also have to be byte.

And the process->read method is a personnal implementation of GetPrivateProfileString.

char res[10];
printf("\nReading port settings of %s\n", comName);
processFileConfig->read(comName, "baudrate", "9600", res, sizeof(res));
baudr = atoi(res);

processFileConfig->read(comName, "parity", "N", res, sizeof(res));
parity = (BYTE)res;
std::cout << "size of Parity : ";
std::cout << sizeof(parity) <<std::endl;
std::cout << "content of Parity : ";
std::cout << parity <<std::endl;

processFileConfig->read(comName, "byteSize", "8", res, sizeof(res));
byteSize = atoi(res);
std::cout << "biteSize : ";
std::cout << byteSize <<std::endl;


processFileConfig->read(comName, "stopBits", "1", res, sizeof(res));
stopBits = atoi(res);
std::cout << "stopBits : ";
std::cout << stopBits <<std::endl;

portSettings.DCBlength = sizeof(portSettings);
portSettings.BaudRate = baudr; //this one is OK
portSettings.ByteSize = byteSize; // returns 
portSettings.Parity = parity; // returns nothing
portSettings.StopBits = stopBits; // returns a smiley :)

And an example of my file.ini :

[COM1]
baudrate=15000
byteSize=8
stopBits=1
parity=N

EDIT : the res buffer returns the good values from the .ini. Everything is OK on this side. I just need to convert the last three values (byteSize, stopBits and parity) to unsigned long.

Upvotes: 0

Views: 1481

Answers (2)

Mr. Starck
Mr. Starck

Reputation: 232

OK I got it. It was easy finally, I don't know why i blocked on it ^^ I created an intermediary unsigned long var :

processFileConfig->read(comName, "parity", "N", res, sizeof(res));
unsigned long parity_ul = res[0];
parity = parity_ul;
std::cout << "parity : "; // 78 for 'N'
std::cout << parity <<std::endl;

portSettings.Parity = parity;

std::cout << "Parity : ";
std::cout << portSettings.Parity <<std::endl; // 'N' that's OK !

i did the same with the other unsigned values. I directly get the implicit cast (well I suppose this is the term) of atoi(res) to unsigned long value. According VS debugger the values are correctly entered in the portSettings but when I try to print them it fails. This is not perfect but if the code works I thinks it's OK.

Thanks to everybody

Upvotes: 0

Medinoc
Medinoc

Reputation: 6608

Your current code attempts to convert a pointer to the buffer into a BYTE. What you need to do is parse the buffer with strtoul(), and cast that into a BYTE.

Upvotes: 2

Related Questions