Thread: Wierd chars...

  1. #1
    Registered User mikahell's Avatar
    Join Date
    Jun 2006
    Posts
    114

    Wierd chars...

    Hi all! I was making some little application that needed to convert some chars into others, just so a txt file seems unreadable... But I got some reeeallly wierd problem, I can go around it and make things work fine, but I get this strange problem:

    I have in my program an "unsigned char" buffer that I use to read on a file an check to see if the char is equal to something and then I do something with it. Somewhere I have that piece of code:

    Code:
    iFile >> buffer;  //Where iFile is an "ifstream" and "buffer" the char...
    if (buffer == '¤')
        //do something...
    else
        //...
    So there I believe I'm having a compiler error. I expect my program to "do something" when the read buffer is equal to the unsigned char '¤' value, or 164... But it never works. If I use the debugger and check what is "(buffer == '¤')" while buffer is supposed to be 164 (¤), it tells me its true (one would hope), but then the code-cursor don't get to the "do something" line and instead it goes inside the "else"... I think that's totally wierd, I use MSVC++ 2003, and I don't see why something that is true could be seen as false...

    But I found out that if I wrote:
    Code:
    if (buffer == 164)
    //instead of
    if (buffer == '¤')
    it would work... Though these are supposed to be equal (¤ == 164)...

    Any idea why this would do something like that?

  2. #2
    Registered User
    Join Date
    Aug 2005
    Location
    Austria
    Posts
    1,990
    Looks like you editor and the compiler use different character encodings.
    Kurt

  3. #3
    Registered User mikahell's Avatar
    Join Date
    Jun 2006
    Posts
    114
    Well, I believe that I can only type in ASCII codes, because I can't save files that contain other characters from sets like unicode... And the problem is only runtime....

  4. #4
    Just Lurking Dave_Sinkula's Avatar
    Join Date
    Oct 2002
    Posts
    5,005
    And the most important part is again left out -- what is the declaration of buffer? Is it really just an unsigned char? Or an array?
    7. It is easier to write an incorrect program than understand a correct one.
    40. There are two ways to write error-free programs; only the third one works.*

  5. #5
    Registered User mikahell's Avatar
    Join Date
    Jun 2006
    Posts
    114
    Yeah, it's not an array...

  6. #6
    Registered User mikahell's Avatar
    Join Date
    Jun 2006
    Posts
    114
    So is there something that I can do to change the compiler's character set?

  7. #7
    Cat without Hat CornedBee's Avatar
    Join Date
    Apr 2003
    Posts
    8,895
    Doesn't look like it:
    http://msdn2.microsoft.com/en-us/library/19z1t1wy.aspx

    Try using the \xnn escape syntax to get a specific character. Of course, runtime character set issues still play in, but at least it'll do something.
    All the buzzt!
    CornedBee

    "There is not now, nor has there ever been, nor will there ever be, any programming language in which it is the least bit difficult to write bad code."
    - Flon's Law

  8. #8
    (?<!re)tired Mario F.'s Avatar
    Join Date
    May 2006
    Location
    Ireland
    Posts
    8,446
    Not the compiler, no. But you can and should change your editor encoding to match your compiler's.

    I had a similar problem before. The solution is to move to unicode, if you want to use the Windows representation of the extended ASCII codes.
    Originally Posted by brewbuck:
    Reimplementing a large system in another language to get a 25% performance boost is nonsense. It would be cheaper to just get a computer which is 25% faster.

  9. #9
    Registered User mikahell's Avatar
    Join Date
    Jun 2006
    Posts
    114
    Hmm... I tried to change the coding to unicode, but I'm stuck because I realized that there are many types of unicodes I can choose from, but I don't know which one I should use.

    I have something like UTF-8, 65000, 1200, big-endian,... Am I lost?


    Let's say I try to write to the console "←↑→↓" it gets out this way "&#226;&#226;&#226;&#242;&#234;&#226;&#226;&#226;& #226;&#234;&#234;&#244;", (approximatively that way...)
    but the problem is that I had only 4 characters, and it prints out 12 characters...
    Last edited by mikahell; 08-06-2006 at 07:27 PM.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. while condition question..
    By transgalactic2 in forum C Programming
    Replies: 3
    Last Post: 04-04-2009, 04:49 PM
  2. OpenGL Wierd **** - Order of Drawing Stuff?
    By Tonto in forum Game Programming
    Replies: 9
    Last Post: 11-09-2006, 09:56 PM
  3. Counting how many chars in a string
    By tigs in forum C Programming
    Replies: 4
    Last Post: 08-05-2002, 12:25 AM
  4. move chars position
    By SpuRky in forum C Programming
    Replies: 3
    Last Post: 06-09-2002, 02:59 AM
  5. fancy strcpy
    By heat511 in forum C++ Programming
    Replies: 34
    Last Post: 05-01-2002, 04:29 PM