Hi everyone o/

How can I concatenate a no print char from the ASCII on my string?

For example:
I have the str1 = "Jhon" and str2 = "Lucas"

I wanna concatenate the special char 01 ( SOH) between the str1 and str2.


can anyone help me?

I hope so..!!

Thanks.

Look up the escape characters for C. You have the option of an octal escape character and a hexadecimal escape character.

Cool man!!

Thanks.. but if my string was like that: "35:Jhon\135:Lucas"

The escape does not works well because the 35.

What I do?

help me!

I tried like you said.

Look how i did:

char *clients = "35:Jhon\x00135:Lucas";

Each client needs to start with the index 35 and I need separate them with the special char...

See? I need add the special char between the clients, and does not works because of the 35, the compiler thrown an error : "escape sequence out of range"

and now?

please... help me :)

I tried like you said.

Look how i did:

char *clients = "35:Jhon\x00135:Lucas";

Each client needs to start with the index 35 and I need separate them with the special char...

See? I need add the special char between the clients, and does not works because of the 35, the compiler thrown an error : "escape sequence out of range"

and now?

please... help me :)

try octal notation

char *clients = "35:Jhon\00135:Lucas";

Try running this program. What was the result

#include <stdio.h>
#include <stdlib.h>
#include <string.h>

char *ch = "this is the 34\00134\n";

int main(int argc, char**argv)
{
	int i = 0;

	for (i = 0; i < strlen(ch); ++i)
		fprintf(stdout, "ans->%x\n", ch[i]);
	exit(EXIT_SUCCESS);
}

Hi everyone o/

How can I concatenate a no print char from the ASCII on my string?

For example:
I have the str1 = "Jhon" and str2 = "Lucas"

I wanna concatenate the special char 01 ( SOH) between the str1 and str2.


can anyone help me?

I hope so..!!

Thanks.

option 1: (If str1 & str2 are pointers to constant strings)

function char* insertSOH(LPCTSTR str1, LPCTSTR str2)
{
  char* pszRet = (char*)alloc(strlen(str1)+strlen(str2)+2);
  sprintf(pszRet, "%s\x01%s", str1, str2);
  return pszRet;
}

You can also rewrite this function so that it asks for a pointer to a string buffer where the function will store its result (assuming the buffer is large enough to accommodate the output).

function void insertSOH(LPCTSTR str1, LPCTSTR str2, char* pszOut)
{
  sprintf(pszOut, "%s\x01%s", str1, str2);
}

option 2: If your str1 is a pointer to writable string and is where the output will be stored.

strcat(str1, "\x01");
strcat(str1, str2);

/******************************************/

You can personally mail me for some c/c++ questions at <<Email Snipped>>

/******************************************/

I don't understand. First you say

For example:
I have the str1 = "Jhon" and str2 = "Lucas"

Then you say

but if my string was like that: "35:Jhon\135:Lucas"

So which is it?

The escape does not works well because the 35.

So what's wrong with the 35?

What I do?

Depends on what the problem is.

Hi gerard4143, i tried with your source and did not work too.

Kurt Kubing, i tried with your source and seems works, i will do more test to see the results.

Hey WaltP, yeah.. first I said one thing and after another one. I'll try explain..

I need create a string that contents all my clients, and the values are separate by a special char ( we choose SOH ), the values as name, address, phone, etc are grouped by index ( 35=name, 95=address, 105=phone, etc) and then when I'll separate the index for another the special char (SOH) does not work because it needs to be together and not separate with space.

Hi gerard4143, i tried with your source and did not work too.

Can I ask...What are you using for a compiler.

gcc on Debian Lenny

gcc on Debian Lenny

Well the code I gave you was compiled on GCC/Mandriva and it ran without a hitch.

#include <stdio.h>
#include <stdlib.h>
#include <string.h>

char *ch = "this is the 34\00134\n";

int main(int argc, char**argv)
{
	int i = 0;

	for (i = 0; i < strlen(ch); ++i)
		fprintf(stdout, "ans->%x\n", ch[i]);
	exit(EXIT_SUCCESS);
}

your telling me when you ran this you got a different output then below:

ans->74
ans->68
ans->69
ans->73
ans->20
ans->69
ans->73
ans->20
ans->74
ans->68
ans->65
ans->20
ans->33
ans->34
ans->1
ans->33
ans->34
ans->a

sorry man...

my mistake.. when i ran your code i did not see the 1.

Your code ran well..

thanks man.. and sorry again...

I'm not really sure if I really understood what you're trying to do, I'll just make assumptions.

I have created a program that is supposed to replace, or to be an alternative to ,the start menu and/or the quick launch bar. In my program I gave the user the option to add/remove items just like you want to add shortcuts in the start menu. That option, of course, made me write a code to save the user-added items and I expect the user would add not just one item but many items.

Each item has this information: (1) item title (a string), (2) the path (string), (3) icon file (string), (4) icon index (int), and, (5) the manner to show the window, the nShowCmd in ShowWindow API (an int).

I concatenated all of the information in a single string then saved it in the registry.

The following is the way how I concatenated the information (I'll use your SOH in this case):

char* pszData = (char*)LocalAlloc(LPTR, lstrlen(szTitle)+1+lstrlen(szPath)+1+lstrlen(szIconPath)+1+lstrlen(szIconIndex)+1+strlen(szShowCmd)+2);
wsprintf(pszData, "%s\x01%s\x01%s\x01%i\x01%i", szTitle, szPath, szIconPath, itoa(szIconIndex), itoa(szShowCmd));

And this is how is extracted the data in the concatenated string:

char szTitle[100];
char szPath[MAX_PATH];
char szIconFile[MAX_PATH];
int iconIndex, nShowCmd;
char* p = strtok(pszData, "\x01");
lstrcpy(szTitle, p);
p = strtok(NULL, "\x01");
lstrcpy(szPath, p);
p = strtok(NULL, "\x01");
lstrcpy(szIconFile, p);
p = strtok(NULL, "\x01");
iconIndex = itoa(p);
p += strlen(p)+1;
nShowCmd = itoa(p);

Hey Man.. sorry for not response you.. i had a family problem and i could not see the forum for a while.

Thanks for your code.. I will improve my code with your example.

Thanks for everyone!

See you guys around.! o/

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.