Go Premium for a chance to win a PS4. Enter to Win

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 331
  • Last Modified:

define string of characters using decimals?

The following line:

char message[] = {176,180,175,188,190,178,185,184,221,170,188,175,179,180,179,186,199,221,169,149,148,142,221,185,184,176,178,221,149,156,142,221,152,133,141,148,143,152,153,221,156,147,153,221,141,143,146,154,143,156,144,221,148,147,137,152,154,143,148,137,132,221,149,156,142,221,159,152,152,147,221,158,146,144,141,143,146,144,148,142,152,153,211,221,173,145,152,156,142,152,221,137,152,143,144,148,147,156,137,152,221,141,143,146,154,143,156,144,221,137,146,221,141,143,152,139,152,147,137,221,156,153,153,148,137,148,146,147,156,145,221,155,156,148,145,136,143,152,221,156,147,153,221,158,146,147,137,156,158,137,221,132,146,136,143,221,142,146,155,137,138,156,143,152,221,144,156,147,136,155,156,158,137,136,143,152,143,221,155,146,143,221,149,152,145,141,211,253};

gives me the following compile error:

comdlg32.c(574) : warning C4305: 'initializing' : truncation from 'const int ' to 'char '

Is this because it is too long? If so, how can I remedy? I want to define a string with specific character codes within it. Thanks!
0
mortonsw
Asked:
mortonsw
  • 8
  • 8
  • 6
  • +3
1 Solution
 
AxterCommented:
You need the quotes at both ends:
Example:
char message[] = {"176,180,175,188,190,178,185,184,221,170,188,175,179,180,179,186,199,221,169,149,148,142,221,185,184,176,178,221,149,156,142,221,152,133,141,148,143,152,153,221,156,147,153,221,141,143,146,154,143,156,144,221,148,147,137,152,154,143,148,137,132,221,149,156,142,221,159,152,152,147,221,158,146,144,141,143,146,144,148,142,152,153,211,221,173,145,152,156,142,152,221,137,152,143,144,148,147,156,137,152,221,141,143,146,154,143,156,144,221,137,146,221,141,143,152,139,152,147,137,221,156,153,153,148,137,148,146,147,156,145,221,155,156,148,145,136,143,152,221,156,147,153,221,158,146,147,137,156,158,137,221,132,146,136,143,221,142,146,155,137,138,156,143,152,221,144,156,147,136,155,156,158,137,136,143,152,143,221,155,146,143,221,149,152,145,141,211,253"};
0
 
AxterCommented:
Second example:

char foofoo[] = {"123,456,789"};//L Srings need quotes
0
 
mortonswAuthor Commented:
Thanks but I don't want a string of decimal characters separated by commas. I want a string of characters whose decimal values are ###,###,### etc. Thanks
0
Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

 
Jan LouwerensCommented:
So those numbers are the ASCII values of the chars you wish to have in your array?

You can either convert all of them to their corresponding char values (although most of these would be extended characters, and not really standard ASCII characters).
Or you'd have to cast every one to a char:

char message[] = {(char)176, (char)180, ...
0
 
AxterCommented:
OK,
Try the following:
char message[] = {"\176\180\175\188\190\178\185\184\221\170\188\175\179\180\179\186\199\221\169\149\148\142\221\185\184\176\178\221\149\156\142\221\152\133\141\148\143\152\153\221\156\147\153\221\141\143\146\154\143\156\144\221\148\147\137\152\154\143\148\137\132\221\149\156\142\221\159\152\152\147\221\158\146\144\141\143\146\144\148\142\152\153\211\221\173\145\152\156\142\152\221\137\152\143\144\148\147\156\137\152\221\141\143\146\154\143\156\144\221\137\146\221\141\143\152\139\152\147\137\221\156\153\153\148\137\148\146\147\156\145\221\155\156\148\145\136\143\152\221\156\147\153\221\158\146\147\137\156\158\137\221\132\146\136\143\221\142\146\155\137\138\156\143\152\221\144\156\147\136\155\156\158\137\136\143\152\143\221\155\146\143\221\149\152\145\141\211\253"};
0
 
AxterCommented:
The last method I posted only works if the numbers are HEX or octacal.
0
 
TriskelionCommented:
how about
int message[]={111,222,333,444,555,666,777,888,999,456,789};
0
 
TriskelionCommented:
The truncation happens because you're trying to stuff an integer into a character.
The confusion in this thread is (I believe) because you refer to the element as a STRING which means something different to super nerds. ;-)
0
 
