Convert int array to char array - C
This is a discussion on Convert int array to char array - C ; 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[i], "%s", (char*)mdc[i]);
}
printf("Result : %s\n", md5s);
}
...
-
Convert int array to char array
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[i], "%s", (char*)mdc[i]);
}
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
-
Re: Convert int array to char array
Frank Liebelt wrote:
> 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[i], "%s", (char*)mdc[i]);
> }
>
> 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...
-
Re: Convert int array to char array
On 27 Sep, 10:41, Frank Liebelt <ecos...@hotmail.de> wrote:
> 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[i], "%s", (char*)mdc[i]);
- mds[i] 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[i], "%d", mdc[i]);
> }
>
> 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...
> 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?
--
Nick Keighley
-
Re: Convert int array to char array
"Frank Liebelt" <ecosys_@hotmail.de> schrieb im Newsbeitrag
news:46fb7abd@news.ish.de...
> 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[i], "%s", (char*)mdc[i]);
you're not deraling with strings (NUL terminated char arrays), so don't use
sprintf here.
mds[i]=(char)mds[i]; /* 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[i] 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[i] 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
-
Re: Convert int array to char array
@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
-
Re: Convert int array to char array
"Frank Liebelt" <ecosys_@hotmail.de> schrieb im Newsbeitrag
news:46fb8650@news.ish.de...
> @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'
-
Re: Convert int array to char array
On Thu, 27 Sep 2007 11:41:17 +0200, Frank Liebelt wrote:
> 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[i], "%s", (char*)mdc[i]);
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[i] = mdc[i]; }, 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).
> }
>
> 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
--
Army1987 (Replace "NOSPAM" with "email")
A hamburger is better than nothing.
Nothing is better than eternal happiness.
Therefore, a hamburger is better than eternal happiness.
-
Re: Convert int array to char array
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
-
Re: Convert int array to char array
Frank Liebelt wrote:
> 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[i], "%s", (char*)mdc[i]);
mds[i] 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[i] 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[i] = mdc[i];
}
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.
>
> Frank
Similar Threads
-
By Application Development in forum Java
Replies: 7
Last Post: 01-11-2008, 02:11 PM
-
By Application Development in forum Javascript
Replies: 1
Last Post: 09-07-2007, 07:20 PM
-
By Application Development in forum Java
Replies: 1
Last Post: 07-26-2007, 10:03 AM
-
By Application Development in forum c++
Replies: 1
Last Post: 06-17-2007, 12:29 AM
-
By Application Development in forum C
Replies: 0
Last Post: 06-07-2007, 07:37 AM