int <-> char[32] 
Author Message
 int <-> char[32]

Hi. I face a little difficult problem for me.

There are two variables -

////////////////////////////////////////////////////////
    char foo[32];
    int    sockslot;
////////////////////////////////////////////////////////

sockslot is more than or equal to 0 and less than or eqaul to 2048.
This exceed char range.(-127 ~ 128)

I want to insert sockslot into foo array and get the original int value from
foo array.

How can I solve this problem?

Thanks in advance!!



Tue, 20 Apr 2004 14:15:47 GMT  
 int <-> char[32]


Quote:
>Hi. I face a little difficult problem for me.

>There are two variables -

>////////////////////////////////////////////////////////
>    char foo[32];
>    int    sockslot;
>////////////////////////////////////////////////////////

>sockslot is more than or equal to 0 and less than or eqaul to 2048.
>This exceed char range.(-127 ~ 128)

>I want to insert sockslot into foo array and get the original int value from
>foo array.

Did you mean [-128..127] ? At any rate, [-127..127] (yes, that's not a
typo) is a guaranteed minimum range for signed char (BTW, make that
signed char foo[32]; to be explicit).

Say you want to stuff ints in that array in a loop.

char foo[32], *pfoo = foo;
int sockslot = 6;
while (pfoo - foo >= (ptrdiff_t) sizeof (int))
{
  *((int *) pfoo++) = sockslot;

Quote:
}

and to get out:
int n = ((int *) foo)[NEEDED_INDEX]

This will stuff however many ints fit into a 32 bytes area.
BTW, this is undefined behaviour if char foo[32] is not properly
aligned for int *, _I think_.
Anyway, don't do this :)

Quote:
>How can I solve this problem?

>Thanks in advance!!

--
rihad


Wed, 21 Apr 2004 02:57:24 GMT  
 int <-> char[32]


Quote:
>     char foo[32];
>     int    sockslot;

> sockslot is more than or equal to 0 and less than or eqaul to 2048.
> This exceed char range.(-127 ~ 128)

and unsigned char minimum range too that is 0-255. No doubt, you need a
short or an int.

   short foo[32];
or
   int foo[32];

Quote:
> I want to insert sockslot into foo array and get the original int value
> from foo array.

> How can I solve this problem?

AFAIK, you can't (at last portably. Some DSP's have 16 or 32 bit char)
without changing foo's type.

--
-hs- emdel at noos.fr "support Afghans against Taleban"
"Car les bandits qui sont cause des guerres
 N'en meurent jamais, on n'tue qu'les innocents."
Gaston Monthus -- La Butte Rouge



Tue, 20 Apr 2004 15:17:26 GMT  
 int <-> char[32]

Quote:

>Hi. I face a little difficult problem for me.

>There are two variables -

>////////////////////////////////////////////////////////
>    char foo[32];
>    int    sockslot;
>////////////////////////////////////////////////////////

>sockslot is more than or equal to 0 and less than or eqaul to 2048.
>This exceed char range.(-127 ~ 128)

>I want to insert sockslot into foo array and get the original int value
from
>foo array.

>How can I solve this problem?

>Thanks in advance!!

As Deep Thought said, "You have to know what the Question is!".

One answer is:

  sprintf(foo, "%d", sockslot);
  sscanf(foo, "%d", &sockslot);

Another answer might be:

From the limit range provided by the Standards, it is obvious that the
integer range 0..2048 inclusive will occupy no more than 2 chars, unsigned
or otherwise.

Now if sizeof(int) == 1, as it can, then:

  foo[0] = sockslot;
  sockslot = foo[0];

will do the trick!

Otherwise:

  foo[0] = sockslot & 0x7F;
  foo[1] = sockslot >> 7;

  sockslot = foo[0] + (foo[1] << 7);

will work, provided the original conditions hold.

[But use it quick Charlie before someone points out a platform from 1944 on
which certain valves would blow in the same manner as a modern illegally
accessed trap representation. :)]

--
Peter



Tue, 20 Apr 2004 15:41:31 GMT  
 int <-> char[32]

Quote:

> Hi. I face a little difficult problem for me.

> There are two variables -

> ////////////////////////////////////////////////////////
>     char foo[32];
>     int    sockslot;
> ////////////////////////////////////////////////////////

> sockslot is more than or equal to 0 and less than or eqaul to 2048.
> This exceed char range.(-127 ~ 128)

> I want to insert sockslot into foo array and get the original int value from
> foo array.

> How can I solve this problem?

Make foo, an array of unsigned char.

/* BEGIN new.c */  

#include <stdio.h>

int main(void)
{
    int sockslot = 2000;
    unsigned char foo[sizeof(int)];

    *(int*) foo = sockslot;
    printf("%i\n", *(int*)foo);
    return 0;

Quote:
}

/* END new.c */

--
 pete



Wed, 21 Apr 2004 17:04:05 GMT  
 int <-> char[32]

Quote:


> > Hi. I face a little difficult problem for me.

> > There are two variables -

