Saving bytes of int in binary file | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 2

Saving bytes of int in binary file

Hi! I have 38 digits long int. And i wants to save that integer in a BINARY file using C program. I use "putw" function to store int to file. This function works well with small int but it is overflowing with 38 digits long int. As we know that storing very long digits is not any problem in PYTHON. I'm using this in PYTHON to store int in BINARY file: file.write((very_long_int).to_bytes(16, byteorder="small", signed=False)) And in C I'm using putw(very_long_int, file) The problem in C is that not any data type can store that large number. Can anyone help me ?? Please.

23rd Mar 2021, 9:44 AM
🌀 Shail Murtaza شعیل مرتضیٰ
🌀 Shail Murtaza شعیل مرتضیٰ - avatar
38 Answers
+ 3
🌀 Shail Murtaza شعیل مرتضیٰ wrote: "Flash If this discussion was that simple then you really think that it will be that long ??" I don't know well C, but I don't think your problem is quite difficult by itself... I believe that your question is too unclear, and that's the primarly cause of the numerous posts here, not the problem itself ^^ what is exactly your problem? title seems to talk about only writting bytes in files, while description suggest you're rather stuck at converting your big number string / digits array to big endian order... in former case, the first Flash answer would be enough to solve it because he's iterating an array to write bytes instead of char in a file (opened in binary rather than in text mode produce issue by stopping writting when a null byte is encountered), even if he doesn't convert your big number to big endian bytes... [post char limit reached: to be continued]
26th Mar 2021, 12:59 PM
visph
visph - avatar
+ 1
Coder Kitten Thanks! I'm currently using ghex to read file
23rd Mar 2021, 2:38 PM
🌀 Shail Murtaza شعیل مرتضیٰ
🌀 Shail Murtaza شعیل مرتضیٰ - avatar
+ 1
Coder Kitten Now I'm trying to write file using fputs("\x62\x04\x00\x00"); 62 4 are written to file but I'm unable to write those last two zeros. Can you help me in this Edit: I was able to do that using fputc I guess \x00 was representing null character, that's why fputs wasn't working. 😃😃😃
23rd Mar 2021, 3:52 PM
🌀 Shail Murtaza شعیل مرتضیٰ
🌀 Shail Murtaza شعیل مرتضیٰ - avatar
+ 1
you could try these files. beware they won’t run on SoloLearn as they expect cmd args. the C file reads a ‘big’ int data and writes in a binary file (.bin for example). the PY file reads the binary file and displays the integer representation of that data. the input num of C file and output of PY should be identical. Minimal error check is done and I tested on a Linux machine. hope this gives some ideas. https://code.sololearn.com/c4A204A13a0a/?ref=app https://code.sololearn.com/ca13A4A6A7A0/?ref=app
25th Mar 2021, 2:51 AM
Flash
+ 1
🌀 Shail Murtaza شعیل مرتضیٰ, actually binary number system is similar to decimal: just different base (2 instead of 10). 1122 in binary is 10001100010. If divide into bytes, then we get 100 01100010 = 00000100 01100010. In hexadecimal it will be 462 = 04 62. And, I think, Flash gave Python code because Python lets the number be as long as needed.
25th Mar 2021, 6:32 PM
#0009e7 [get]
#0009e7 [get] - avatar
+ 1
get I was saying to flash that his program is not actually saving int in file but characters like 1,2,3,4. I already know that characters are represented by int in C. I hope that you will understand. This is hard for me to explain
25th Mar 2021, 7:02 PM
🌀 Shail Murtaza شعیل مرتضیٰ
🌀 Shail Murtaza شعیل مرتضیٰ - avatar
+ 1
[answer 2nd part] in last case, the problem is not as hard as it could seems, as I am able to convert 38 digits decimal int string to bytes (hex) by spending some minutes to the logic and some hours to get the skills needed to implement it ;P anyway, I will not post it now, without knowing if that's the answer you're looking for, as your numerous figthing posts with Flash doesn't make us want trying to help without first asking for clarification ^^
26th Mar 2021, 1:00 PM
visph
visph - avatar
+ 1
🌀 Shail Murtaza شعیل مرتضیٰ as you do also... why doesn't matter... you're already posted two posts to claim your rights, but doesn't try to clarify your question or even answer my doubt about what you're asking: I will keep my code private, and pass to something else so ^^
26th Mar 2021, 1:15 PM
visph
visph - avatar
0
Coder Kitten It is not. I'm just giving directly argument to putw function. But it's overflowing.
23rd Mar 2021, 11:14 AM
🌀 Shail Murtaza شعیل مرتضیٰ
🌀 Shail Murtaza شعیل مرتضیٰ - avatar
0
Coder Kitten I can save each digit individualy in array. But problem is that how I can save that number in BINARY file. Because putw function first convert number in hex form and then save it.
23rd Mar 2021, 11:30 AM
🌀 Shail Murtaza شعیل مرتضیٰ
🌀 Shail Murtaza شعیل مرتضیٰ - avatar
0
Coder Kitten As array of integers
23rd Mar 2021, 11:44 AM
🌀 Shail Murtaza شعیل مرتضیٰ
🌀 Shail Murtaza شعیل مرتضیٰ - avatar
0
Coder Kitten I'm trying to modify a BINARY file. I can't store numbers directly in it like 16 First I need to convert that number to bytes i.e., \x00\x10
23rd Mar 2021, 11:50 AM
🌀 Shail Murtaza شعیل مرتضیٰ
🌀 Shail Murtaza شعیل مرتضیٰ - avatar
0
Coder Kitten I doesn't know much about these things. I just want to save integer in a file. For example if I will save "1122" in file then file will have "62 04 00 00" I can save small integers but can't large.
23rd Mar 2021, 2:21 PM
🌀 Shail Murtaza شعیل مرتضیٰ
🌀 Shail Murtaza شعیل مرتضیٰ - avatar
0
Flash Thanks for help. 😄😄😄 But this is not what I want. Your program is just writing characters in file not integer. This is very easy.
25th Mar 2021, 5:50 PM
🌀 Shail Murtaza شعیل مرتضیٰ
🌀 Shail Murtaza شعیل مرتضیٰ - avatar
0
🌀 Shail Murtaza شعیل مرتضیٰ I think you should check. it writes bytes, not characters. besides C can’t have that long int in any integral datatype. I also posted a PY file. you can verify there if its a character or byte.
25th Mar 2021, 6:19 PM
Flash
0
Flash You're just reading characters from file and converting them to integer using PYTHON. Which is also too easy.
25th Mar 2021, 6:21 PM
🌀 Shail Murtaza شعیل مرتضیٰ
🌀 Shail Murtaza شعیل مرتضیٰ - avatar
0
no point in arguing if you don’t get the basics right. anyway. nevermind
25th Mar 2021, 6:24 PM
Flash
0
Flash You really don't know about BINARY, do you. if you want to save 1122 in your file then hex representation of that binary will be 0x62 0x04 0x00 0x00
25th Mar 2021, 6:26 PM
🌀 Shail Murtaza شعیل مرتضیٰ
🌀 Shail Murtaza شعیل مرتضیٰ - avatar
0
Flash When you will save int to file then series of hexadecimal numbers will represent one integer. When you will save character then one hexadecimal number will represent one character. If I will save 1234 with your program then I will get 31 32 33 34 as hex in file each number is individual
25th Mar 2021, 6:34 PM
🌀 Shail Murtaza شعیل مرتضیٰ
🌀 Shail Murtaza شعیل مرتضیٰ - avatar
0
🌀 Shail Murtaza شعیل مرتضیٰ, 1. Character (as <char> date type in C and C++) is actually just a 8-bit integer, and compilers just give it special behaviour when inputting and outputting, not like other integer data types (<int>, <long int>, <short int>, <unsigned int>, etc.). 2. It will be the same effect. Binary is just a series of 0's and 1's, as decimal also includes 3's, 4's, ... upto 9's. And it is up to the programmer how the program will process this binary data.
25th Mar 2021, 6:40 PM
#0009e7 [get]
#0009e7 [get] - avatar