Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 3 years ago.
Improve this question
Suppose I have a piece of code like
unsigned char *tag = NULL;
tag = (unsigned char *)malloc(8);
memset(tag, 0, 8);
memcpy(tag, (const char *)"50C59390",8);
I have to send it as length 4. So I am trying to convert it in 4 byte hex like 0x50C59390.
unsigned char * buffer = (unsigned char *)calloc(4, sizeof(char));
int index,j = 0;
for(index = 0 ; index < 8; index++)
{
buffer[j] = (tag[index] & 0x0F) | (tag[++index]>>4 & 0xF0);
printf("%02X", buffer[j]);
j++;
}
I am trying above code. but its not working as required.
You can't just copy ascii characters as hex values. You need to convert them.
Something like:
unsigned char convert(unsigned char ch)
{
if (ch >= '0' && ch <= '9')
{
return ch -'0';
}
if (ch >= 'a' && ch <= 'f')
{
return ch -'a' + 10;
}
if (ch >= 'A' && ch <= 'F')
{
return ch -'A' + 10;
}
return 0; // or some error handling
}
and use it like:
for(index = 0 ; index < 8; index = index + 2)
{
buffer[j] = convert(tag[index]) << 4;
buffer[j] += convert(tag[index+1]);
printf("%02X", buffer[j]);
j++;
}
See it online here: https://ideone.com/e2FPCT
From your question I am not exactly sure whether you try to convert to or from a string. However, if you have C++11 available, you can use std::stoi() and std::to_string() for easy conversion:
int hex_value = std::stoi(tag, nullptr, 16)
Note how the third argument of std::stoi() denominates the base (in this case 16 for hexadecimal).
Related
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);
Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 4 years ago.
Improve this question
I'm trying to return a string from the function solution() but I am getting the error below. Apologies if this is pretty basic but could anybody explain how to return the string. I understand that it is related to pointers.
error: could not convert ‘(std::__cxx11::string*)(& hexaDeciNum)’ from
‘std::__cxx11::string* {aka std::__cxx11::basic_string*}’ to
‘std::__cxx11::string {aka std::__cxx11::basic_string}’
string solution(string &S){
int n = stoi(S);
int answer = 0;
// char array to store hexadecimal number
string hexaDeciNum[100];
// counter for hexadecimal number array
int i = 0;
while(n!=0)
{
// temporary variable to store remainder
int temp = 0;
// storing remainder in temp variable.
temp = n % 16;
// check if temp < 10
if(temp < 10)
{
hexaDeciNum[i] = temp + 48;
i++;
}
else
{
hexaDeciNum[i] = temp + 55;
i++;
}
n = n/16;
}
// printing hexadecimal number array in reverse order
for(int j=i-1; j>=0; j--){
//cout << hexaDeciNum[j] << "\n";
if (hexaDeciNum[j].compare("A") ==0 or hexaDeciNum[j].compare("B") ==0 or hexaDeciNum[j].compare("C") ==0 or hexaDeciNum[j].compare("D") ==0 or hexaDeciNum[j].compare("E") ==0 or hexaDeciNum[j].compare("F") ==0 or hexaDeciNum[j].compare("1") ==0 or hexaDeciNum[j].compare("0") ==0 ) {
answer = 1;
}
}
if (answer == 1){
return hexaDeciNum;
}
else {
return "ERROR";
}
}
int main() {
string word = "257";
string answer = solution(word);
return 0;
}
hexaDeciNum is defined as string hexaDeciNum[100]. It is not a string - it is an array of 100 string instances.
You're attempting to return it from a function that should return string.
You should define hexaDeciNum as string hexaDeciNum; instead of string hexaDeciNum[100];. With that way, you can still indexing operator. However you can not use compare method anymore, because each element of string is a char. Instead use operator == like in the following for your piece of code.
// printing hexadecimal number array in reverse order
for(int j=i-1; j>=0; j--){
//cout << hexaDeciNum[j] << "\n";
if (hexaDeciNum[j] == 'A' or hexaDeciNum[j]=='B' or
hexaDeciNum[j] == 'C' or hexaDeciNum[j] == 'D' or
hexaDeciNum[j] == 'E' or hexaDeciNum[j] == 'F' or
hexaDeciNum[j] == '1' or hexaDeciNum[j] == '0' ) {
answer = 1;
}
}
and please don't forget to compile it for c++ 11 with -std=c++11 option of compiler.
Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 6 years ago.
Improve this question
I wrote this program for school, however I keep getting C++ related errors (apparently)
#include <stdio.h>
#define int NUM_OF_CHARS 51
void switch (char *c)
{
//Little Letters
if (((*c)>=97) && ((*c)<=122))
(*c)-=32;
//Capital Letters
if ((c>=65) && (c<=90))
(*c)+=32;
//*c>=5
if ((c>=53) && (c<=57))
(*c)=56;
//*c<5
if ((c>=48) && (c<=52))
(*c)=48;
}*/
int main() {
char string[51];
printf("PLease Enter a String \n");
scanf("%s", string);
printf("%s => ", string);
int i=0;
char s[51];
while((string[i]!= "\0") && (i < NUM_OF_CHARS))
{
s[i]=switch (string[i]);
i++;
}
printf("%s", s);
return 0;*/
}
I'm getting errors like /stray xxx in program and macro names must be identified.
I'm kind of new to C so I'd appreciate if you could point me out to what the errors in this code are. Thanks!!
#define int NUM_OF_CHARS 51
replace it with
#define NUM_OF_CHARS 51
also, you have used
void switch (char *c)
since switch is a keyword, you cannot use it as a function name.
There are many issues. You probably want the program below. The code compiles and works but it is still absolutly horrible, but it respects your intention.
Try to make it better.
BTW the characters contained in the string variable are also changed, is this intended ?
#include <stdio.h>
#define NUM_OF_CHARS 51 // removed "int"
char switchcase (char *c) // << we need to return a char not void
{ // << name changed to switchcase
// all c changed to (*c), BTW: *c without () would be OK too
//Little Letters
if (((*c) >= 97) && ((*c) <= 122))
(*c) -= 32;
//Capital Letters
else if (((*c) >= 65) && ((*c) <= 90))
(*c) += 32;
//*c>=5
else if (((*c) >= 53) && ((*c) <= 57))
(*c) = 56;
//*c<5
else if (((*c) >= 48) && ((*c) <= 52))
(*c) = 48;
return *c;
}
int main() {
char string[51];
printf("PLease Enter a String \n");
scanf("%s", string);
printf("%s => ", string);
int i = 0;
char s[51];
while ((string[i] != '\0') && (i < NUM_OF_CHARS))
{
s[i] = switchcase(&string[i]);
i++; //^ & was missing here
}
s[i] = '\0'; // << you forgot the zero terminator
printf("%s", s);
return 0; // << removed stray "*/"
}
switch is a reserved word and cannot be used as a function name.
Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 4 years ago.
Improve this question
I wanted to write a C++ program that checks whether a string contains every letter in the alphabet from a to z.
bool allLetters(string s) {
vector<bool> checked(255, false);
for (int i=0; i < int(s.size()); ++i)
checked[s[i]] = true;
for (int i='a'; i <= 'z'; ++i)
if (!checked[i])
return false;
return true;
}
Assuming a character set where the letters are contiguous from a to z:
bool contains_all_letters(const char * p)
{
unsigned seen = 0;
while (*p)
{
unsigned c = *p++ - 'a';
if (c < 26)
{
seen |= (1 << c);
}
}
return seen == (1 << 26) - 1;
}
You can always do this (in ASCII)
bool has_alphabet(std::string const &s)
{
std::bitset<26> alphabet;
for (char c : s) {
if (c >= 'A' && c <= 'Z')
alphabet.set(c - 'A');
else if (c >= 'a' && c <= 'z')
alphabet.set(c - 'a');
}
return 26 == alphabet.count();
}
You need to include the bitset header and no matter whether caps are before or after lower letters or they have other symbols between them, this will work
I have a MAC address like "6F:e:5B:7C:b:a" that I want to parse and insert the implicit zeros before the :e:, :b:, :a.
I cannot use Boost at the moment but I have a rough solution. The solution splits on ':'. Then I count the characters between and if there is only one I insert a zero at the front.
I was wondering if anyone had a faster approach?
For the quick and dirty:
if (sscanf(text, "%x:%x:%x:%x:%x:%x",
&mac[0], &mac[1], &mac[2], &mac[3], &mac[4], &mac[5]) != 6) {
// handle error
}
Note that it does not check if numbers are really hex. Usual precautions of sscanf() applies.
First of all you could use script that would convert char to int quite fast, so:
unsigned char hex_to_int(const char c)
{
if( c >= 'a' && c <= 'f'){
return c - 'a' + 10;
}
if( c >= 'A' && c <= 'F'){
return c - 'A' + 10;
}
if( c >= '0' && c <= '9'){
return c - '0';
}
return 0;
}
Then you may create loop that will iterate over the string:
unsigned char mac[6]; /* Resulting mac */
int i; /* Iteration number */
char *buffer; /* Text input - will be changed! */
unsigned char tmp; /* Iteration variable */
for( i = 0; i < 6; ++i){
mac[i] = 0;
/*
* Next separator or end of string
* You may also want to limit this loop to just 2 iterations
*/
while( ((*buffer) != '\0') && ((*buffer) != ':'){
mac[i] <<= 4;
mac[i] |= hex_to_int( *buffer);
++buffer;
}
}
if( (i != 6) || (*buffer != NULL)){
// Error in parsing, failed to get to the 6th iteration
// or having trailing characters at the end of MAC
}
This function doesn't do any error checking, but it's probably the fastest solution you'll be getting.