> > ////////////////////////////////////////////////////////
> >     char foo[32];
> >     int    sockslot;
> > ////////////////////////////////////////////////////////

> > sockslot is more than or equal to 0 and less than or eqaul to 2048.
> > This exceed char range.(-127 ~ 128)

> > I want to insert sockslot into foo array and get the original int value from
> > foo array.

> > How can I solve this problem?

> Make foo, an array of unsigned char.

> /* BEGIN new.c */

> #include <stdio.h>

> int main(void)
> {
>     int sockslot = 2000;
>     unsigned char foo[sizeof(int)];

>     *(int*) foo = sockslot;
>     printf("%i\n", *(int*)foo);
>     return 0;
> }

> /* END new.c */

Oops!, that has an alignment problem.

/* BEGIN new.c */  

#include <stdio.h>

#define foo (align.Foo)

int main(void)
{
    union{
        int X;
        unsigned char Foo[sizeof(int)];
    }align;
    int sockslot = 2000;

    *(int*) foo = sockslot;
    printf("%i\n", *(int*)foo);
    return 0;

Quote:
}

/* END new.c */

--
 pete



Wed, 21 Apr 2004 17:15:24 GMT  
 int <-> char[32]


Quote:

>> Hi. I face a little difficult problem for me.

>> There are two variables -

>> ////////////////////////////////////////////////////////
>>     char foo[32];
>>     int    sockslot;
>> ////////////////////////////////////////////////////////

>> sockslot is more than or equal to 0 and less than or eqaul to 2048.
>> This exceed char range.(-127 ~ 128)

>> I want to insert sockslot into foo array and get the original int
>> value from foo array.

>> How can I solve this problem?

> Make foo, an array of unsigned char.

> /* BEGIN new.c */  

> #include <stdio.h>

> int main(void)
> {
>     int sockslot = 2000;
>     unsigned char foo[sizeof(int)];

>     *(int*) foo = sockslot;

This is not portable due to endianness.

Quote:
>     printf("%i\n", *(int*)foo);
>     return 0;
> }

> /* END new.c */

--
-hs- emdel at noos.fr "support Afghans against Taleban"
"Car les bandits qui sont cause des guerres
 N'en meurent jamais, on n'tue qu'les innocents."
Gaston Monthus -- La Butte Rouge


Wed, 21 Apr 2004 17:16:25 GMT  
 int <-> char[32]

Quote:




> >> Hi. I face a little difficult problem for me.

> >> There are two variables -

> >> ////////////////////////////////////////////////////////
> >>     char foo[32];
> >>     int    sockslot;
> >> ////////////////////////////////////////////////////////

> >> sockslot is more than or equal to 0 and less than or eqaul to 2048.
> >> This exceed char range.(-127 ~ 128)

> >> I want to insert sockslot into foo array and get the original int
> >> value from foo array.

> >> How can I solve this problem?

> > Make foo, an array of unsigned char.

> > /* BEGIN new.c */

> > #include <stdio.h>

