Parsing string to hex value in C++ on Arduino - c++

I have the IR code in form of hexadecimal stored in string (without 0x prefix) which has to be transmited via sendNEC() from IRremote.h. What's the easiest way to convert string in form like "FFFFFF" to 0xFFFFFF?

if you get every char of the string and converted to a numeric hex value then you will need only to calculate the power of every digit, that conversion returns a number which can be represented in several ways (hex in your case)
std::string w("ABCD");
unsigned int res = 0;
for (int i = w.length()-1; i >= 0; i--)
{
unsigned int t = parseCharToHex(w[w.length() - 1 - i]);
std::cout << std::hex << t << std::endl;
res += pow(16, i) * t;
}
std::cout << "res: " << std::dec << res << std::endl;
the function parseCharToHex:
unsigned int parseCharToHex(const char charX)
{
if ('0' <= charX && charX <= '9') return charX - '0';
if ('a' <= charX && charX <= 'f') return 10 + charX - 'a';
if ('A' <= charX && charX <= 'F') return 10 + charX - 'A';
}
pow function is required from the arduino doc here

This is a dirty code which works well under severe limitations:
no error checking needed
string format is exactly known and is F...F'\0'
it is assumed that codes for '0' to '9' and 'A' to 'F' are subsequent and growing
The trick is to use character codes for calculations
_
char * in;
uint64_t out=0;
int counter;
for(counter=0; in[counter]; counter++){
if(in[counter]>='0'&&in[counter]<='9') {
out*=0x10;
out+=in[counter]-'0';
} else {
//assuming that character is from 'A' to 'F'
out*=0x10;
out+=in[counter]-'A'+10;
}
}

Related

Arduino c++ web form string to string [duplicate]

This question already has answers here:
Encode/Decode URLs in C++ [closed]
(19 answers)
Closed 4 months ago.
I am trying to process instructions from a web page, but special characters are encoded.
Here is an example:
command=setkey
page=1
key=D
value=N%6eE8qiCZ\r
this is received as
"command=setkey&page=1&key=D&value=N%256eE8qiCZ%5Cr"
"%" is converted to "%25", "\" becomes "%5C".
Is there easy way to return "N%256eE8qiCZ%5Cr" to "N%6eE8qiCZ\r" ?
Here is one solution I found with hint "urlEncode" (thanks lorro):
//decode received web form string
unsigned char h2int(char c)
{
if (c >= '0' && c <='9'){
return((unsigned char)c - '0');
}
if (c >= 'a' && c <='f'){
return((unsigned char)c - 'a' + 10);
}
if (c >= 'A' && c <='F'){
return((unsigned char)c - 'A' + 10);
}
return(0);
}
String urldecode(String str)
{
String encodedString="";
char c;
char code0;
char code1;
for (int i =0; i < str.length(); i++){
c=str.charAt(i);
if (c == '+'){
encodedString+=' ';
}else if (c == '%') {
i++;
code0=str.charAt(i);
i++;
code1=str.charAt(i);
c = (h2int(code0) << 4) | h2int(code1);
encodedString+=c;
} else{
encodedString+=c;
}
yield();
}
return encodedString;
}
Called like this:
String webresponse = "command=setkey&page=1&key=D&value=N%256eE8qiCZ%5Cr";
String result = urldecode(webresponse);
Serial.println(result);

hexa-decimal to decimal conversion (using implicit type casting)

I think there's some problem in my vs code I am new to this coding stuff even after writing the correct code it gives me wrong results in almost every second code I write i get uncertain results Plz guys help me with this , plz check running this code in your machine....
#include <iostream>
using namespace std;
int main()
{
char a[30];
cout << "enter the hexadecimal";
cin >> a;
int i = 0, c, digit, decimal = 0, p = 1;
while (a[i] != '\0') {
i++;
}
for (int j = i; j >= 0; j--) {
c = a[j];
if (c >= 48 && c <= 57) {
digit = c - 48;
}
else if (c >= 97 && c <= 112) {
digit = c - 87;
}
decimal += digit * p;
p *= 8;
}
cout << "\ndecimal is " << decimal;
return 0;
}
while entering hexa decimal plz only enter small alphabets i have not taken capital letters into consideration
for cheking hexadecimal to decimal use this site https://www.rapidtables.com/convert/number/hex-to-decimal.html?x=146
There are several problems with the code, but I think that the main one is that you are multiplying p by 8 when it should be 16 (as hex is base-16, not base-8).
You also should take care with invalid inputs. What happens if someone enters an invalid letter 'j' for instance?
Besides, when you calculate the initial length of the string, you are setting ito the position of the array with a '\0' value so when you start processing the input, a[i] is 0 and that leads to using an uninitialized variable (digit has not been assigned a value, this is related to the previous "invalid input" issue).
By the way, I would also use chars in the comparisions instead of ASCII codes, it's easier to see what you are looking for:
if (c >= '0' && c <= '9') {
digit = c - '0';
}
and so on...

