944,030 Members | Top Members by Rank

Ad:
  • C Discussion Thread
  • Unsolved
  • Views: 2307
  • C RSS
Dec 29th, 2005
0

C preprocessor

Expand Post »
I would like to create a macro to convert a variable string into the UNICODE (16 bits).
Example:
#define UNICODE ("xyz") - It will return 0,'x',0,'y',0, z at compilation time.

The size of parameter to the macro can be any length.

Thanks for helping
Similar Threads
Reputation Points: 10
Solved Threads: 0
Newbie Poster
iamlearning is offline Offline
4 posts
since Dec 2005
Dec 29th, 2005
0

Re: C preprocessor

Something like this?
  1. #include <stdio.h>
  2. #include <wchar.h>
  3.  
  4. #define UNICODE(x) L#x
  5.  
  6. int main ( void )
  7. {
  8. wchar_t *p = UNICODE ( xyz\n );
  9.  
  10. fputws ( p, stdout );
  11.  
  12. return 0;
  13. }
By the way, Unicode is only an encoding, not a storage specification. The actual storage is with multi-byte characters that happen to represent Unicode codes. Technically, you can use Unicode with single byte characters as long as you restrict yourself to the single byte Unicode values.
Administrator
Reputation Points: 6442
Solved Threads: 1393
Bad Cop
Narue is offline Offline
11,807 posts
since Sep 2004
Dec 30th, 2005
0

Re: C preprocessor

Quote originally posted by Narue ...
Something like this?
  1. #include <stdio.h>
  2. #include <wchar.h>
  3.  
  4. #define UNICODE(x) L#x
  5.  
  6. int main ( void )
  7. {
  8. wchar_t *p = UNICODE ( xyz\n );
  9.  
  10. fputws ( p, stdout );
  11.  
  12. return 0;
  13. }
By the way, Unicode is only an encoding, not a storage specification. The actual storage is with multi-byte characters that happen to represent Unicode codes. Technically, you can use Unicode with single byte characters as long as you restrict yourself to the single byte Unicode values.
This is for the USB message which require 16-bit Unicode character. We can always do this in the function but I wonder if we can do it in the macro that will have the message at compilation so it will run faster.

Thanks a lot for your post.
Reputation Points: 10
Solved Threads: 0
Newbie Poster
iamlearning is offline Offline
4 posts
since Dec 2005
Dec 30th, 2005
0

Re: C preprocessor

you can use that macro anywhere you use a string literal. You can't, however, use that macro with a char*
  1. // this will NOT work
  2. char* hello = "Hello World";
  3. wchar_t thello = UNICODE(hello);
Sponsor
Team Colleague
Featured Poster
Reputation Points: 5608
Solved Threads: 2282
Retired and Enjoying Life
Ancient Dragon is offline Offline
21,953 posts
since Aug 2005
Dec 30th, 2005
0

Re: C preprocessor

Quote originally posted by Ancient Dragon ...
you can use that macro anywhere you use a string literal. You can't, however, use that macro with a char*
  1. // this will NOT work
  2. char* hello = "Hello World";
  3. wchar_t thello = UNICODE(hello);
Your point being? There was never any assumption that the macro would be used with narrow string variables, unless you were reading a few posts that I conveniently missed. Or are you just posting pointless fluff to make yourself look smart when you can't find anything meaningful to add?
Administrator
Reputation Points: 6442
Solved Threads: 1393
Bad Cop
Narue is offline Offline
11,807 posts
since Sep 2004
Dec 31st, 2005
0

Re: C preprocessor

Quote originally posted by Narue ...
Your point being? There was never any assumption that the macro would be used with narrow string variables, unless you were reading a few posts that I conveniently missed. Or are you just posting pointless fluff to make yourself look smart when you can't find anything meaningful to add?
getting pretty snotty now aren't you or are you suffering from pms today :mrgreen:

you might (or might not) be supprised at the number of people I've see try to do what I mentioned, so its not an unreasonable thing to warn people about.
Sponsor
Team Colleague
Featured Poster
Reputation Points: 5608
Solved Threads: 2282
Retired and Enjoying Life
Ancient Dragon is offline Offline
21,953 posts
since Aug 2005
Dec 31st, 2005
4

Re: C preprocessor

Unicode is not an encoding, the actual storage specification is the character encoding. Unicode is more accurately described a coded character set.
Team Colleague
Reputation Points: 1135
Solved Threads: 172
Super Senior Demiposter
Rashakil Fol is offline Offline
2,479 posts
since Jun 2005
Dec 31st, 2005
0

Re: C preprocessor

Quote ...
are you suffering from pms today
Isn't that like the eight wonder of the natural world, for us men anyway? He he.

Don't worry Narue the New Year may be more hopeful.

God bless everyone.
Featured Poster
Reputation Points: 1536
Solved Threads: 431
Posting Expert
iamthwee is offline Offline
5,865 posts
since Aug 2005
Dec 31st, 2005
0

Re: C preprocessor

>getting pretty snotty now aren't you or are you suffering from pms today :mrgreen:
That was pretty sharp-tongued, even for me. I was suffering from arrogant forumgoers trying to show off their brilliance by incorrectly "correcting" me with meaningless crap. But you wouldn't do that...more than once.

>you might (or might not) be supprised
I'm very surprised, actually, since it's immediately obvious what the macro does and that it only works with string literals. Though I shouldn't be surprised, with the rampant idiocy of many programmers. :rolleyes:

>Unicode is not an encoding
From dictionary.com:
Quote ...
encoding: Computer Science. To format (electronic data) according to a standard format.
From unicode.org (emphasis is mine):
Quote ...
Q: What is Unicode?
A: Unicode is the universal character encoding, maintained by the Unicode Consortium. This encoding standard provides the basis for processing, storage and interchange of text data in any language in all modern software and information technology protocols.
Now, it's entirely possible that the Unicode Consortium doesn't know what encoding means, or hired someone to write their FAQ who doesn't know what encoding means, or you're being far too pedantic and making mistakes in the process.

How about we meet halfway and call Unicode a code table? I respect you too much to flame you out of existence.
Administrator
Reputation Points: 6442
Solved Threads: 1393
Bad Cop
Narue is offline Offline
11,807 posts
since Sep 2004
Jan 1st, 2006
2

Re: C preprocessor

I don't think so. What is generally called a character encoding, they call a Character Encoding Scheme. See http://www.unicode.org/glossary/. And their definition of Coded Character Set would characterize Unicode nicely. Their glossary doesn't give a definition of character encoding, probably because they've avoided using the term because it's sometimes used in different ways. I would not call a numbering of a series of characters an encoding.

To say that "Unicode is just an encoding" is misleading, because "character encoding" normally refers to the storage specification, such as UTF-8 or UTF-16BE. This is how the term is usually used, and their FAQ goes against the norm. (I'm basing what I call the 'norm' from looking at the View > 'Character Encoding' menu in Firefox, the 'Encoding' menu in I.E., and just about every link I've looked at in a Google search for 'unicode character encoding'.)
Team Colleague
Reputation Points: 1135
Solved Threads: 172
Super Senior Demiposter
Rashakil Fol is offline Offline
2,479 posts
since Jun 2005

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in C Forum Timeline: remove method linked list
Next Thread in C Forum Timeline: mock test question, can anyone help





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC