Converting int to char* in C

  • frankly
  • Beginner
  • Beginner
  • No Avatar
  • Joined: Feb 28, 2003
  • Posts: 52
  • Status: Offline

Post March 7th, 2003, 7:23 am

Hi all

Do u guys know any quick or smart way to turn a int type into a char* ? I mean like there is a "int X = 9000" and a char array buffer char *Y. How should I make Y to be {0x90, 0x00}? I can only think of subtraction, thath is separate the int X into two bytes and then minus a certain number in order to obtain the corresponding char in that ASCII number. I m sure u guys will think of some better ways. please do let me know. Thanz !!

Franky :D
  • Anonymous
  • Bot
  • No Avatar
  • Joined: 25 Feb 2008
  • Posts: ?
  • Loc: Ozzuland
  • Status: Online

Post March 7th, 2003, 7:23 am

  • ED
  • Student
  • Student
  • No Avatar
  • Joined: Jan 02, 2003
  • Posts: 66
  • Loc: Halifax
  • Status: Offline

Post March 7th, 2003, 4:00 pm

If you want convert int to char or char to int use these:
char c=5;
int i=345;

c=(char)i;
or
i=(int)c;

If you want convert int to char* (string) or char* to int use these:
char str[10];
int i=567;

str=itoa(i, str, 10); // 10 - decimal;
or
i=atoi(str);
  • frankly
  • Beginner
  • Beginner
  • No Avatar
  • Joined: Feb 28, 2003
  • Posts: 52
  • Status: Offline

Post March 7th, 2003, 6:31 pm

o...Actually I want to make a decimal int type e.g. 9000, that's nine thousand into a hexadecimal char array, {0x90, 0x00), that's in decimal 144 and 0. Also, I'm not really displaying the char array, I have to store it. So I don't think I can use atoi.

Yes it is very weird to change it like this....I'm thinking of some other ways to modify my program so that there's no need for that stupid change hehe. :D

I'm now fixing another problem....don't know why I can't even put two chars into a char array....I simply use
Code: [ Select ]
char *a, b, c;
a[0] = b;
a[1] = c;
  1. char *a, b, c;
  2. a[0] = b;
  3. a[1] = c;

there should be nth wrong with that right? :o
  • ED
  • Student
  • Student
  • No Avatar
  • Joined: Jan 02, 2003
  • Posts: 66
  • Loc: Halifax
  • Status: Offline

Post March 7th, 2003, 6:53 pm

I'm just gonna pretend that I know a little about C, which i do... but VERY little.

you're declaring 'a' as a pointer and then treating it as an array.

you don't need the pointer at all to do just what you have there
define a as an array like

Code: [ Select ]
char a[2], b, c;
a[0] = b;
a[1] = c;
  1. char a[2], b, c;
  2. a[0] = b;
  3. a[1] = c;


when you use char *a you allocate only the pointer but not the memory to store the data.

i'm not sure but i think you can do this
Code: [ Select ]
char *a=(char *)malloc(2);

which will allocate the memory as well.
same as
Code: [ Select ]
char thearray[2];
char *a=thearray;
  1. char thearray[2];
  2. char *a=thearray;
  • Bigwebmaster
  • Site Admin
  • Site Admin
  • User avatar
  • Joined: Dec 20, 2002
  • Posts: 8925
  • Loc: Seattle, WA & Phoenix, AZ
  • Status: Offline

Post March 7th, 2003, 7:10 pm

For your first problem, I am curious to why you need to split the number and store it like that?

As far as your second problem

Code: [ Select ]
char *a, b, c;
a[0] = b;
a[1] = c;
  1. char *a, b, c;
  2. a[0] = b;
  3. a[1] = c;


Does it give you a compilation error when you try to compile it? Also I believe in your case you are really going to be dynamically changing the size of that char array. I would think you would need to use some kind of memory managment to allocate that memory so that you do not get segmentation faults or the like. Anyway in C++ you would use the new operator to first create an array that has a dynamic size. In C you would use something like malloc I believe.

int *array;
array = new int[50]; //c++

Not sure if that helps any.
Ozzu Hosting - Want your website on a fast server like Ozzu?
  • frankly
  • Beginner
  • Beginner
  • No Avatar
  • Joined: Feb 28, 2003
  • Posts: 52
  • Status: Offline

Post March 7th, 2003, 7:10 pm

o i'm sorry hehe, yes i did put it like that, i used char a[256] but still....errors...that is the right way already right? :shock:
  • Bigwebmaster
  • Site Admin
  • Site Admin
  • User avatar
  • Joined: Dec 20, 2002
  • Posts: 8925
  • Loc: Seattle, WA & Phoenix, AZ
  • Status: Offline

Post March 7th, 2003, 7:13 pm

What is the error you are getting? that might help here.
Ozzu Hosting - Want your website on a fast server like Ozzu?
  • frankly
  • Beginner
  • Beginner
  • No Avatar
  • Joined: Feb 28, 2003
  • Posts: 52
  • Status: Offline

Post March 7th, 2003, 7:24 pm

actually i think maybe it's just i made it wrong coz that codes didn't really return anything wrong
after the operation yes the a got the chars b and c. but then i wanna pass this parameter "a" to another function.
now there is the problem, in that function when i display the char "b", it's "FFFFFF90h". it is supposed to be "90h"........ :?
i think i'll save to display the codes of that function here coz it's a real long one. :D
  • Bigwebmaster
  • Site Admin
  • Site Admin
  • User avatar
  • Joined: Dec 20, 2002
  • Posts: 8925
  • Loc: Seattle, WA & Phoenix, AZ
  • Status: Offline

Post March 7th, 2003, 7:35 pm

Okay so for character string b, did you set it to be 90h ? and how did you go about that? Also did you simply initialize b as char * b? Or did you give it a fixed length like char b[2];
Ozzu Hosting - Want your website on a fast server like Ozzu?
  • frankly
  • Beginner
  • Beginner
  • No Avatar
  • Joined: Feb 28, 2003
  • Posts: 52
  • Status: Offline

Post March 7th, 2003, 7:48 pm

o I used char b and c...is it ok? Coz I'll simple storing one char...I think it
s ok
  • Bigwebmaster
  • Site Admin
  • Site Admin
  • User avatar
  • Joined: Dec 20, 2002
  • Posts: 8925
  • Loc: Seattle, WA & Phoenix, AZ
  • Status: Offline

Post March 7th, 2003, 7:56 pm

Can you copy and paste some of your relevant code? I think it might be clearer on what is going on that way maybe :)
Ozzu Hosting - Want your website on a fast server like Ozzu?
  • frankly
  • Beginner
  • Beginner
  • No Avatar
  • Joined: Feb 28, 2003
  • Posts: 52
  • Status: Offline

Post March 7th, 2003, 8:08 pm

ok i'll skmply put the relevant parts
Code: [ Select ]
int main(void)

{
unsigned char *datafield, *command1, command[256], response[256], commandlen1, resplen;
unsigned short int commandlen, i, j, k, sw, x;

sw1 = 0x90;
sw2 = 0x00;
response[0] = sw1;
response[1] = sw2;
resplen = sizeof(sw);
printf("reponse[0] %02Xh ",response[0]);
printf("reponse[1] %02Xh ",response[1]);
printf("reponse %02Xh", response);
Send(response, resplen);

}
  1. int main(void)
  2. {
  3. unsigned char *datafield, *command1, command[256], response[256], commandlen1, resplen;
  4. unsigned short int commandlen, i, j, k, sw, x;
  5. sw1 = 0x90;
  6. sw2 = 0x00;
  7. response[0] = sw1;
  8. response[1] = sw2;
  9. resplen = sizeof(sw);
  10. printf("reponse[0] %02Xh ",response[0]);
  11. printf("reponse[1] %02Xh ",response[1]);
  12. printf("reponse %02Xh", response);
  13. Send(response, resplen);
  14. }


nd the function being called is
Code: [ Select ]
short int Send(char *message, unsigned short int nbytes) {
printf("message %02Xh ", message);
printf("message[0] %02Xh ", message[0]);
printf("message[1] %02Xh", message[1]);
printf("message[2] %02Xh", message[2]);
printf("nbytes %i ", nbytes);
    if ((fd = open("reader.txt", O_WRONLY | O_CREAT | O_TRUNC, S_IRUSR | S_IWUSR | S_IRGRP | S_IROTH ))<0)
    printf("open error\n");
    if (write(fd, message, nbytes) != nbytes)
        printf("write error\n");
    if (close(fd)<0)
        printf("close error\n");
    return(NO_ERR);
}
  1. short int Send(char *message, unsigned short int nbytes) {
  2. printf("message %02Xh ", message);
  3. printf("message[0] %02Xh ", message[0]);
  4. printf("message[1] %02Xh", message[1]);
  5. printf("message[2] %02Xh", message[2]);
  6. printf("nbytes %i ", nbytes);
  7.     if ((fd = open("reader.txt", O_WRONLY | O_CREAT | O_TRUNC, S_IRUSR | S_IWUSR | S_IRGRP | S_IROTH ))<0)
  8.     printf("open error\n");
  9.     if (write(fd, message, nbytes) != nbytes)
  10.         printf("write error\n");
  11.     if (close(fd)<0)
  12.         printf("close error\n");
  13.     return(NO_ERR);
  14. }

i got the output
Code: [ Select ]
reponse[0] 90h reponse[1] 00h reponse 244FD38hmessage 244FD38h message[0] FFFFFF90h message[1] 00hmessage[2] 00h

Thanz!!
  • Bigwebmaster
  • Site Admin
  • Site Admin
  • User avatar
  • Joined: Dec 20, 2002
  • Posts: 8925
  • Loc: Seattle, WA & Phoenix, AZ
  • Status: Offline

Post March 7th, 2003, 8:37 pm

For your function try making it

short int Send(unsigned char *message, unsigned short int nbytes)

for the start. I added the unsigned since you were actually passing an unsigned value to it. Let me know if that changes any of your results. The compiler I am using will not even compile the code until I change it to unsigned. Anyway after running the program the result I get is

Code: [ Select ]
reponse[0] 90h reponse[1] 00h reponse BFFFF870hmessage BFFFF870h message[0] 90h message[1] 00hmessage[2] 00hnbytes 2


To me this looks like the output you were trying to obtain. I think :silly:
Ozzu Hosting - Want your website on a fast server like Ozzu?
  • frankly
  • Beginner
  • Beginner
  • No Avatar
  • Joined: Feb 28, 2003
  • Posts: 52
  • Status: Offline

Post March 7th, 2003, 9:01 pm

oooooooo :D :D :D
THANZ !! haha 8)
i think that just solved the problem
cool thanz a bunch !! hehe :D
  • Bigwebmaster
  • Site Admin
  • Site Admin
  • User avatar
  • Joined: Dec 20, 2002
  • Posts: 8925
  • Loc: Seattle, WA & Phoenix, AZ
  • Status: Offline

Post March 7th, 2003, 9:16 pm

Glad it all works out!
Ozzu Hosting - Want your website on a fast server like Ozzu?
  • Anonymous
  • Bot
  • No Avatar
  • Joined: 25 Feb 2008
  • Posts: ?
  • Loc: Ozzuland
  • Status: Online

Post March 7th, 2003, 9:16 pm

Post Information

  • Total Posts in this topic: 15 posts
  • Users browsing this forum: No registered users and 144 guests
  • You cannot post new topics in this forum
  • You cannot reply to topics in this forum
  • You cannot edit your posts in this forum
  • You cannot delete your posts in this forum
  • You cannot post attachments in this forum
 
 

© 2011 Unmelted, LLC. Ozzu® is a registered trademark of Unmelted, LLC.