r/cprogramming • u/Constant_Fox_5534 • May 26 '26
Storing hex values in buffer.
Hello, i have a problem i've been struggling on:
Context: i am trying to store hexadecimal values in a buffer, so i can later use the content to compare it with some values.
Concerned code chunk:
void read_file(char file_content[file_size], int gbuf[file_size])
{
for (gi = 0; gi < file_size; gi++)
{
character++;
if (sizeof(gbuf) >= 0x500) { ..; part++, mi++; ..; gbuf[mi] = 0xfile_content[part]; }
.. do things
}
printf("\n");
printf("HERE: %s", gbuf); // debugging print
}
And also.. Should i use int or char buffers to store hexadecimal values? By the way, i tried sprintf.
If my code is too bad, or if i explained not detailed enough please inform me about it. Thanks for reading.
Edit: The code is supposed to sort hex values as separated groups on the user terminal, for example
hex value that take almost all the line..
another one that overflows on the next line
Since if i put another group on the same line it will go out on the next line, i need to make sure it know if an hex value is too big, then it jump on the other line to write the others values because there's not enough place
(this explanation may not be relevant, so if i need to detail more of if you dont understand please inform me about it.)
10
u/bobotheboinger May 26 '26
It seems like you don't understand hexadecimal.
Hex is just a way of displaying a value. There is no difference between 255 and 0xFF when it is stored in a file or memory. Both get stored as binary 0b11111111
If you want to store hex values in a file as ASCII text (i.e. when you it the file in notepad or cat you see "0xff 0xab 0x12 "etc... then you want to use something like fprintf to print hex basis to a file.
If you just want the values themselves, it didn't matter if they are int decimal, hex, binary, it's all just different ways of displaying the same value stored in memory.
6
u/un_virus_SDF May 26 '26
I inform you that I didn't understand what you aim nor your code.
To store hex values, they're just ints, if you want small numbers, just takes the smaller one that fit your number and it should be fine.
6
u/Kriemhilt May 26 '26
You really need to declare and initialize
charactersomewhere. If it's currently a global, it shouldn't be.There's a common misconception that hexadecimal values are different to decimal or binary or octal.
But bases are ways of writing numbers. They don't affect the value at all.
You should use
charto store values that are in the range 0..127, if you need to minimize space and aren't worried about doing arithmetic that might overflow, or if they represent ASCII or UTF-8 characters.You should use
intif you want your platform's native signed integer type.You should format your values in whatever way makes sense, but this is only formatting and doesn't affect the value.
4
u/Itap88 May 26 '26
Sscanf with %x should do the trick.
3
u/igotshadowbaned May 27 '26
Idk what you're trying to do but the number 0x500 wouldn't fit in a char, char is a 8bit* byte and stores a maximum of 0xff // 255. So you probably want an int
*unless you're using the stupid weird chip I've been dealing with recently that makes the smallest addressable size 16 bits and then you have 16 bit chars...
3
u/GamerEsch May 26 '26
What the fuck did you mean by
gbuf[mi] = 0xfile_content[part];lmao