linux writing string value as hex to serial - c++

here's my problem
i have a string with hex value such as
std::string str ="8C000002008E"
and i want to write this as hex to serial out by using the
write()
I have a sony Display which i want to control with.
Passing a
unsigned char test[6] = {0x8c, 0x00, 0x00, 0x02, 0x00, 08E};
to the write method works.
But i dont know how to convert the string to such a char array especially that the size of the char array has to be calculated on runtime.
Thanks for your help.
Here is my full code
#include <stdio.h>
#include <iostream>
#include <cstring>
#include <fcntl.h>
#include <termios.h>
#include <sstream>
using namespace std;
#define TERM_DEVICE "/dev/ttyUSB0"
#define TERM_SPEED "B9600"
int main() {
std::string teststr = "8C000002008E";
int fd, old_flags;
ssize_t length;
char buffer[16];
struct termios term_attr;
fd_set input_fdset;
if ((fd = open(TERM_DEVICE, O_RDWR)) == -1)
{
perror("terminal: Can't open device " TERM_DEVICE);
return(1);
}
/* RS232 konfigurieren */
if (tcgetattr(fd, &term_attr) != 0)
{
perror("terminal: tcgetattr() failed");
return(1);
}
cfsetispeed(&term_attr, B9600);
cfsetospeed(&term_attr, B9600);
term_attr.c_cflag &= ~PARENB;
term_attr.c_cflag &= CS8;
term_attr.c_cflag &= CSIZE;
term_attr.c_cflag &= CSTOPB;
term_attr.c_iflag = 0;
term_attr.c_oflag = OPOST | ONLCR;
term_attr.c_lflag = 0;
if (tcsetattr(fd, TCSAFLUSH, &term_attr) != 0)
perror("terminal: tcsetattr() failed");
if (tcgetattr(STDIN_FILENO, &term_attr) != 0)
{
perror("terminal: tcgetattr() failed");
return(1);
}
old_flags = term_attr.c_lflag;
term_attr.c_lflag &= ~(ICANON | ECHO);
if (tcsetattr(STDIN_FILENO, TCSAFLUSH, &term_attr) != 0)
perror("terminal: tcsetattr() failed");
while (1)
{
FD_ZERO(&input_fdset);
FD_SET(STDIN_FILENO, &input_fdset);
FD_SET(fd, &input_fdset);
if (select(fd+1, &input_fdset, NULL, NULL, NULL) == -1)
perror("terminal: select() failed");
unsigned char test[6] = {0x8c, 0x00, 0x00, 0x02, 0x00, 0x8E};
if (FD_ISSET(STDIN_FILENO, &input_fdset)){
if ((length = read(STDIN_FILENO, buffer, 16)) == -1)
perror("terminal: read() failed");
else
if (buffer[0] == '\33')
break;
else{
write(fd, test , sizeof(test));
}
}
if (FD_ISSET(fd, &input_fdset))
{
if ((length = read(fd, buffer, 16)) == -1)
perror("terminal: read() failed");
else
cout << std::hex << buffer<< endl;
}
}
term_attr.c_lflag = old_flags;
if (tcsetattr(STDIN_FILENO, TCSAFLUSH, &term_attr) != 0)
perror("terminal: tcsetattr() failed");
printf("Aborted.\n");
close(fd);
return 0;
}

If the problem is only one of converting the string to a char array, you can try the following:
#include <iostream>
#include <sstream>
int main(int argc, char **argv)
{
const std::string str ="8C000002008E";
// count the number of character pairs (i.e. bytes) in the string
// and dynamically allocate an array of the required size
const int numBytes = str.size() / 2;
unsigned char* bytes = new unsigned char[numBytes];
for (int i = 0; i < numBytes; ++i)
{
// grab two characters from the string...
std::string twoChars = str.substr(2 * i, 2);
// ...and convert them to an integer using a stringstream
int byte;
std::stringstream ss(twoChars);
ss >> std::hex >> byte;
// store the result in our char array
bytes[i] = byte;
}
//
// do something with the byte array here
//
// deallocate array before exiting
delete[] bytes;
}
Please note that this assumes that the initial string always contains an even number of characters. Some extra code would be needed for handling input strings of an odd size.

Probably fast solution I can't offer to you, but you may try this:
std::string str ="8C000002008E"
std::vector<unsigned char> vect;
vect.resize(str.length() / 2);
for (size_t i = 0; i < str.length(); ++i)
{
vect[i] = str[2*i] - ('0' - 7 * (str[2*i] >= '9')); //it is possible to optimize this
vect[i] <<= 4;
vect[i] = str[2*i+1] - ('0' - 7 * (str[2*i+1] >= '9')); //it is possible to optimize this
}
Didn't tried that, but should work, however, code is really not optimized, I have left this part for yourself.
Also this assumes that the initial string always contains an even number of characters.