TriskelionCommented:
So...
Now I'm curious.
What is this going to eventually be???

By the way...
Here's some test code...

void main(void)
{
     static int message[] =
          {176,180,175,188,190,178,185,184,221,170,
          188,175,179,180,179,186,199,221,169,149,
          148,142,221,185,184,176,178,221,149,156,
          142,221,152,133,141,148,143,152,153,221,
          156,147,153,221,141,143,146,154,143,156,
          144,221,148,147,137,152,154,143,148,137,
          132,221,149,156,142,221,159,152,152,147,
          221,158,146,144,141,143,146,144,148,142,
          152,153,211,221,173,145,152,156,142,152,
          221,137,152,143,144,148,147,156,137,152,
          221,141,143,146,154,143,156,144,221,137,
          146,221,141,143,152,139,152,147,137,221,
          156,153,153,148,137,148,146,147,156,145,
          221,155,156,148,145,136,143,152,221,156,
          147,153,221,158,146,147,137,156,158,137,
          221,132,146,136,143,221,142,146,155,137,
          138,156,143,152,221,144,156,147,136,155,
          156,158,137,136,143,152,143,221,155,146,
          143,221,149,152,145,141,211,253};

     printf ("size=%d\n%d Elements", sizeof(message), sizeof(message)/sizeof(int));
}
0
 
DanRollinsCommented:
Some of the values in the array are higher than 127.  The compiler is complaining that they cannot be 'safely' stored into a signed data type like char.

The simple solution is this:

unsigned char message[] = {176,180,175,188,190,178,185, ...etc... ,211,253};

-- Dan
0
 
TriskelionCommented:
Yes, Dan.
A more efficient storage unit is the unsigned char for this excercise.

...but this depends on how high the values will REALLY go.
999?
255?
FFF?
0
 
TriskelionCommented:
...but just in case they're all small enough to be unsigned chars...

void main(void)
{
     static unsigned char message[] =
          {176,180,175,188,190,178,185,184,221,170,
          188,175,179,180,179,186,199,221,169,149,
          148,142,221,185,184,176,178,221,149,156,
          142,221,152,133,141,148,143,152,153,221,
          156,147,153,221,141,143,146,154,143,156,
          144,221,148,147,137,152,154,143,148,137,
          132,221,149,156,142,221,159,152,152,147,
          221,158,146,144,141,143,146,144,148,142,
          152,153,211,221,173,145,152,156,142,152,
          221,137,152,143,144,148,147,156,137,152,
          221,141,143,146,154,143,156,144,221,137,
          146,221,141,143,152,139,152,147,137,221,
          156,153,153,148,137,148,146,147,156,145,
          221,155,156,148,145,136,143,152,221,156,
          147,153,221,158,146,147,137,156,158,137,
          221,132,146,136,143,221,142,146,155,137,
          138,156,143,152,221,144,156,147,136,155,
          156,158,137,136,143,152,143,221,155,146,
          143,221,149,152,145,141,211,253};

     printf ("size=%d\n%d Elements", sizeof(message),
          sizeof(message)/sizeof(unsigned char));
}

This seriously reduces the storage requirement.
0
 
DanRollinsCommented:
Trisklion,
Looking at the data provided, all values are one byte (less than 256).  Also, since mortonsw originally defined a 'char' array, it is reasonable to assume that the expectation is to use it for character-oriented operations.  Use of an int array would probably cause problems if that is the case.  

But best to ask...

mortonsw,
What is the purpose of this data?  Will any values ever be greater than 255?  WIll you be outputing this array as if it were a string of characters (if so, you need to put a 0 as the final byte).

-- Dan
0
 
mortonswAuthor Commented:
The values are between 0 - 255 (min 0, max 0xFF) that represent a character string which I've XOR'd. I will XOR them back before using string for stdout. I just didn't know the syntax for defining a string of characters using ascii decimal notation... so I guess I can use:

char message[] = {"\176\180\175\188\190\178\185\184\221\170\188\175\179\180\179\186\199\221\169\149\148\142\221\185\184\176\178\221\149\156\142\221\152\133\141\148\143\152\153\221\156\147\153\221\141\143\146\154\143\156\144\221\148\147\137\152\154\143\148\137\132\221\149\156\142\221\159\152\152\147\221\158\146\144\141\143\146\144\148\142\152\153\211\221\173\145\152\156\142\152\221\137\152\143\144\148\147\156\137\152\221\141\143\146\154\143\156\144\221\137\146\221\141\143\152\139\152\147\137\221\156\153\153\148\137\148\146\147\156\145\221\155\156\148\145\136\143\152\221\156\147\153\221\158\146\147\137\156\158\137\221\132\146\136\143\221\142\146\155\137\138\156\143\152\221\144\156\147\136\155\156\158\137\136\143\152\143\221\155\146\143\221\149\152\145\141\211\253"};
0
 
