# The method of converting string to byte, hexadecimal number to decimal and exporting data to excel format in C + +

In a recent experiment, I need to collect sensor data. My method is to save the data to TXT file through serial port assistant, and then write a processing program for corresponding data processing operations. There are two main problems: string to byte type and hexadecimal to decimal. After the problem is solved through query, make a record here.

catalogue

1. The problem of converting string to byte in C + +

2. Hex to decimal

3. Save the C + + result data to excel

# 1. The problem of converting string to byte in C + +

There is no byte type in C + +. Unsigned char is usually used instead of byte. Therefore, for ease of presentation, you can customize a byte type with typedef and unsigned char.

`typedef unsigned char BYTE; `

With byte type, it is the conversion of data type from string to byte. There are many ways to convert. I use the strtoll function, which can succinctly realize the conversion from string to byte.

Its function prototype is as follows:
unsigned long strtoul(const char *nptr,char **endptr,int base )
Parameter 2: returns the end address of the string significant number, which is why the secondary pointer is used.
Parameter 3: conversion cardinality. When base=0, automatically judge the type of string and output it in hexadecimal, such as "0xa", the string will be treated as hexadecimal, and the output is 10. More details are described below.

The specific implementation methods are as follows:

```//Conversion example

#include<iostream>
#include<string>

using namespace std;
typedef unsigned char BYTE;  //Custom byte type

int main(int argc, char** argv)
{
string str = "ff";   //0xff corresponds to decimal 255
BYTE bytes = strtoul(str.c_str(), NULL, 16);
int byte = int(bytes);   //Strong conversion to int verifies that the conversion was successful

cout << "byte=" << byte << endl;

system("pause");
return 0;
}```

The output result is: # 2. Hex to decimal

When operating hexadecimal numbers, we often encounter that numbers such as 33 6A (i.e. 0x33 0x6a) need to be converted into decimal, and the number from 33 6a to decimal is 13162. We take 33 6A as an example to realize its conversion process with code.

The implementation is as follows:

```#include<iostream>
#include<string>

using namespace std;
typedef unsigned char BYTE;

int main(int argc, char** argv)
{
BYTE a = 0x33;
BYTE b = 0x6a;
int Sym = (a & 0x80) >> 7;   //Sign judgment: 1 is negative and 0 is positive
cout << "Symbol is=: " << Sym << endl;
float output;
if (Sym)
{
output = -(((~(a) << 8) & 0xff00) | (~(b) & 0xff) + 0x01) ;
}
else
{
output = ((((a) << 8) & 0xff00) | ((b) & 0xff) ) ;
}

cout << "output=: " << output << endl;
system("pause");
return 0;
}```

The output result is: The above is how to switch from string to byte, and then convert byte hexadecimal data to decimal data.

# 3. Save the C + + result data to excel

The implementation code is as follows:

```#include<iostream>
#include<string>
#include<fstream>
using namespace std;

int main(int argc, char** argv)
{
ofstream data;
data.open("data.csv", ios::out | ios::trunc);
data << "full name" << "," << "Age" << "," << "class" << "," << "achievement" << endl;   //Column wrapping, endl line wrapping
data << "Li Ziming" << "," << "18" << "," << "Class two of three years" << "," << "666" << endl;
data.close();

system("pause");
return 0;
}
```

As shown in the figure, the output result csv file is in the project folder and can be directly opened with excel. reference resources:

(35 messages) very convenient c + + string conversion to hexadecimal_ Moonlight shines like water - CSDN blog_ c + + string to hexadecimal

(35 messages) the use of strtoul function has uncovered its mystery_ Anti linen bag people's blog - CSDN blog_ Usage of strtoul function

(35 messages) write excel file in c + + file stream mode_ u010325168 blog - CSDN blog_ Writing c + + data into Excel

Keywords: C++ Back-end

Added by chowwiie on Mon, 10 Jan 2022 06:48:54 +0200