How could I transform and save the ASCII value of an integer????

Hi all!

very simple question.
Lets say I have the following:

char str[20];
int num1=7;
int num2=15;
int num3=21;
int num4=50;

How could I transform and then save the ASCII equivalence of those integers in my string "str"?
Thanks in advance!

Your problems come if you try to display the full range of four "int"s in a twenty byte buffer.

thank you Delta_G, well i just want to save 4 values of ascii values in one string.... i have tried with sprintf but... how would u write it? Sprintf(str,"%d%d%d%d",num1,num2,num3,num4);
Of course i know that I shouldnt put the %d because it will save the decimal value so...what would u write instead? Thanks in advance!

Well, according the ascci table:
the ascii value of the decimal number 5 is "bell symbol".15=shift in. 21=negative acknowledge and 50=2. So the string should be: "bell symbol shift in negative ackowledge 2".
Im very sorry i know thay by choosing those values the example becomes hard do understand. Hope you got my point.
You can take a look in this link for the table: http://www.asciitable.com

Of course i know that I shouldnt put the %d because it will save the decimal value

This makes no sense.

You normally want the "decimal value" of your integer, unless you want to trick people by using octal or base-13 or something.

If you are concerned about displaying a decimal fractional value, an interger does not have a fractional value and %d won't display a fractional value for an integer.

Well, according the ascci table:
the ascii value of the decimal number 5 is "bell symbol".15=shift in. 21=negative acknowledge and 50=2. So the string should be: "bell symbol shift in negative ackowledge 2".

You have got that concept completely back to front. The ascii value of the decimal number 5 is bel symbol, is not correct.

The ascii scheme is how characters and control codes are represented in the computer, as numbers. Everything in the computer is represented as numbers. Everything. Characters are represented as numbers. Numbers are represented as numbers. The processor instructions which you source code program is translated into, are represented as numbers.

The "bell' character is represented by a byte with the binary value B00000101 in it, which also corresponds to the number 5. Whether this byte represents a bel character, or the number 5, or something else, depends on the context of your program, and what that byte is supposed to represent.

If your question "how do I transform and save the ASCII value of an integer ?", means what it says,

if you want to "transform" the integer 20 to an ascii value, the normal meaning of this question would be, you want to start with the actual number 20, and end up with the character/text value "20", which is the ascii character '2' followed by the ascii character '0'.

Using the ascii encoding, the characters '2' and '0' are represented by a byte containing the values b00110010 and then b00110000 . This can be more conveniently written using hexadecimal notation as 0x32 and 0x30, which is just a different way of writing the same binary number.

These 2 single-byte binary numbers also happen to be the same as 50 and 48, when considered to be simple binary numbers representing decimal integers.

Delta_G:
If I'm still not understanding you right then maybe it is time for you to get a little more verbose about what you are trying to accomplish.

Of course, I'll give more details. Im working in a project making a SIP call. So far, I can stablish connection from my wifishield to another SIP phone. But now comes the interesting part, how to talk!? and this is done through the RTP packages, those packages are generated following a specific protocol. Im receiving those RTP packages in ASCII characters.
And now its me who wants to generate those RTP packages and send them, and to make it easier for me to generate those , Im creating packages in decimal, and then, at some point I would need to transform those packages into ASCII before sending them.
Im sending those packages to the other SIP phone by UDP. And the UDP library does not allow me to use the same trick of "Serial.write()"; Whatever I need to send must be in a string.

I know im not good at explaining my ideas, but hope that was close enough :slight_smile:
Once again, thanks in advance!

michinyon:
You normally want the "decimal value" of your integer, unless you want to trick people by using octal or base-13 or something.

You got my answer in the previous post

michinyon:

kokaly:
Hi all!

very simple question.
Lets say I have the following:

char str[20];
int num1=7;
int num2=15;
int num3=21;
int num4=50;

How could I transform and then save the ASCII equivalence of those integers in my string "str"?
Thanks in advance!

Really, I think the problem is so trivial that nobody can believe it. It is happening because you don't understand the versatility of variables, and don't know how to cast. Here is how you can do it:

str[0] = (char)num1;
str[1] = (char)num2;
str[2] = (char)num3;
str[3] = (char)num4;

break for coffee.

But maybe you should look at how those values got to be ints in the first place. Somewhere along the line, they should have been created as chars. Then you wouldn't have this problem. Like:

char num1 = 7;
str[0] = num1;