CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Page 1 of 2 12 LastLast
Results 1 to 15 of 23

Thread: Ansi?

  1. #1
    Join Date
    May 2009
    Posts
    35

    Question Ansi?

    Hi everybody,
    I am working on an encryption program using WinAPi when this question poped up..
    If im opening the source file as binary, and the encryption method is to read characters (8 bit?) from the file encrypt them and put it back, is it possible for program to encrypt and decrypt diffrently over diffrent computer (with diffrent character encoding)?
    If so, how sould I approach this, what sould I use for variables and how can I straightly manipulate bits? Only by x=0x01...?


    Thanks alot for any help!

  2. #2
    Join Date
    Oct 2005
    Location
    England
    Posts
    803

    Re: Ansi?

    I was under the impression that the first 128 ASCII values were standard at least.

    As for bit manipulation you can use the bitwise operators:

    Code:
    char c = '1';
    
    c &= 0x01; //and
    
    c |= 0x01; //or
    
    c ^= 0x01; //xor
    Rich

    Visual Studio 2010 Professional | Windows 7 (x64)
    Ubuntu

  3. #3
    Join Date
    May 2009
    Posts
    35

    Re: Ansi?

    I really dont know, the first 128 (7 bit) are as I understood standard, but when I read a character from a file, does he read 7 bits or a full 8 bits?

    If its reading 8, then the last 128 out of the 256 (the last bit) can change between computers, which can mess up my encryption.. no?

    how can I read a fixed amount of bit out of a file? (in my program I used get(char))..

    and could you please explain what you wrote:

    Code:
    char c = '1';
    c &= 0x01; //and
    c |= 0x01; //or
    c ^= 0x01; //xor


    THANKS

  4. #4
    Join Date
    Oct 2005
    Location
    England
    Posts
    803

    Re: Ansi?

    Quote Originally Posted by Igeru View Post
    I really dont know, the first 128 (7 bit) are as I understood standard, but when I read a character from a file, does he read 7 bits or a full 8 bits?

    If its reading 8, then the last 128 out of the 256 (the last bit) can change between computers, which can mess up my encryption.. no?

    how can I read a fixed amount of bit out of a file? (in my program I used get(char))..

    and could you please explain what you wrote:

    Code:
    char c = '1';
    c &= 0x01; //and
    c |= 0x01; //or
    c ^= 0x01; //xor


    THANKS
    To take the first N bits of a value you need to AND it with an N bit mask. So to obtain the first 7 bits you need to AND it with (1111111) base 2 which is 7F in base 16.

    Code:
    //Assign c some value 127 - 255.
    char c = 209;
    
    //And with the mask to remove the 8th bit.	
    c &= 0x7f;
    
    //Display
    cout << c << '\n';
    The bit you remove might need to change depending on the byte ordering.
    Rich

    Visual Studio 2010 Professional | Windows 7 (x64)
    Ubuntu

  5. #5
    Join Date
    Apr 2009
    Location
    Russia, Nizhny Novgorod
    Posts
    99

    Re: Ansi?

    Igeru, the most architectures I know have a byte equal to octet. And as the standard of C++ states the minimal chunk of date must represents one character. And this is equal to octet in all architectures I've faced. According to above statements you have to read no less then one byte(octet or 8 bits) per reading operation in C++. Now go to your question:
    is it possible for program to encrypt and decrypt diffrently over diffrent computer (with diffrent character encoding)
    The answer is NO. You wouldn't have such a behavior if you have a correct cipher and decipher mechanism which operates with bytes. There is no matter what codepage you have on different computers either it is cp-1251 or marsians technowriting because different codepages impact only for displaying character. They have no impacts on the byte itself.

  6. #6
    Join Date
    May 2009
    Posts
    35

    Re: Ansi?

    First of all, thanks for refering ixSci.

    My program have a good cipher and decipher codes, and I haven't had any problem with my program, I just starting to learn a bit about the character encoding and realized I may have done some big mistakes with my code..

    All that you said if clear, but take this scenario for example-
    my program opens a file, any file (not just text), reads it by characters (8 bit according to you), each character it reads it does NewChar=OldChar+1 , and puts it back to the OldChar original place.
    say I encrypt a file with text "a" on the first computer, where "a"=254 (1111 1110 if not mistaking),
    so the encrypted file contains 1111 1111 (lets say = "b").
    when I want to decrypt the file on another computer, which read the "b" as =222, and then writes some other char which is in the 221 place instead of the original 254="a"..

    Am I totally wrong?

  7. #7
    Join Date
    Apr 2009
    Location
    Russia, Nizhny Novgorod
    Posts
    99

    Re: Ansi?

    Igeru, I think you don't fully understand the byte conception. According to your example. Comp1 encrypt a(1111 1110) to b(1111 1111). Comp2 read this encrypted symbol and value of this symbol is 0xFF(1111 1111) and nothing else. It doesn't matter what codepage this comp2 use. But if we want to dispaly this character "b" on comp2 terminal that have some unexpected codepage then we have a strange(not "b") symbol on the terminal. But on underlying level it is just 0xFF and it can't impact on your decipher mechanism.

  8. #8
    Join Date
    May 2009
    Posts
    35

    Re: Ansi?

    Great, understood, I got a little confused back there..

    So when I read any file, and file.get(char) from it, the char just has the binary value of the 8-bits?
    Then, if Im doing char*8 for example, what will happen?

  9. #9
    Join Date
    Apr 2009
    Location
    Russia, Nizhny Novgorod
    Posts
    99

    Re: Ansi?

    So when I read any file, and file.get(char) from it, the char just has the binary value of the 8-bits?
    Yes, but you should use more appropriate, for binary reading, function - read
    Then, if Im doing char*8 for example, what will happen?
    if the result of operation would fit char then it would be char multiplied by 8. If it doesn't fit char then would be executed overflow-related operations and you get an unexpected result. Overflow is handled with the addition modulo as far as I remember

  10. #10
    Lindley is offline Elite Member Power Poster
    Join Date
    Oct 2007
    Location
    Seattle, WA
    Posts
    10,895

    Re: Ansi?

    http://www.asciitable.com/

    Virtually every 1-byte-char based encoding scheme uses the same meanings for 0-127 as on that page. The differences come in when you start considering the meanings of 128 and above.

    The old-style approach was to have lots and lots of "code pages" which changed the meaning of larger "code points" (as the numeric representation of a character may be called). In order to display a certain symbol, you'd have to know both its code page and code point.

    Unicode changes that. All characters may be represented by unique code points within Unicode. The tricky part is that there are far more than 255 symbols, so some characters take more than one byte to represent. How exactly that representation occurs depends on the encoding. If you use the UTF-8 encoding, then 0-127 are still the same familiar ASCII values, but 128-255 don't have isolated meanings----those are always the leading bytes in multi-byte characters.

    Of course, just to confuse the issue, there are also UTF-16 and UTF-32 which *aren't* backwards-compatible with ASCII. But unless you start working with wchar_ts, don't worry about those.

  11. #11
    Join Date
    May 2009
    Posts
    35

    Re: Ansi?

    ixSci, I didn't fully undertand the second part, if for say I read 0xff which is "a" and multiply it by 8, then the new char would be 255*8=2040? and if its not withing the 256, what happens?!
    purhaps the overflow causes unexpected symbols, but I tested my program hundreds of times and it always works, how is that?

    Lindley, thanks for making it so clear! So the whole or most of the world uses Unicode now?

  12. #12
    Join Date
    Apr 2009
    Location
    Russia, Nizhny Novgorod
    Posts
    99

    Re: Ansi?

    Sorry if I mess you. Unexpected doesn't mean unknown the overflow handling is the known process hence you always have the same result. It's true for one compiler or architecture, I'm not sure what things are responsible for overflow handling. That is why you have the right results on one computer and another result on another computer, you shouldn't expect normal results if you use abnormal overflow process. You should avoid overflow whenever you can it should be warning from compiler about overflow I think.

  13. #13
    Join Date
    May 2009
    Posts
    35

    Re: Ansi?

    Ok then if the function involves many operations (+,-,*,/,^ ... ), if after each of those I'd use the (.....)%256 everything would be restricted to 0-255, and there will be no overflow, right?

  14. #14
    Join Date
    Oct 2005
    Location
    England
    Posts
    803

    Re: Ansi?

    Quote Originally Posted by Igeru View Post
    Ok then if the function involves many operations (+,-,*,/,^ ... ), if after each of those I'd use the (.....)%256 everything would be restricted to 0-255, and there will be no overflow, right?
    If you are doing these operations directly on the char data type then it will not prevent an overflow. % 256 on a data type which can only hold 0-255 will achieve nothing.
    Rich

    Visual Studio 2010 Professional | Windows 7 (x64)
    Ubuntu

  15. #15
    Join Date
    Apr 2009
    Location
    Russia, Nizhny Novgorod
    Posts
    99

    Re: Ansi?

    I don't think so. If your result ciphercharacter doesn't fit char you shouldn't use char. Use larger type for your manipulation instead of char

Page 1 of 2 12 LastLast

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured