BinaryReader与byte [] + shifts

时间:2018-11-15 18:56:56

标签: c# binaryreader

我一定误会了BinaryReader在做什么。为什么这些输出不同?

{
    var data = File.ReadAllBytes(testFile);
    var pos = 0;
    var read8 = new Func<uint>(() => data[pos++]);
    var read32 = new Func<uint>(() => (read8() << 24) | (read8() << 16) | (read8() << 8) | read8());

    Console.WriteLine(read32());
}

using (var reader = new BinaryReader(File.Open(testFile, FileMode.Open)))
{
    Console.WriteLine(reader.ReadUInt32());
}

1 个答案:

答案 0 :(得分:2)

Endiannes

使用:

ALPHABET = 'abcdefghijklmnopqrstuvwxyz'

def Menu():
    print("Please choose from the following: \n")
    print("'e' to encode a string.")
    print("'d' to decode a string.")
    print("'q' to quit.\n")
    choice = input("Please enter one of the letters above.\n")
    if choice == "e":
        print (Encode())
    if choice == "d":
        print (Decode())
    if choice == "q":
        print("The program will now exit.")
        quit()

def stringValidation():
    while True:
        try:
            valid = str(input("Enter a string to encode.\n"))
            return valid
            break
        except:
            print("Value Error. Enter a string with only letters from the alphabet.")
            continue

def shiftValidation():
    while True:
        try:
            valid = int(input("Enter the number of shifts you would like.\n"))
            return valid
            break
        except:
            print("Value Error. Please enter an integer.")

def decodeShiftValidation():
    while True:
        try:
            valid = int(input("Enter the key. (Number of shifts used to encrypt the encoded word.)\n"))
            return valid
            break
        except:
            print("Value Error. Please enter an integer.")


def Encode():
    data = []
    string = stringValidation() # asks the user for the string input to be encoded
    shift = shiftValidation() # asks the user for the number of shifts
    for i in string:        # for the letters in string...
        if i.strip() and i in ALPHABET: # i.strip removes all default whitespace characters from i (string input by user.)
            data.append(ALPHABET[(ALPHABET.index(i) + shift) % 26]) # gets position of the letters from input string in ALPHABET using indexing, and adds the shift to get the new position and new letter.
        else:
            data.append(i) # if it is a space, simply append it to the data.
    output = ''.join(data)
    return output
    encoded_string= Encode()
    print(encoded_string)

def Decode():
    data = []
    string = input("Please enter the string you wish to decode.\n")
    shift = int(input("Enter the key. (Number of shifts used when encoding original word. \n"))
    for i in string:
        if i.strip() and i in ALPHABET:
            data.append(ALPHABET[(ALPHABET.index(i) - shift) % 26])
    else:
        data.append(i)
    output = ''.join(data)
    return output

Menu()

请注意,请勿编写具有此类副作用的代码。
因为the order of evaluation is guaranteed,您正在这里与他们脱身,但请不要这么做。