How to avoid that "x0d" is interpreted as a newline character?

116 Views Asked by At

I have to send a file to the mainframe and generate some numeric values in packed decimal format. The problem is that in some cases, the value in the file generates a new line and I need it in the same line. After conversion to a packed decimal, the value -10 becomes "x01x0d". The "x0d" at the end is lost because it was interpreted as a newline character ('\n'). Is it possible to prevent this character from being interpreted as a newline?

I tried to change the encoding to cps500 (in python) instead of utf-8, but it didn't work. I would like to have a tip that allows me to give this information to the mainframe properly.

I made python programa with the example I'm facing. note that every time a call the function "pack_number" passing a negative value and print the result, the line breaks in a new one.

from array import array
from struct import pack

def pack_number(valor, tam, qtdecasasdecimais):
    """ Pack a COMP-3 number. Format: PIC 9(tam)V9(qtdecasasdecimais). """
     # qtde de casas decimais nao pode ser maior que o tamanho do campo
    if qtdecasasdecimais > tam:
        qtdecasasdecimais = tam

    valor *= 10 ** qtdecasasdecimais
    tam_packed = int((tam + 1) / 2) + (tam + 1) % 2

    #print (tam_packed)
    valor = int(valor)

    # Is the number negative?  Remember for later.
    negative = False
    if valor < 0:
        negative = True
        valor *= -1

    # Treat the number as a string.  Makes it easier to loop over.
    n_str = str(valor)
    b = int(n_str[0])

    # For each digit, shift it onto the result.
    for c in n_str[1:]:
        b = (b << 4) | int(c)

    # Make the number negative if needed.
    if negative:
        b = (b << 4) | 0xD
    else:
        b = (b << 4) | 0xc

    # calling pack_number()
    b_packed = pack('>q', b)
    if len(b_packed) > tam_packed:
        b_packed = b_packed[-1 * tam_packed:]

    return b_packed

if __name__ == '__main__':
    with open('arquivo.bin', 'wb') as pdd:
        for linhas in range(1, 11):
            pdd.write(pack_number( linhas, 7, 2))
            pdd.write(pack_number( -1 * linhas, 7, 2))
        pdd.close()
0

There are 0 best solutions below