简体   繁体   中英

BigInteger to byte array, with size of bigint at the beginning of array

I want to write a BigInteger to a file, but I want to prepend it with the lenght of the integer, so I know how long it is when I read it again (the file wih have several bigints)

I know I can convert it to a byte array using bigInteger.toByteArray() , but then to prepend this array with its length the only way I can think of is to create another array and copy it. I don't want to do that since it will basically double the time I would take to write the file, and waste time and memory.

Is there any other, more efficient method?

Edit: Looking at the comments - I'm doing this because I want to put multiple bigints in a single file, and so I can use the length as delimiter of sorts.

You don't have to prepend anything to the array. Write the length first, and the array right after that.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM