开发者

Is it feasible to stream hexadecimal digits and output the stream in decimal without waiting for all the data to appear

Say you have a file containing gigabytes worth of

deadbeefdeadbeefdeadbeefdeadbeefdeadbeefdeadbeef

and you want to convert it to decimal, in the same way as you would 0xdeadbeef to 3,735,928,559.

Bases whose number is divisible by 2 are easy to do this with since you can operate on every few bits and output a number (6 for base64).

Any way to do this with decimal?

EDIT: The file represents one long number. The开发者_JS百科 fact that it repeats itself is no different than the number 55,055,055,055.

EDIT2: The size of the file is known. What then?


You can usually start writing before you're done reading the input. But if the number is very close to a power of ten, you might have to read more than half of the input before you can write the first digit of the output!

To see why, take a relatively small example. Suppose the number is 1060. The hex encoding for this is 50 hex digits. After reading the first 34 digits, you know this much:

9f4f2726179a224501d762422c946590d9................

The dots are the digits you haven't read yet.

At this point, you still can't write the first digit of the output, because the input could be anything from

9f4f2726179a224501d762422c946590d90000000000000000

to

9f4f2726179a224501d762422c946590d9ffffffffffffffff

And the former is decimal 999999999999999999999999999999999999999998847078495393153024, but the latter is 1000000000000000000000000000000000000000017293822569102704639. So you still don't know whether to write a 1 or a 9! Not until the 35th input digit can you start writing the output.

Generally, you'll have to read about three quarters of the input before writing the first output digit, in the worst case.


Wrong, wrong wrong. See comments.

Yes, but you have to work backwards (i.e. start from the end of the file).

  1. Read the last digit.
  2. Convert the digit to decimal, print it and store the most significant digit.
  3. Read the next digit to the left.
  4. Add the previous most significant digit, convert to decimal, print it and store the new most significant digit.
  5. Repeat from step 3.

Edit: Added where the digits are printed. This produces the output decimal number in reverse (least significant digit first) order.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