Solved

define string of characters using decimals?

Posted on 2002-05-20
29
290 Views
Last Modified: 2010-04-02
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
Comment
Question by:mortonsw
  • 8
  • 8
  • 6
  • +3
29 Comments
 
LVL 30

Expert Comment

by:Axter
ID: 7022571
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
 
LVL 30

Expert Comment

by:Axter
ID: 7022573
Second example:

char foofoo[] = {"123,456,789"};//L Srings need quotes
0
 
LVL 1

Author Comment

by:mortonsw
ID: 7022581
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
 
LVL 5

Expert Comment

by:Jan Louwerens
ID: 7022657
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
 
LVL 30

Expert Comment

by:Axter
ID: 7022712
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
 
LVL 30

Expert Comment

by:Axter
ID: 7022722
The last method I posted only works if the numbers are HEX or octacal.
0
 
LVL 6

Expert Comment

by:Triskelion
ID: 7022723
how about
int message[]={111,222,333,444,555,666,777,888,999,456,789};
0
 
LVL 6

Expert Comment

by:Triskelion
ID: 7022730
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
 
LVL 6

Expert Comment

by:Triskelion
ID: 7022747
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
 
LVL 49

Expert Comment

by:DanRollins
ID: 7022781
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
 
LVL 6

Expert Comment

by:Triskelion
ID: 7022806
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
 
LVL 6

Expert Comment

by:Triskelion
ID: 7022833
...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
 
LVL 49

Expert Comment

by:DanRollins
ID: 7022868
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
 
LVL 1

Author Comment

by:mortonsw
ID: 7025022
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
How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

 
LVL 6

Expert Comment

by:Triskelion
ID: 7025035
...but if you're doing that, the braces are not necessary.
0
 
LVL 1

Author Comment

by:mortonsw
ID: 7025075
Oh, I was thinking the { } kept compiler from complaining that line was too long. When would { } be necessary?
0
 
LVL 30

Expert Comment

by:Axter
ID: 7025093
>>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
 
LVL 30

Expert Comment

by:Axter
ID: 7025099
Example:

foo[2][32] = {"bla bla", "bla bla2"}; //Needs {}
foo[32] = "bla bla bla"; //Don't need {}
0
 
LVL 1

Author Comment

by:mortonsw
ID: 7025148
Thanks, please answer and I'll award points.
0
 
LVL 49

Expert Comment

by:DanRollins
ID: 7025185

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
 
LVL 30

Accepted Solution

by:
Axter earned 50 total points
ID: 7025214
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
 
LVL 49

Expert Comment

by:DanRollins
ID: 7025524
mortonw
If you don't want to accept Axter's proposed Answer, you may reject it and select a different one.
-- Dan
0
 
LVL 5

Expert Comment

by:Jan Louwerens
ID: 7025608
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
 
LVL 30

Expert Comment

by:Axter
ID: 7025620
DanRollins & jlouwere,
My last post was posted as an answer per questioner's request.
0
 
LVL 1

Author Comment

by:mortonsw
ID: 7025793
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
 
LVL 1

Author Comment

by:mortonsw
ID: 7025794
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
 
LVL 1

Author Comment

by:mortonsw
ID: 7025795
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
 
LVL 1

Author Comment

by:mortonsw
ID: 7025797
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
 
LVL 1

Expert Comment

by:Moondancer
ID: 7027222
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

Maximize Your Threat Intelligence Reporting

Reporting is one of the most important and least talked about aspects of a world-class threat intelligence program. Here’s how to do it right.

Join & Write a Comment

IntroductionThis article is the second in a three part article series on the Visual Studio 2008 Debugger.  It provides tips in setting and using breakpoints. If not familiar with this debugger, you can find a basic introduction in the EE article loc…
Go is an acronym of golang, is a programming language developed Google in 2007. Go is a new language that is mostly in the C family, with significant input from Pascal/Modula/Oberon family. Hence Go arisen as low-level language with fast compilation…
The viewer will learn additional member functions of the vector class. Specifically, the capacity and swap member functions will be introduced.
The viewer will be introduced to the member functions push_back and pop_back of the vector class. The video will teach the difference between the two as well as how to use each one along with its functionality.

760 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

20 Experts available now in Live!

Get 1:1 Help Now