Convert int array to char array

F

Frank Liebelt

Hi

I try to convert a int array into a char array. My code: void exec()
{
char mds[32];
int i;

int mdc[32] =
{50,100,97,51,101,50,99,51,48,55,100,102,55,53,101,56,100,48,101,53,101,52,99,98,102,55,101,50,53,100,49,53};

for(i=0; i < 32; i++) {
sprintf(mds, "%s", (char*)mdc);
}

printf("Result : %s\n", md5s);

}

If i compile this i got one compiler warning:
warning: passing arg 1 of `sprintf' makes pointer from integer without
a cast

Cause this was the only warning i tried to run the program but get allways
a Segmentation Fault.

I dont know what to do. Uncle Goolge doesn't help me. Sorry, maybe i use
the wrong search terms.

Who could tell me whats going wrong?

Frank
 
M

Mark Bluemel

Frank said:
Hi

I try to convert a int array into a char array. My code: void exec()
{
char mds[32];
int i;

int mdc[32] =
{50,100,97,51,101,50,99,51,48,55,100,102,55,53,101,56,100,48,101,53,101,52,99,98,102,55,101,50,53,100,49,53};

for(i=0; i < 32; i++) {
sprintf(mds, "%s", (char*)mdc);
}

printf("Result : %s\n", md5s);

}

If i compile this i got one compiler warning:
warning: passing arg 1 of `sprintf' makes pointer from integer without
a cast


The first argument to sprintf() should be the address of a character
buffer... You pass it an uninitialised character ...
Cause this was the only warning i tried to run the program but get allways
a Segmentation Fault.

You've told sprintf() that you will be passing it a pointer to a
null-terminated string. You instead take an arbitrary integer and cast
it to a pointer - what do you expect?
Who could tell me whats going wrong?

It may be that I'm wasting my time talking to a troll...
 
N

Nick Keighley

I try to convert a int array into a char array. My code: void exec()

I think you're trying to convert the int into its corresponding
string ie. into a char*. So you want an array of char*.

{
char mds[32];

you want an array of char* or an array of array of char

char mds [32][8];

int i;

int mdc[32] =
{50,100,97,51,101,50,99,51,48,55,100,102,55,53,101,56,100,48,101,53,101,52,­99,98,102,55,101,50,53,100,49,53};

for(i=0; i < 32; i++) {
sprintf(mds, "%s", (char*)mdc);


- mds is of type char whilst sptintf() wants char*
- the format for int is %d not %s
- the cast does nothing useful. An int is not a char*
don't put casts in just to get rid of compiler errors.

sprintf(&mds, "%d", mdc);


}

printf("Result : %s\n", md5s);

what is md5s?

}

If i compile this i got one compiler warning:
warning: passing arg 1 of `sprintf' makes pointer from integer without
a cast

because you did...
 
J

Joachim Schmitz

Frank Liebelt said:
Hi

I try to convert a int array into a char array. My code: void exec()
{
char mds[32];
int i;

int mdc[32] =
{50,100,97,51,101,50,99,51,48,55,100,102,55,53,101,56,100,48,101,53,101,52,99,98,102,55,101,50,53,100,49,53};

for(i=0; i < 32; i++) {
sprintf(mds, "%s", (char*)mdc);

you're not deraling with strings (NUL terminated char arrays), so don't use
sprintf here.

mds=(char)mds; /* cast to silence a compiler warning about loss of
precision and possibly sign (if char is unsigned in your implementation),
which seems exaclty your intention */
}

printf("Result : %s\n", md5s);

}

If i compile this i got one compiler warning:
warning: passing arg 1 of `sprintf' makes pointer from integer without
a cast
Because you do. mds is not a char * but a char
Cause this was the only warning i tried to run the program but get allways
a Segmentation Fault.
because mds is not a NUL termainated char array. The cast doesn't turn it
into such a thing but instead lies to the compiler and the compiler then
takes it's revenge...

Bye, Jojo
 
F

Frank Liebelt

@Mark
No, you dont talk to a troll i am just a beginner.

@Nick
printf("Result : %s\n", md5s);

I dont copy/paste the code. I typed it by hand and this was a mistake.
This should be: printf("Result : %s\n", mds);

@Joachim
I tried your solution and it worked. Thanks.
But now i have only one problem left.

The int array is as char: 2da3e2c307df75e8d0e5e4cbf7e25d15
but printf("Result : %s\n", mds); shows me
2da3e2c307df75e8d0e5e4cbf7e25d15
How could i remove this character after the last 5 and where it comes from?
The array has a size of 32 but this above are 33.

Sorry if this are dumb questions but i still start to learn C.

Frank
 
J

Joachim Schmitz

Frank Liebelt said:
@Mark
No, you dont talk to a troll i am just a beginner.

@Nick
printf("Result : %s\n", md5s);

I dont copy/paste the code. I typed it by hand and this was a mistake.
This should be: printf("Result : %s\n", mds);

@Joachim
I tried your solution and it worked. Thanks.
But now i have only one problem left.

The int array is as char: 2da3e2c307df75e8d0e5e4cbf7e25d15
but printf("Result : %s\n", mds); shows me
2da3e2c307df75e8d0e5e4cbf7e25d15
How could i remove this character after the last 5 and where it comes
from?
The array has a size of 32 but this above are 33.

Sorry if this are dumb questions but i still start to learn C.
Sorry, I've missed that you tried tp prinft that char array.
The problem is, that it still isn't a string, as it doesn't have a
terminating NUL (at least not at the right place)

Make that char array one element bigger (mds[33]) and place a NUL in the
last element (mds[32]='\0';)
 
A

Army1987

Hi

I try to convert a int array into a char array. My code: void exec()
{
char mds[32];
int i;

int mdc[32] =
{50,100,97,51,101,50,99,51,48,55,100,102,55,53,101,56,100,48,101,53,101,52,99,98,102,55,101,50,53,100,49,53};

for(i=0; i < 32; i++) {
sprintf(mds, "%s", (char*)mdc);

This tries to copy whatever is in memory location 50 (or 100, or
97...) into wherever uninitialized mds[0] (or mds[1]...) happens
to point when converted to a char *.
You meant for(i=0; i<32; i++) { mds = mdc; }, right?
}

printf("Result : %s\n", md5s);
What is md5s? I guess a file scope variable, but you didn't touch
it. (Or are the magic numbers in that array its address? If so,
whatever you're trying to do is highly platform-specific).
 
F

Frank Liebelt

Am Thu, 27 Sep 2007 12:30:40 +0200 schrieb Frank Liebelt:
How could i remove this character after the last 5 and where it comes from?
The array has a size of 32 but this above are 33.

Sorry if this are dumb questions but i still start to learn C.

Resolved.
char mds[33]; instead of char mds[32]; to have space for \0 at the end.

Found in my C Book.

Frank
 
M

Martin Ambuhl

Frank said:
Hi

I try to convert a int array into a char array. My code: void exec()
{
char mds[32];
int i;

int mdc[32] =
{50,100,97,51,101,50,99,51,48,55,100,102,55,53,101,56,100,48,101,53,101,52,99,98,102,55,101,50,53,100,49,53};

for(i=0; i < 32; i++) {
sprintf(mds, "%s", (char*)mdc);


mds is a char (an integer), not an array, or address
of an array, or a pointer to an array. The first
argument to sprintf() must be compatible with a
pointer-to-char, which an integer is not.

mdc is an int, and it makes no sense to cast it to a
pointer

Even if your code did what you seem to want it to do (see
below), you have not null-terminated the array. In fact, you
_can't_, because you haven't provided space for that '\0' byte.
That means that mds is not a string and the following (with the
typo corrected) is an error.
printf("Result : %s\n", md5s);
^^^^
Cut-and-paste your actual code. This typo would not then creep
in.

#include <stdio.h>

void exec()
{

int mdc[] =
{ 50, 100, 97, 51, 101, 50, 99, 51, 48, 55, 100, 102, 55, 53,
101, 56, 100, 48, 101, 53, 101, 52, 99, 98, 102, 55, 101, 50,
53, 100,
49, 53
};
size_t nchars = sizeof mdc / sizeof *mdc, i;
char mds[1 + nchars];

for (i = 0; i < nchars; i++) {
mds = mdc;
}
mds[nchars] = 0;
printf("Result (encoding is implementation-specific):\n \"%s\"\n",
mds);

}

int main(void)
{
exec();
return 0;
}


Result (encoding is implementation-specific):
"2da3e2c307df75e8d0e5e4cbf7e25d15"
If i compile this i got one compiler warning:
warning: passing arg 1 of `sprintf' makes pointer from integer without
a cast

Your cast masked one of your other errors.
Cause this was the only warning i tried to run the program but get allways
a Segmentation Fault.

I dont know what to do. Uncle Goolge doesn't help me. Sorry, maybe i use
the wrong search terms.

Your C text should tell you what the arguments to sprintf are, and
should tell you the difference between a char and something that will
decay to a pointer-to-char as an argument. Google is not the answer to
everything.
Who could tell me whats going wrong?

Much. See above.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,769
Messages
2,569,580
Members
45,055
Latest member
SlimSparkKetoACVReview

Latest Threads

Top