How to convert '\\x41' to '\x41' in c? - c

Probably the question has already been answered, but unfortunately many many attempts didn't work for me :(
Precisely said, let's assume I have:
char buf[] = "\\x41\\x41\\x41\\x41"
Basically I want to convert it into
char con[] = "\x41\x41\x41\x41"
I tried by splitting the buf into arrays like:
buf1[]="41",buf2[]="41", buf3[]="41", buf4[]="41"
char newbuf[30];
sprintf(newbuf, "%2x%2x%2x%2x", buf1,buf2,buf3,buf4);
printf("%s:%llx:%p:%d",newbuf,newbuf,newbuf, strlen(newbuf))
and the output I get is: ffffe3f0ffffe410ffffe430ffffe450ffffe470ffffe490:7fffffffe3b0:0x7fffffffe3b0:48
But the output I wish to see is AAAA
So is there something I am missing or doing wrong?

Try this:
#include <stdio.h>
#include <string.h>
#include <ctype.h>
void parseCstring(const char buf[], char con[])
{
int i=0, j=0;
unsigned char x;
while ( (x = buf[i]) != '\0')
{
if ( (x=='\\') && (buf[i+1]=='x') && (isxdigit(buf[i+2])) && (isxdigit(buf[i+3])) )
{
unsigned int val;
sscanf(&(buf[i+2]),"%2x", &val);
x=(unsigned char)val;
i+=3;
}
con[j++]=x;
i++;
}
}
int main()
{
char buf[] = "\\x41\\x42\\x42\\x41 was great\\x21";
char con[40];
printf("%s\n",buf);
parseCstring(buf, con);
printf("%s\n",con);
return 0;
}

Related

Implementing string to int function in c [duplicate]

This question already has answers here:
returning a local variable from function in C [duplicate]
(4 answers)
Changing address contained by pointer using function
(5 answers)
Closed 1 year ago.
I am trying to implement a function as stated in the title. I think I am very close to solution but a problem.
input: 51% are admitted.
output: x:51 (null)
but output should have been:
s:% are admitted.
My try is here:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
int str2int(int);
int isdigit(int);
long str2double(int);
int driver(char *, char *);
int main(){
char *s = "51% are admitted.";
char *sPtr;
int x = driver(s, sPtr);
printf("x:%d sPtr:%s", x, sPtr);
return 0;
}
int isdigit(int ch){
return (ch>=48 && ch<=57)?1:0;
}
int str2int(int ch){
return ch-48;
}
int driver(char *s, char *sPtr){
int i=0, number=0;
while(s[i]!='\0' && isdigit(s[i])){
number = number*10 + str2int(s[i]);
i++;
}
sPtr=s+i;
printf("%s\n", sPtr);
return number;
}
The problem is, in main, sPtr seems as null but in driver function, sPtr is % is admitted which is what it should be. How can I fix the problem so that I can print the solution correctly without using a printf statement in driver function?
EDIT:
The problem is as #Johnny Mopp said, I was trying to pass a copy of that variable. Therefore, I need to pass the address of variable of *sPtr which appears char **sPtr in prototype. And the code should be:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
int str2int(int);
int isdigit(int);
long str2double(int);
int driver(char *, char **);
int main(){
char *s = "51% are admitted.";
char **sPtr;
int x = driver(s, &sPtr);
printf("x:%d sPtr:%s", x, sPtr);
return 0;
}
int isdigit(int ch){
return (ch>=48 && ch<=57)?1:0;
}
int str2int(int ch){
return ch-48;
}
int driver(char *s, char **sPtr){
int i=0, number=0;
while(s[i]!='\0' && isdigit(s[i])){
number = number*10 + str2int(s[i]);
i++;
}
*sPtr=s+i;
return number;
}
Thanks for contributes of #Johnny Mopp and #paulsm4

How to convert array of char into array of int?

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "stats.h"
/* Size of the Data Set */
#define SIZE (40)
void print_array (unsigned char *p, int l) {
int i;
for (i=0;i<l;i++) {
printf("%d\t",*p);
p++;
}
}
void print_array_int (int *p, int l) {
int i;
for (i=0;i<l;i++) {
printf("%d\t",*p);
p++;
}
}
void typecasting(unsigned char test[SIZE], int array[SIZE]) {
int i=0;
unsigned char *token = strtok(test,",");
while (token) {
if(i<SIZE) {
array[i++] = atoi(token);
}
token = strtok(NULL,",");
}
}
void main() {
int array[SIZE] = {};
unsigned char test[SIZE] = {34,201,190,154,8,194,2,6,114,88,45,76,123,87,25,23,200,122,150,90,92,87,177,244,201,6,12,60,8,2,5,67,7,87,250,230,99,3,100,90};
/* Other Variable Declarations Go Here */
/* Statistics and Printing Functions Go Here */
print_array(test, SIZE);
typecasting(test,array);
print_array_int(array,SIZE);
}
What I want in this code is to convert the array of char into an array of int.
Previously I tried doing this by using pointers but didn't work and it showed stack smashing error. I want to convert this array of char into array of int to perform some mathematical operations.
You are trying too hard. Here's how typecasting should look
void typecasting(unsigned char test[SIZE], int array[SIZE]) {
for (int i = 0; i < SIZE; ++i)
array[i] = test[i];
}
Your code might be suitable if you were converting from a C string, i.e. if your original test array was
char test[] = "34,201,190,154,8,194,2,6,114,88,45,76,123,87,25,23,...";
So I guess you could say you're misunderstanding the nature of char (and unsigned char) in C++. They can represent character data as in char greeting[] = "hello"; or they can represent small integers as in char test[] = {1,2,3};.

Segmentation fault (core dumped) error for C program

I am trying to run below program in an online C compiler. But I get segmentation error. Can you help me fix this
#include <stdio.h>
#include <string.h>
int main()
{
char string[15] = "Strlwr in C";
printf("%s",tolower(string));
return 0;
}
Following is the prototype of tolower
int tolower(int c);
You should pass an int or something like char which can safely convert to int. Passing char * (Type of string) like you do leads to UB.
To convert a string to lowercase, you need to convert each character separately. One way to do this is:
char string[15] = "Strlwr in C";
char lstr[15];
int i = 0;
do {
lstr[i] = tolower(string[i]);
} while(lstr[i] != '\0');
printf("%s", lstr);
You are using tolower incorrectly. This function returns int and gets int as a parameter (here is it's declaration: int tolower(int c);). What you want to do is call it on each char of your char array, and print each one:
char string[15] = "Strlwr in C";
for(int i = 0; i < strlen(string); i++)
printf("%c",tolower(string[i]));
Read cplusplus.com/reference/cctype/tolower It takes a single int as parameter, not char and not array.
You probably want to use a loop on "string", which processes each in turn.
#include <stdio.h>
#include <string.h>
#include <ctype.h>
int main(void)
{
int i;
char string[15] = "Strlwr in C";
for (i=0; i< sizeof(string)/sizeof(char); i++)
{
string[i]=(char)(tolower((int)string[i]));
}
printf("%s\n",string);
return 0;
}
Output:
strlwr in c

Getting wrong answer when counting array in c

#include <stdio.h>
#include <stdlib.h>
int countArrayChars(char *strArray[]){
int i=0;
while (strArray[i] != '\0'){
i++;
}
printf("%d\n", i);
return i;
}
int main(int argc, const char * argv[]) {
char *dog[] = {"dog"};
countArrayChars(dog);
For some reason, it prints "5".
Shouldn't it print 3?
I even tried to put \0 after the "g".
You declare array of string and initialize it with dog.
char *dog[] = {"dog"};
Actually it represented as
dog[0] = "Dog"; //In your case only element index with 0.
...............
...............
dog[n] = "tiger"; //If there Have n+1 element
Hence your array size is 1. Which hold constant string dog. To access it you should use dog[0].
So without less modification you can use your code as:
int countArrayChars(char *strArray[])
{
int i=0;
while (strArray[0][i] != '\0')
{
i++;
}
printf("%d\n", i);
return i;
}
int main(int argc, const char * argv[])
{
char *dog[] = {"dog"};
countArrayChars(dog);
}
Or if you want to declare a string use
char *dog = "dog";
or
char dog[] = "dog";
Please try this
#include <stdio.h>
#include <stdlib.h>
int countArrayChars(char *strArray){
int i=0;
while (strArray[i] != '\0'){
i++;
}
printf("%d\n", i);
return i;
}
int main(int argc, const char * argv[]) {
char *dog[] = "dog";
countArrayChars(dog);
}

Wrong output in my stringreverse program. what is fault in my code?

#include<stdio.h>
#include<conio.h>
#include<string.h>
char* strreverse(char*);
int main()
{
char *rev_string;
char *name="computer";
clrscr();
rev_string=strreverse(name);
printf("%s", rev_string);
getch();
return 0;
}
char* strreverse(char *name)
{
int length=strlen(name);
char *ptr;
char *rstr;
for(ptr=name+(length-1);ptr>=name;ptr--)
{
*rstr=*ptr;
printf("%c",rstr);
rstr++;
}
*(rstr)=NULL;
return rstr;
}
the above is my code. i tried to write a program for string reverse without using arrays. But i am not getting the output retupmoc. what is wrong in my code? how to insert null char in char*?
#include <stdio.h>
#include <conio.h>
#include <string.h>
#include <stdlib.h>
char* strreverse(const char*);
int main(){
char *rev_string;
char *name="computer";
clrscr();
rev_string=strreverse(name);
printf("%s\n", rev_string);
free(rev_string);
getch();
return 0;
}
char* strreverse(const char *name){
int length=strlen(name);
const char *ptr;
char *ret, *rstr = malloc(length + 1);
if(ret=rstr){
for(ptr=name+length;ptr != name;){
*rstr++ = *--ptr;
}
*rstr = '\0';
}
return ret;
}
You did not allocate memory to hold your reversed string. Try
char *rstr = calloc(1, length+1);
Also it should be
printf("%c", *rstr); // dereference
*(rstr)= '\0'; // instead of NULL
Here you find sweet and short solution for string reverse:
#include<stdio.h>
#include<string.h>
int strreverse(char* , char*);
int main()
{
char rev_string[10] = {0};
char name[10]="computer";
strreverse(name, rev_string);
printf("%s\n", rev_string);
return 0;
}
int strreverse(char *name, char *rStr)
{
int i = 0;
int length = strlen(name);
while(i < length)
{
rStr[i] = name[length-i-1];
i++;
}
return 0;
}
Try to run and have fun.

Resources