473,320 Members | 1,862 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,320 software developers and data experts.

How to concatenate string and unsigned char ?

Hi,

I've got the following ...

char user[10] = "root";
unsigned long session = 0x0012453b;
char result[20];

How can I concatenate user and session to result? Thank you.
Nov 13 '05 #1
5 19752
ab*****@yahoo.com (Abby) writes:
char user[10] = "root";
unsigned long session = 0x0012453b;
char result[20];

How can I concatenate user and session to result? Thank you.


sprintf(result, "%s%lx", user, session);

Be sure to allocate enough space in the result string.
--
"I don't have C&V for that handy, but I've got Dan Pop."
--E. Gibbons
Nov 13 '05 #2
Abby <ab*****@yahoo.com> wrote:
Hi, I've got the following ... char user[10] = "root";
unsigned long session = 0x0012453b;
char result[20];

How can I concatenate user and session to result? Thank you.


Sure, look at 'sprintf'.

Alex
Nov 13 '05 #3
Abby wrote:
Hi,

I've got the following ...

char user[10] = "root";
unsigned long session = 0x0012453b;
char result[20];

How can I concatenate user and session to result? Thank you.


#include <stdio.h>

int main(void)
{
char user[10] = "root";
unsigned long session = 0x0012453b;
char result[20];
sprintf(result, "%s%#.8lx", user, session);
printf("\"%s\" and %#.8lx combined to yield\n"
" \"%s\"\n", user, session, result);
return 0;
}

[output]
"root" and 0x0012453b combined to yield
"root0x0012453b"
--
Martin Ambuhl

Nov 13 '05 #4
Abby wrote:
Ben Pfaff <bl*@cs.stanford.edu> wrote in message news:<87************@pfaff.stanford.edu>...
ab*****@yahoo.com (Abby) writes:
char user[10] = "root";
unsigned long session = 0x0012453b;
char result[20];

How can I concatenate user and session to result? Thank you.


sprintf(result, "%s%lx", user, session);

Be sure to allocate enough space in the result string.


What if I got

unsigned char session[4];
session[0] = 0x00;
session[1] = 0x12;
session[2] = 0x45;
session[3] = 0x3B;

instead of unsigned long session = 0x0012453b

even worse, how about if I have a very long array like session[100],
which I need to concatenate all of them to "result". Do I need to
write the sprintf for all session[0] - session[99] manually?? Please
advise. Thank you so much!!


char s[42] = "root0x", *p = s + strlen(s);

unsigned char session[4];
session[0] = 0x00;
session[1] = 0x12;
session[2] = 0x45;
session[3] = 0x3B;

for (i=0; i<4; i++, p+=2) sprintf(p, "%02x", session[i]);

Jirka

Nov 13 '05 #5
On Mon, 18 Aug 2003 12:22:27 -0700, Abby wrote:
Sorry to ask another silly question. After above, I need to
concatenate another string characters to "p". How can I do that? I
tried to use strncpy, but it didn't work. I think I really miss
concept about data type. Please help me figure this thing out.

What I need to do is

[snip ... ]

As you should have been able to work our from what was said to you
before, you can use something like...

char buf[128]; /* This _needs_ to be big enough */

/* so assume we test it with the largest s possible */
assert(((strlen(s) * 2) + 8) < sizeof(buf));

sprintf(buf,
"%s"
"%02x%02x%02x%02x"
"%02x%02x%02x%02x"
"%s",
s,
session[0], session[1], session[2], session[3],
id[0], id[1], id[2], id[3],
s);

....which will do what you want, however I'd recommend you take a deeper
look into how pointers and memory work in C ... and also look at using a
real dynamically allocated string type. See...

http://www.and.org/vstr/security.html
http://www.and.org/vstr/comparison.html

--
James Antill -- ja***@and.org
Need an efficent and powerful string library for C?
http://www.and.org/vstr/

Nov 13 '05 #6

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
by: Andrew | last post by:
I have written two classes : a String Class based on the book " C++ in 21 days " and a GenericIpClass listed below : file GenericStringClass.h // Generic String class
13
by: jt | last post by:
I can't seem to find a way to concatenate strings that have nulls within the string. I have a string that I need another string that has nulls in it and what to append the 2nd string, 3 string...
1
by: Martin Andersson | last post by:
Hi ------------------------------------------------------------- typedef struct bitarray { unsigned short length; /* number of significant bits */ unsigned char *value; } bitarray; ...
29
by: zoltan | last post by:
Hi, The scenario is like this : struct ns_rr { const u_char* rdata; }; The rdata field contains some fields such as :
1
by: el jefe | last post by:
hey im new, howdy. here is my prob, i am have this problem using the string count function, i am trying to center some text. looking at my code will clarify. i will be forever in debt to whoever...
10
by: sposes | last post by:
Im very much a newbie but perhaps somehone can help me. Ive been searching for a way to convert a std::string to a unsigned char* The situation is I have a function that wants a unsigned char*...
5
by: Generic Usenet Account | last post by:
I have been to recreate a problem that I am having with strings with the trivial code snippet given below. In the trivial code example, I am reading five lines from a data file, each line having...
14
by: metamorphiq | last post by:
Hello, I'm a Java programmer, so I'm probably asking a very simple question here, but I have trouble solving it :) I'd like to know how you concatenate multiple (4, in my case) char* in C++,...
13
by: sinbad | last post by:
hi, how to concatenate a "hash defined" constant value to another "hash defined" constant string. For example #define ABC 100 #define MYSTR "The value of ABC is" Now i need a string that...
0
by: DolphinDB | last post by:
The formulas of 101 quantitative trading alphas used by WorldQuant were presented in the paper 101 Formulaic Alphas. However, some formulas are complex, leading to challenges in calculation. Take...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
0
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.