Related

Problem to read from PIPE in posix Linux when the content size is bigger than 65536

Why I can't strings contents from file descriptor bigger than 65536 with my code?
void SetFdAsync(int fd) {
int flags;
if (-1 == (flags = fcntl(fd, F_GETFL, 0))) flags = 0;
fcntl(fd, F_SETFL, flags | O_NONBLOCK);
}
std::tuple<std::string, std::string> ReadPipe(int pipe_out, int pipe_err) {
char buf[PIPE_BUF];
char buf_err[PIPE_BUF];
int rd = 0;
int rd_err = 0;
SetFdAsync(pipe_out);
SetFdAsync(pipe_err);
std::string str_out;
std::string str_err;
int i = 0;
while ((rd = read(pipe_out, buf, PIPE_BUF)) > 0) {
std::cout << "loop: " << i++ << std::endl;
str_out.append(buf, rd);
}
while ((rd_err = read(pipe_err, buf_err, PIPE_BUF)) > 0) {
str_err.append(buf_err, rd_err);
}
return std::tuple<std::string, std::string>(str_out, str_err);
}
Why I can't read contents when the length is bigger than 65536? What must I change?

How to use bitwise manipulation to decode machine language into mips assembly? Using c++ to code add, nand, lw, sw, beq, halt

I have written the code that reads the file with machine language, saved the values into an array for memory. I am iterating through the array and for each index, I am using that number to manipulate the bits to do what the binary representation is telling it to do. The only problem is I am not sure how to do this?
I tried storing in separate variables and adding later. I want to do this by shifting the bits to the left and right but then I am not sure how I would store the bits/value. Im not sure how to get this done tbh.
int main (int argc, char *argv[]) {
char line[MAXLINELENGTH];
stateType state;
for (int i = 0; i < NUMREGS; i++)
{
state.reg[i] = 0;
}
FILE *filePtr;
if (argc != 2) {
printf("error: usage: %s <machine-code file>\n", argv[0]);
exit(1);
}
filePtr = fopen(argv[1], "r");
if (filePtr == NULL) {
printf("error: can't open file %s", argv[1]);
perror("fopen");
exit(1);
}
/* read in the entire machine-code file into memory */
for (state.numMemory = 0; fgets(line, MAXLINELENGTH, filePtr) != NULL;
state.numMemory++) {
if (sscanf(line, "%d", state.mem+state.numMemory) != 1) {
printf("error in reading address %d\n", state.numMemory);
exit(1);
}
printf("memory[%d]=%d\n", state.numMemory, state.mem[state.numMemory]);
}
printState(&state);
for (int i = 0; i < state.numMemory + 1; i++ ) {
int opcode = (state.mem[i] >> 22);
if (opcode == 0) {
int regA = (state.mem[i] >> 19) - opcode;
int regB = (state.mem[i] >> 16) - opcode;
int destReg = regA + regB;
}
else if (opcode == 1) {
int regA = (state.mem[i] >> 19) - opcode;
int regB = (state.mem[i] >> 16) - opcode;
int destReg = !(regA && regB);
}

direct access to HDD

I want to print out boot sector using code below, but there is mistake.
#include <windows.h>
#include <stdio.h>
#include <iostream>
#include <math.h>
using namespace std;
short ReadSect
(const char * _dsk, // disk to access
char *&_buff, // buffer where sector will be stored
unsigned int _nsect // sector number, starting with 0
)
{
DWORD dwRead;
HANDLE
hDisk=CreateFile(_dsk,GENERIC_READ,FILE_SHARE_READ,0,OPEN_EXISTING,0,0);
if(hDisk==INVALID_HANDLE_VALUE) // this may happen if another program is
already reading from disk
{
CloseHandle(hDisk);
return 1;
}
SetFilePointer(hDisk,_nsect*512,0,FILE_BEGIN); // which sector to read
ReadFile(hDisk,_buff,512,&dwRead,0); // read sector
CloseHandle(hDisk);
return 0;
}
int main()
{
char * drv="\\\\.\\C:";
char *dsk=" \\\\.\\PhysicalDrive0";
int sector=0;
int b = 1;
char *buff=new char[512];
ReadSect(dsk,buff,sector);
if((unsigned char)buff[510]==0x55 && (unsigned char)buff[511]==0xaa) cout
<<"Disk is bootable!"<<endl;
else printf("%02hhX\n",(unsigned int)(unsigned char)buff[511]);
printf("\n");
while (b<513)
{
if (b%16==0)
printf(" %02hhX\n",(unsigned int)(unsigned char)buff[b-1]);
else
printf (" %02hhX ",(unsigned int)(unsigned char)buff[b-1]);
b++;
}
getchar();
}
Instead of printing hexadecimal digits of boot sectors, microsoft visual studio prints out stream of "CDs". What is the mistake and how to fix the problem? Can anyone help?
Photo of output
First of all, start it as Administrator
remove space from
char *dsk=" \\\\.\\PhysicalDrive0";
must be
char *dsk="\\\\.\\PhysicalDrive0";
And, if you use char *dsk, make modification in:
hDisk=CreateFile(_dsk,GENERIC_READ,FILE_SHARE_READ,0,OPEN_EXISTING,0,0);
must be:
CreateFileA (......)
#include <windows.h>
#include <stdio.h>
#include <iostream>
#include <math.h>
using namespace std;
short ReadSect
(const char * _dsk, // disk to access
char *&_buff, // buffer where sector will be stored
unsigned int _nsect // sector number, starting with 0
)
{
DWORD dwRead;
HANDLE
hDisk = CreateFileA( _dsk, GENERIC_READ, FILE_SHARE_READ, NULL, OPEN_EXISTING, 0, 0);
//CreateFile()
if (hDisk == INVALID_HANDLE_VALUE) // this may happen if another program is already reading from disk
{
CloseHandle(hDisk);
return 1;
}
SetFilePointer(hDisk, _nsect * 512, 0, FILE_BEGIN); // which sector to read
ReadFile(hDisk, _buff, 512, &dwRead, 0); // read sector
CloseHandle(hDisk);
return 0;
}
int main()
{
char * drv = "\\\\.\\C:";
char *dsk = "\\\\.\\PhysicalDrive0";
int sector = 0;
int b = 1;
char *buff = new char[512];
ReadSect(dsk, buff, sector);
if ((unsigned char)buff[510] == 0x55 && (unsigned char)buff[511] == 0xaa) cout
<< "Disk is bootable!" << endl;
else printf("%02hhX\n", (unsigned int)(unsigned char)buff[511]);
printf("\n");
while (b<513)
{
if (b % 16 == 0)
printf(" %02hhX\n", (unsigned int)(unsigned char)buff[b - 1]);
else
printf(" %02hhX ", (unsigned int)(unsigned char)buff[b - 1]);
b++;
}
getchar();
}
WinAPI Unicode and ANSI functions

Why is MD5Sum so fast

I've been studying hashing in C/C++ and tried to replicate the md5sum command in Linux. After analysing the source code, it seems that md5sum relies on the md5 library's md5_stream. I've approximated the md5_stream function from the md5.h library into the code below, and it runs in ~13-14 seconds. I've tried to call the md5_stream function directly and got ~13-14 seconds. The md5sum runs in 4 seconds. What have the GNU people done to get the speed out of the code?
The md5.h/md5.c code is available in the CoreUtils source code.
#include <QtCore/QCoreApplication>
#include <QtCore/QDebug>
#include <iostream>
#include <iomanip>
#include <fstream>
#include "md5.h"
#define BLOCKSIZE 32784
int main()
{
FILE *fpinput, *fpoutput;
if ((fpinput = fopen("/dev/sdb", "rb")) == 0) {
throw std::runtime_error("input file doesn't exist");
}
struct md5_ctx ctx;
size_t sum;
char *buffer = (char*)malloc (BLOCKSIZE + 72);
unsigned char *resblock = (unsigned char*)malloc (16);
if (!buffer)
return 1;
md5_init_ctx (&ctx);
size_t n;
sum = 0;
while (!ferror(fpinput) && !feof(fpinput)) {
n = fread (buffer + sum, 1, BLOCKSIZE - sum, fpinput);
if (n == 0){
break;
}
sum += n;
if (sum == BLOCKSIZE) {
md5_process_block (buffer, BLOCKSIZE, &ctx);
sum = 0;
}
}
if (n == 0 && ferror (fpinput)) {
free (buffer);
return 1;
}
/* Process any remaining bytes. */
if (sum > 0){
md5_process_bytes (buffer, sum, &ctx);
}
/* Construct result in desired memory. */
md5_finish_ctx (&ctx, resblock);
free (buffer);
for (int x = 0; x < 16; ++x){
std::cout << std::setfill('0') << std::setw(2) << std::hex << static_cast<uint16_t>(resblock[x]);
std::cout << " ";
}
std::cout << std::endl;
free(resblock);
return 0;
}
EDIT: Was a default mkspec problem in Fedora 19 64-bit.
fread() is convenient, but don't use fread() if you care about performance. fread() will copy from the OS to a libc buffer, then to your buffer. This extra copying cost CPU cycles and cache.
For better performance use open() then read() to avoid the extra copy. Make sure your read() calls are multiples of the block size, but lower than your CPU cache size.
For best performance use mmap() map the disk directly to RAM.
If you try something like the below code, it should go faster.
// compile gcc mmap_md5.c -lgcrypt
#include <sys/mman.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <gcrypt.h>
#include <linux/fs.h> // ioctl
#define handle_error(msg) \
do { perror(msg); exit(EXIT_FAILURE); } while (0)
int main(int argc, char *argv[])
{
char *addr;
int fd;
struct stat sb;
off_t offset, pa_offset;
size_t length;
ssize_t s;
unsigned char digest[16];
char digest_ascii[32+1] = {0,};
int digest_length = gcry_md_get_algo_dlen (GCRY_MD_MD5);
int i;
if (argc < 3 || argc > 4) {
fprintf(stderr, "%s file offset [length]\n", argv[0]);
exit(EXIT_FAILURE);
}
fd = open(argv[1], O_RDONLY);
if (fd == -1)
handle_error("open");
if (fstat(fd, &sb) == -1) /* To obtain file size */
handle_error("fstat");
offset = atoi(argv[2]);
pa_offset = offset & ~(sysconf(_SC_PAGE_SIZE) - 1);
if (sb.st_mode | S_IFBLK ) {
// block device. use ioctl to find length
ioctl(fd, BLKGETSIZE64, &length);
} else {
/* offset for mmap() must be page aligned */
if (offset >= sb.st_size) {
fprintf(stderr, "offset is past end of file size=%zd, offset=%d\n", sb.st_size, (int) offset);
exit(EXIT_FAILURE);
}
if (argc == 4) {
length = atoi(argv[3]);
if (offset + length > sb.st_size)
length = sb.st_size - offset;
/* Canaqt display bytes past end of file */
} else { /* No length arg ==> display to end of file */
length = sb.st_size - offset;
}
}
printf("length= %zd\n", length);
addr = mmap(NULL, length + offset - pa_offset, PROT_READ,
MAP_PRIVATE, fd, pa_offset);
if (addr == MAP_FAILED)
handle_error("mmap");
gcry_md_hash_buffer(GCRY_MD_MD5, digest, addr + offset - pa_offset, length);
for (i=0; i < digest_length; i++) {
sprintf(digest_ascii+(i*2), "%02x", digest[i]);
}
printf("hash=%s\n", digest_ascii);
exit(EXIT_SUCCESS);
}
It turned out to be an error in the Qt mkspecs regarding an optimization flag not being set properly.

SendARP not writing out to mac array

SendARP is not setting my mac array, so likewise when I try to convert the mac array to BYTE to convert it to human readable, it also gets random characters in it. also the memset does not seem to make MacAddr 0!
std::wstring GetMacAddress(IPAddr destip)
{
DWORD ret;
ULONG MacAddr[2] = {0}; //initialize instead of memset
ULONG PhyAddrLen = 6; /* default to length of six bytes */
unsigned char mac[6];
//memset(MacAddr, 0, sizeof(MacAddr)); //MacAddr doesn't get set to 0!
//Send an arp packet
ret = SendARP(destip , 0, MacAddr , &PhyAddrLen); //MacAddr stays
//Prepare the mac address
if (ret == NO_ERROR)
{
BYTE *bMacAddr = (BYTE *) & MacAddr;
if(PhyAddrLen)
{
for (int i = 0; i < (int) PhyAddrLen; i++)
{
mac[i] = (char)bMacAddr[i];
}
}
}
}
I have tried numerous ways to get MacAddr to get set by the SendARP function, but it doesn't seem to work and it doesn't return an error.
Casting to char does not convert to a textual representation. If you want to convert to a textual representation one option is to use std::wstringstream
#include <sstream>
#include <string>
#include <iomanip>
std::wstring GetMacAddress(IPAddr destip)
{
// ... snip ...
std::wstringstream out;
for (int i = 0; i < (int) PhyAddrLen; i++)
{
out << std::setw(2) << std::setfill(L'0') << bMacAddr[i];
}
return out.str();
}
Try this:
static const wchar_t *HexChars = L"0123456789ABCDEF";
std::wstring GetMacAddress(IPAddr destip)
{
DWORD ret;
BYTE MacAddr[sizeof(ULONG)*2];
ULONG PhyAddrLen = sizeof(MacAddr);
std::wstring MacAddrStr;
ret = SendARP(destip, 0, (PULONG)MacAddr, &PhyAddrLen);
if ((ret == NO_ERROR) && (PhyAddrLen != 0))
{
MacAddrStr.resize((PhyAddrLen * 2) + (PhyAddrLen-1));
MacAddrStr[0] = HexChars[(MacAddr[0] & 0xF0) >> 4];
MacAddrStr[1] = HexChars[MacAddr[0] & 0x0F];
for (ULONG i = 1, j = 2; i < PhyAddrLen; ++i, j += 3)
{
MacAddrStr[j+0] = L':';
MacAddrStr[j+1] = HexChars[(MacAddr[i] & 0xF0) >> 4];
MacAddrStr[j+2] = HexChars[MacAddr[i] & 0x0F];
}
}
return MacAddrStr;
}