how copy int to char[2] using pointer?

Go To Last Post
11 posts / 0 new
Author
Message
#1
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

how to do this using pointer? (codevision avr)
thank :)

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

you can use union

union i2c
{
char c[2];
int i;
};

cs

I'm happy ytd, today, and tmr :)

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

please show example how convert value 0xefda to c[2]

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

At least 3 ways I can think of.

1) Union, as mentioned
2) Explicit memcpy
3) Implicit memcpy, using a casted pointer

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Quote:

At least 3 ways I can think of.

1) Union, as mentioned
2) Explicit memcpy
3) Implicit memcpy, using a casted pointer


"There must be 50 ways to love your lever..."

(we don't know the byte order, nor why one might want to send arbitrary "int" values--does OP really want itoa()? [see https://www.avrfreaks.net/index.p... ] is the data really signed?)

4) c[0] = (unsigned char)i; c[1] = (unsigned char)((unsigned int)i >> 8);

You can put lipstick on a pig, but it is still a pig.

I've never met a pig I didn't like, as long as you have some salt and pepper.

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

char c[2];

c[0] = (char)(0xefda & 0xff);
c[1] = (char)((0xefda >> 8 )&0xff);
--------------------------------or-------------
int i = 0xefda;
char c[2];
char *pc;
pc = &c[0];

*pc = (char)(i & 0xff);
pc++;
*pc = (char)((i >> 8 )& 0xff);

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0
char* itoa( int	__val,
            char * __s,
            int	__radix	
           ) 		

The function itoa() converts the integer value from val into an ASCII representation that will be stored under s. The caller is responsible for providing sufficient storage in s.

Note:
The minimal size of the buffer s depends on the choice of radix. For example, if the radix is 2 (binary), you need to supply a buffer with a minimal length of 8 * sizeof (int) + 1 characters, i.e. one character for each bit plus one for the string terminator. Using a larger radix will require a smaller minimal buffer size.

Warning:
If the buffer is too small, you risk a buffer overflow.

Conversion is done using the radix as base, which may be a number between 2 (binary conversion) and up to 36. If radix is greater than 10, the next digit after '9' will be the letter 'a'.

If radix is 10 and val is negative, a minus sign will be prepended.

The itoa() function returns the pointer passed as s.

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

@pardhu4poori

I'm pretty sure the OP is not looking for a printable string representation of the integer, since two chars will not suffice for this.

Rather, it seems his question is "how to split a (16-bit) integer into two (8-bit) chars?".

As of January 15, 2018, Site fix-up work has begun! Now do your part and report any bugs or deficiencies here

No guarantees, but if we don't report problems they won't get much of  a chance to be fixed! Details/discussions at link given just above.

 

"Some questions have no answers."[C Baird] "There comes a point where the spoon-feeding has to stop and the independent thinking has to start." [C Lawson] "There are always ways to disagree, without being disagreeable."[E Weddington] "Words represent concepts. Use the wrong words, communicate the wrong concept." [J Morin] "Persistence only goes so far if you set yourself up for failure." [Kartman]

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Suppose you have an integer, you can declare a pointer to an array of 2 char:

int my_int;
char (*my_array_p)[2];
...
// then inside main
my_int=0xABCD;  // give a value to the integer
my_array_p= &my_int  // assign the address of my_int

//to read you have to do:
(*my_array_p)[0];  //low byte (0xCD) 
(*my_array_p)[1];  //high byte (0xAB)

if you want to copy the contents to another char array

char my_array[2];

my_array[0]=(*my_array_p)[0];
my_array[1]=(*my_array_p)[1];

Be careful how you declare and how you read the pointer,they should be the same as my example.

char (*my_array_p)[2]; // this is a pointer to an array of 2 char
char *my_array_p[2]; // this is an array of 2 char pointers

Alex

"For every effect there is a root cause. Find and address the root cause rather than try to fix the effect, as there is no end to the latter."
Author Unknown

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

kscharf wrote:
char c[2];

int i = 0xefda;
char c[2];
char *pc;
pc = &c[0];

*pc = (char)(i & 0xff);
pc++;
*pc = (char)((i >> 8 )& 0xff);

this has the same result as

int i = 0xefda;
char c[2];
char *pc;

pc=&i;
c[0]=*pc;
pc++;
c[1]=*pc;

but you will get a few warnings from the compiler,
assuming you know what you are doing you can ignore them.

Alex

"For every effect there is a root cause. Find and address the root cause rather than try to fix the effect, as there is no end to the latter."
Author Unknown

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0
int ival;
int * p_ival;
char c[2];

  p_ival = (int *)c; // (int *)&c[0] is more explicit
  *p_ival = ival;

This is the implicit memcpy using a casted pointer that I mentioned before.