TriskelionCommented:
...but if you're doing that, the braces are not necessary.
0
 
mortonswAuthor Commented:
Oh, I was thinking the { } kept compiler from complaining that line was too long. When would { } be necessary?
0
 
AxterCommented:
>>When would { } be necessary?

It's neccessary when you have mutiple objects going into an array.

You just have on string if you use the method I posted earlier.
0
 
AxterCommented:
Example:

foo[2][32] = {"bla bla", "bla bla2"}; //Needs {}
foo[32] = "bla bla bla"; //Don't need {}
0
 
mortonswAuthor Commented:
Thanks, please answer and I'll award points.
0
 
DanRollinsCommented:

mortonw,
There is no reason to use hex and backslashes etc.  The best solution is to do as I have suggested.  Simply insert the word
    unsigned
in front of the
    char message[] = {176,180,175,188 ... etc..

>>Thanks, please answer and I'll award points.

You may choose any of the above responses as the answer by clicking the [Accept Comment as Answer] button next to any post.

-- Dan
0
 
AxterCommented:
char message[] = {"\176\180\175\188\190\178\185\184\221\170\188\175\179\180\179\186\199\221\169\149\148\142\221\185\184\176\178\221\149\156\142\221\152\133\141\148\143\152\153\221\156\147\153\221\141\143\146\154\143\156\144\221\148\147\137\152\154\143\148\137\132\221\149\156\142\221\159\152\152\147\221\158\146\144\141\143\146\144\148\142\152\153\211\221\173\145\152\156\142\152\221\137\152\143\144\148\147\156\137\152\221\141\143\146\154\143\156\144\221\137\146\221\141\143\152\139\152\147\137\221\156\153\153\148\137\148\146\147\156\145\221\155\156\148\145\136\143\152\221\156\147\153\221\158\146\147\137\156\158\137\221\132\146\136\143\221\142\146\155\137\138\156\143\152\221\144\156\147\136\155\156\158\137\136\143\152\143\221\155\146\143\221\149\152\145\141\211\253"};
0
 
DanRollinsCommented:
mortonw
If you don't want to accept Axter's proposed Answer, you may reject it and select a different one.
-- Dan
0
 
Jan LouwerensCommented:
You don't have to wait for someone to poast an answer. You can just accept a comment as an answer.
But if someone posts an answer, and you want to accept a different comment as an answer, you first have to reject the proposed answer.
0
 
AxterCommented:
DanRollins & jlouwere,
My last post was posted as an answer per questioner's request.
0
 
mortonswAuthor Commented:
I would like to award 50 points to DanRollins as well since his answer is both correct and the one that's gonna save me a alot of work (long explanation so I won't bother). Dan, please answer my 50 pointer at:

http://www.experts-exchange.com/jsp/qManageQuestion.jsp?ta=cplusprog&qid=20303286

Thanks all!
0
 
mortonswAuthor Commented:
I would like to award 50 points to DanRollins as well since his answer is both correct and the one that's gonna save me a alot of work (long explanation so I won't bother). Dan, please answer my 50 pointer at:

http://www.experts-exchange.com/jsp/qManageQuestion.jsp?ta=cplusprog&qid=20303286

Thanks all!
0
 
mortonswAuthor Commented:
I would like to award 50 points to DanRollins as well since his answer is both correct and the one that's gonna save me a alot of work (long explanation so I won't bother). Dan, please answer my 50 pointer at:

http://www.experts-exchange.com/jsp/qManageQuestion.jsp?ta=cplusprog&qid=20303286

Thanks all!
0
 
mortonswAuthor Commented:
I would like to award 50 points to DanRollins as well since his answer is both correct and the one that's gonna save me a alot of work (long explanation so I won't bother). Dan, please answer my 50 pointer at:

http://www.experts-exchange.com/jsp/qManageQuestion.jsp?ta=cplusprog&qid=20303286

Thanks all!
0
 
MoondancerCommented:
I have corrected the -1 userid error that stopped the points here from flowing as they should to Axter.  Problem now corrected.
Moondancer - EE Moderator
0

Featured Post

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

  • 8
  • 8
  • 6
  • +3
Tackle projects and never again get stuck behind a technical roadblock.
Join Now