I2OSP and OS2IP

source link: http://bamboojason.blog.sohu.com/62579990.html

  一, I2OSP 是将一非负整数转换为八位串,简单而言就是将一非负整数转换为以 256 为基数的表示方式即可.简单的方法可以先将其转换为二进制串,之后再每八位转换为一个 ASCII 码字符即可.它的介绍如下(RFC3447):

    I2OSP (x, xLen)

    Input:
          x nonnegative integer to be converted
          xLen intended length of the resulting octet string

    Output:
          X corresponding octet string of length xLen    

    Error: "integer too large"

    Steps:
    1. If x >= 256^xLen, output "integer too large" and stop.
    2. Write the integer x in its unique xLen-digit representation in base 256:
        x = x_(xLen-1) 256^(xLen-1) + x_(xLen-2) 256^(xLen-2) + ...+ x_1 256 + x_0,
        where 0 <= x_i < 256 (note that one or more leading digits will be zero if x is less than 256^(xLen-1)).
    3. Let the octet X_i have the integer value x_(xLen-i) for 1 <= i <= xLen. Output the octet string   X = X_1 X_2 ... X_xLen.

    2, 相反 OS2IP 则是将一八位串转换为一非负整数,它的描述如下(RFC3447):

    OS2IP (X)

    Input:
          X octet string to be converted

    Output:
          x corresponding nonnegative integer

    Steps:
    1. Let X_1 X_2 ... X_xLen be the octets of X from first to last, and let x_(xLen-i) be the integer value of the octet X_i for 1 <= i <= xLen.
    2. Let x = x_(xLen-1) 256^(xLen-1) + x_(xLen-2) 256^(xLen-2) + ...+ x_1 256 + x_0.
    3. Output x.

你可能感兴趣的:(String,OS,Integer,less,input,output)