C++ - Changing single lowercase charachter in a word to uppercase and vice versa using struct data type

As you can see from the title I want to change lowercase charachter in word to uppercase and vice versa.
Also I need to use struct object (in my case name).
I have a problem when I change charachter from lowercase to uppercase it only changes in the first word not in the second,third and so on. I am also reading words from file
Here is the input file
Aayak Audi
Ahmed Golf7
Samed Golf5
Here is the code
#include <iostream>
#include <fstream>
#include <string>
#include <algorithm>
#include <cctype>
using namespace std;
struct pismaStr
{
string ime;
string objekat;
};
void malaVelikaSlova (string name)
{
for (int i = 0; i < name.length()-1; i++)
{
if (name.at(i) >= 'A' && name.at(i) <= 'Z')
name.at(i) += 32;
else if (name.at(i) >= 'a' && name.at(i) <= 'z')
name.at(i) -= 32;
cout << name;
break;
}
}
int main()
{
ifstream pismo;
pismo.open("pismo.txt");
ofstream novoPismo;
novoPismo.open("novaSlova.txt");
pismaStr stvari[200];
int brojStvari = 0;
while(pismo >> stvari[brojStvari].ime >> stvari[brojStvari].objekat)
{
brojStvari++;
}
for (int i = 0; i < brojStvari; i++)
{
vector <pismaStr> vec = {pismaStr{stvari[i].ime}};
for (auto obj : vec)
{
malaVelikaSlova (obj.ime);
}
}
Here is the output:
aayak
ahmed
samed
It was:
Aayak
ahmed
samed
I want it to look like this
aAYAK
sAMED
aHMED
How can I fix this?
Any tips?
Tangential,
but it will be an issue, is this line
for (int i = 0; i < name.length()-1; i++)
This will loop from name[0] to name[name.length() - 2]. The std::string::length returns the number of usable characters. It does not include the null terminator, so you don't need to subtract 1. It should be
for (int i = 0; i < name.length(); i++)
Your bigger problem
is the break statement at the end of your loop (indentation added for clarity)
for (int i = 0; i < name.length()-1; i++)
{
if (name.at(i) >= 'A' && name.at(i) <= 'Z')
name.at(i) += 32;
else if (name.at(i) >= 'a' && name.at(i) <= 'z')
name.at(i) -= 32;
cout << name;
break; // <--- this exits the loop entirely
}
Your break; tells the program to exit the loop immediately. No further iterations of the loop are performed. Your cout statement is also within the loop. Once you do get the loop running for each iteration, you'll output each step of the transformation. To only output it once (at the end) you put it outside of the loop. If you want to loop over every character (and you do), your final code looks like
void malaVelikaSlova (string name)
{
for (int i = 0; i < name.length() - 1; i++)
{
if (name.at(i) >= 'A' && name.at(i) <= 'Z')
name.at(i) += 32;
else if (name.at(i) >= 'a' && name.at(i) <= 'z')
name.at(i) -= 32;
}
cout << name;
}
Other things you can change
You don't need to do bounds checking on your string indexes, since you're looping based on the string length, and it's not changing, so you don't need to extra overhead of std::string::at. You can just use the index operator:
// name.at(i); // <-- no need to do this anywhere
name[i] // <-- slightly faster
Since you're applying some operation to each element (character) in your container (string), this is a great candidate for std::transform, from the <algorithm> library. I'm also using a lambda expression here, which is a great too from C++ to get familiar with.
https://en.cppreference.com/w/cpp/algorithm/transform
https://en.cppreference.com/w/cpp/language/lambda
void malaVelikaSlova (string name)
{
std::transform(
name.begin(),
name.end(),
[](char c) -> char
{
if (c >= 'A' && c <= 'Z')
return c + 32;
if (c >= 'a' && c <= 'z')
return c - 32;
return c; // <-- for all other characters
}
);
std::cout << name << "\n";
}
You could even take advantage of the std::isupper, std::islower, std::toupper, and std::tolower functions to make your code more explicit. Note that std::string is an alias for std::basic_string<char> (its value type is char), the upper and lower functions operate on unsigned chars, so you'll need to convert the chars to unsigned chars:
https://en.cppreference.com/w/cpp/string/basic_string
https://en.cppreference.com/w/cpp/string/byte/tolower
https://en.cppreference.com/w/cpp/string/byte/toupper
https://en.cppreference.com/w/cpp/string/byte/isupper
https://en.cppreference.com/w/cpp/string/byte/islower
void malaVelikaSlova (string name)
{
std::transform(
name.begin(),
name.end(),
[](unsigned char c) -> unsigned char // <-- must convert to unsigned to be safe with upper/lower functions
{
if std::isupper(c) return std::tolower(c);
if std::islower(c) return std::toupper(c);
return c; // <-- for all other characters
}
);
std::cout << name << "\n";
}

Arrays not outputting correctly - C++

I am writing a C++ console application and I'm turning a into 1, b into 2 and so on. Thing is, it's outputting numbers like 48 and 52 - even though the array I'm basing it off only goes up to 26.
Here's the code:
void calculateOutput() {
while (input[checkedNum] != alphabet[checkedAlpha]) {
checkedAlpha++;
if (checkedAlpha > 27) {
checkedAlpha = 0;
}
}
if (input[checkedNum] == alphabet[checkedAlpha]) {
cout << numbers[checkedAlpha] << "-";
checkedAlpha = 0;
checkedNum++;
calculateOutput();
}
}
Here is my number and alphabet arrays:
char alphabet [27] = { 'a','b','c','d','e','f','g','h','i','j','k','l','m','o','p','q','r','s','t','u','v','w','x','y','z',' '};
int numbers [27] = { '1','2','3','4','5','6','7','8','9','10','11','12','13','14','15','16','17','18','19','20','21','22','23','24','25','26','0' };
Its int array so it means that it will save ASCII values of characters.
If you would look carefully on ASCII table, you would find out that 48,49,50,... are ascii values of numbers 0,1,2,...
What you have to do is deduct value of first number in table -> '0' (48)
cout << numbers[checkedAlpha] - '0' << "-";
or better, save numbers as numbers not characters
int numbers [27] = { 1,2,3,4,5,6,7,8,9,10,11,12,13,14​,15,16,17,18'​,19,20,21,22,23,24,25,26​,0 };
Btw. here is hint which make it easier for you
tolower(inputAlphabet[index]) - 'a' + 1 // For 'a' output is 1 and so on :-)
The algorithm to get the number (i9ndex) of the letters of the alphabet is quite simple. No need for tables, a simple subtraction does the trick.
int getLetterIndex(char c)
{
// returns -1 if c is not a letter
if ('a' <= c && c <= 'z')
return 1 + c - 'a';
if ('A' <= c && c <= 'Z')
return 1 + c - 'A';
return -1;
}

Convert a HEX single literal character to its value

I need to convert a hex literal character to its value. Consider the following:
char hex1 = 'f'; // hex equals 102, as ´f´ is ASCII 102.
char hexvalue = converter(hex1); // I need on hexvalue 0x0F, or 1111 binary
What shall be the most straightfoward converter function here ?
Thanks for helping.
A straight forward converter function would be to use a lookup array:
unsigned int Convert_Char_Digit_To_Hex(char digit)
{
static const std::string char_to_hex[] = "0123456789ABCDEF";
const std::string::size_type posn =
char_to_hex.find(digit);
if (posn != std::string::npos)
{
return posn;
}
return 0; // Error if here.
}
But why write your own when you can use existing functions to convert from textual representation to internal representation?
See also strtol, strtoul, std::istringstream, sscanf.
Edit 1: Comparisons
Another alternative is to use comparisons and math:
unsigned int Hex_Char_Digit_To_Int(char digit)
{
unsigned int value = 0U;
digit = toupper(digit);
if ((digit >= '0') and (digit <= '9'))
{
value = digit - '0';
}
else
{
if ((digit >= 'A') and (digit <= 'F'))
{
value = digit - 'A' + 10;
}
}
return value;
}