FR

Is there a quick or smart way to turn an int type into a char* type in the C programming language?

For example, if I have an integer defined such as int X = 9000 and a character array buffer char *Y, how can Y be assigned 0x90,0x00?

I can only think of subtraction, that is to separate the int X into two bytes and then subtract a certain number in order to obtain the corresponding char in that ASCII representation.

Here are the relevant parts to the code I am using which might help solve this:

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);
}

and the declaration of the Send function being called is:

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);
    }
}

After running the code this is the output I received:

reponse[0] 90h reponse[1] 00h reponse 244FD38hmessage 244FD38h message[0] FFFFFF90h message[1] 00hmessage[2] 00h
  • 0
    Can you copy and paste some of your relevant code? I think it might be clearer on what is going on that way maybe 🙂 — Brian Wozeniak
add a comment
0

10 Answers

  • Votes
  • Oldest
  • Latest
ED

If you want convert int to char or char to int use these:

char c = 5;
int i = 345;

c = (char)i;
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;
i = atoi(str);
add a comment
0
FR

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. 😁

I'm now fixing another problem....dunno why I can't even put two chars into a char array....I simply use

char *a, b, c;
a[0] = b;
a[1] = c;

there should be nth wrong with that right? 😲

add a comment
0
ED

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

char a[2], b, c;
a[0] = b;
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

char *a=(char *)malloc(2);

which will allocate the memory as well.
same as

char thearray[2];
char *a=thearray;
add a comment
0
BW

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

char *a, b, c; 
a[0] = b; 
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.

add a comment
0
FR

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? 😱

add a comment
0
BW

What is the error you are getting? that might help here.

add a comment
0
FR

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. 😁

add a comment
0
BW

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];

add a comment
0
FR

o I used char b and c...is it ok? Coz I'll simple storing one char...I think it
s ok

add a comment
0
BW

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

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 🙃

  • 0
    Oooh, thank you 😎, I think that just solved the problem! 😁 — frankly
  • 0
    Glad it all works out! — Brian Wozeniak
add a comment
0