> > int main(void)
> > {
> >     int sockslot = 2000;
> >     unsigned char foo[sizeof(int)];

> >     *(int*) foo = sockslot;

> This is not portable due to endianness.

It's not portable for alignment reasons.
Why do you think there's a problem for endianness?

--
 pete



Wed, 21 Apr 2004 18:18:52 GMT  
 int <-> char[32]

Quote:




> >> Hi. I face a little difficult problem for me.

> >> There are two variables -

> >> ////////////////////////////////////////////////////////
> >>     char foo[32];
> >>     int    sockslot;
> >> ////////////////////////////////////////////////////////

> >> sockslot is more than or equal to 0 and less than or eqaul to 2048.
> >> This exceed char range.(-127 ~ 128)

> >> I want to insert sockslot into foo array and get the original int
> >> value from foo array.

> >> How can I solve this problem?

> > Make foo, an array of unsigned char.

> > /* BEGIN new.c */

> > #include <stdio.h>

> > int main(void)
> > {
> >     int sockslot = 2000;
> >     unsigned char foo[sizeof(int)];

> >     *(int*) foo = sockslot;

> This is not portable due to endianness.

It's not portable for alignment reasons.
Why do you think there's a problem with endianness?

--
 pete



Wed, 21 Apr 2004 18:22:14 GMT  
 int <-> char[32]

Quote:




>> >> Hi. I face a little difficult problem for me.

>> >> There are two variables -

>> >> ////////////////////////////////////////////////////////
>> >>     char foo[32];
>> >>     int    sockslot;
>> >> ////////////////////////////////////////////////////////

>> >> sockslot is more than or equal to 0 and less than or eqaul to 2048.
>> >> This exceed char range.(-127 ~ 128)

>> >> I want to insert sockslot into foo array and get the original int
>> >> value from foo array.

>> >> How can I solve this problem?

>> > Make foo, an array of unsigned char.

>> > /* BEGIN new.c */

>> > #include <stdio.h>

>> > int main(void)
>> > {
>> >     int sockslot = 2000;
>> >     unsigned char foo[sizeof(int)];

>> >     *(int*) foo = sockslot;

>> This is not portable due to endianness.

> It's not portable for alignment reasons.
> Why do you think there's a problem for endianness?

You are writing an int into an array of unsigned char.

- It can use a various number of bytes
- Some can be encoded

[0] MSB
...
[x] LSB

or

[0] LSB
...
[x] MSB
 or whatever. That means many reasons to have a different memory (or
stream) representation, making exchange of data not portable.

--
-hs- emdel at noos.fr "support Afghans against Taleban"
"Car les bandits qui sont cause des guerres
 N'en meurent jamais, on n'tue qu'les innocents."
Gaston Monthus -- La Butte Rouge



Wed, 21 Apr 2004 18:45:53 GMT  
 int <-> char[32]

Quote:






> >> >> Hi. I face a little difficult problem for me.

> >> >> There are two variables -

> >> >> ////////////////////////////////////////////////////////
> >> >>     char foo[32];
> >> >>     int    sockslot;
> >> >> ////////////////////////////////////////////////////////

> >> >> sockslot is more than or equal to 0 and less than or eqaul to 2048.
> >> >> This exceed char range.(-127 ~ 128)

> >> >> I want to insert sockslot into foo array and get the original int
> >> >> value from foo array.

> >> >> How can I solve this problem?

> >> > Make foo, an array of unsigned char.

> >> > /* BEGIN new.c */

> >> > #include <stdio.h>

> >> > int main(void)
> >> > {
> >> >     int sockslot = 2000;
> >> >     unsigned char foo[sizeof(int)];

> >> >     *(int*) foo = sockslot;

> >> This is not portable due to endianness.

> > It's not portable for alignment reasons.
> > Why do you think there's a problem for endianness?

> You are writing an int into an array of unsigned char.

> - It can use a various number of bytes
> - Some can be encoded

> [0] MSB
> ...
> [x] LSB

> or

> [0] LSB
> ...
> [x] MSB
>  or whatever. That means many reasons to have a different memory (or
> stream) representation, making exchange of data not portable.

Actually, there was no reason to use unsigned char,
plain char would have been fine.
It doesn't matter how the memory is allocated,
as long as it lines up with an int, and is big enough for an int,
then that memory can accessed with pointers for int, and used as int.

/* BEGIN new.c */  

#include <stdio.h>
#include <stdlib.h>

#define foo   (align.Foo)
#define RANGE 17

int main(void)
{
    union{
        int X;
        float Foo[RANGE * sizeof(int)];
    }align;
    int sockslot = 2000;
    int num;

    num = rand() % RANGE;
    ((int*)foo)[num] = sockslot;
    printf("%i\n", ((int*)foo)[num]);
    return 0;

Quote:
}

/* END new.c */

--
 pete



Wed, 21 Apr 2004 19:47:27 GMT  
 int <-> char[32]


Quote:
>> You are writing an int into an array of unsigned char.

>> - It can use a various number of bytes
>> - Some can be encoded

>> [0] MSB
>> ...
>> [x] LSB

>> or

>> [0] LSB
>> ...
>> [x] MSB
>>  or whatever. That means many reasons to have a different memory (or
>> stream) representation, making exchange of data not portable.

> Actually, there was no reason to use unsigned char,
> plain char would have been fine.
> It doesn't matter how the memory is allocated,
> as long as it lines up with an int, and is big enough for an int,
> then that memory can accessed with pointers for int, and used as int.

As long as you stay inside a program, it's fine. Prtability isue occur when
data are exchanged with other programs or other machines using streams.

--
-hs- emdel at noos.fr "support Afghans against Taleban"
"Car les bandits qui sont cause des guerres
 N'en meurent jamais, on n'tue qu'les innocents."
Gaston Monthus -- La Butte Rouge



Wed, 21 Apr 2004 20:59:42 GMT  
 int <-> char[32]


Quote:
> As long as you stay inside a program, it's fine. Prtability isue occur

Dammned!

 <...> Portability issues occur

--
-hs- emdel at noos.fr "support Afghans against Taleban"
"Car les bandits qui sont cause des guerres
 N'en meurent jamais, on n'tue qu'les innocents."
Gaston Monthus -- La Butte Rouge



Wed, 21 Apr 2004 22:32:30 GMT  
 
 [ 13 post ] 

 Relevant Pages 

1. <<<<<<<Parsing help, please>>>>>>>>

2. map<int,const char*> question

3. map<int,const char*> question

4. Wacky behavior from map<char*, int>

5. sizeof int < 32

6. File Format conversion, ascii freeform -->.csv <-->.wk1<-->dbf<-->?HELP

7. <<<>>>Need C code advice with functions and sorting.<<<>>>

8. <><><>HELP<><><> PCMCIA Motorola Montana 33.6

9. Communication between 16-bit<==>32-bit

10. DDE 16<>32 bit?

11. DDE 16<>32 bit?

12. >>>Windows Service<<<

 

 
Powered by phpBB® Forum Software