-2

I'm trying to convert a hex string to an int (as hex still). I have it working, but there is a problem. If I enter the string "00000001" the result is 0x1. However, my function requires the full 8 bit values leading up to the 1. How can this be done?

I did see an example here on StackOverflow, but it's for C# and not C or C++.

Input:

Enter the memory address you want to dump from (EG: 0xABC00000)
address? >> 0x00000001

serial_buffer = 00000001
memory_address = "0x1"

Code:

unsigned int memory_address = 0xABC00000;
static char serial_buffer[30]; // plenty of room

memory_address = strtoul(serial_buffer, NULL, 16); // convert it

printf("serial_buffer = %s", serial_buffer);
printf("memory_address = \"0x%X\" bytes", memory_address);

// this is done in a loop (not part of this example)
serial_buffer[i++] = key_code; // add char into serial_buffer
putchar(key_code); // echo back the typed char

If I comment out memory_address = strtoul(serial_buffer, NULL, 16);, I get the correct value from the printf as memory_address = "0xABC00000".

If I then put memory_address = strtoul(serial_buffer, NULL, 16); back, I get from the printf a value of 0x1 (but I want it as 0x00000001).

t0rxe
  • 95
  • 3
  • 14
  • `printf("memory_address = \"0x%8X\" bytes", memory_address);` – 001 Jan 29 '22 at 23:30
  • 1
    Also, if the C++ tag is not relevant, remove it. – 001 Jan 29 '22 at 23:30
  • Yes, I know how to do that. That's just showing the output though. I need the actual data value corrected. – t0rxe Jan 29 '22 at 23:30
  • 1
    _" If I enter the string "00000001" the result is 0x1. However, my function requires the full 8 bit values leading up to the 1."_ what makes you doubt that there are missing bits? It's just that the leading zeroes aren't shown in the representation. – πάντα ῥεῖ Jan 29 '22 at 23:30
  • @JohnnyMopp C or C++ is fine for me to utilise. Both are arguably interchangeable so I added both tags. I would prefer old school C though so I'll remove the C++ tag to keep the StackOverflow moderators happy. – t0rxe Jan 29 '22 at 23:31
  • @JohnnyMopp sure, I selected C. – t0rxe Jan 29 '22 at 23:32
  • Also are you sure you meant hex input or binary?? – πάντα ῥεῖ Jan 29 '22 at 23:33
  • @πάνταῥεῖ HEXADECIMAL. – t0rxe Jan 29 '22 at 23:35
  • It is unclear what is supposed to be converted to what. In the first panel, the variables look like an integer and a string. In the second panel the same-named variables are the other way round: string and integer. – Weather Vane Jan 29 '22 at 23:38
  • @t0rxe It's not very clear, since you're talking about bits missing, which would be fully covered by 2 digits input, but you're showing 8. Very confusing, please [edit] your question accordingly, and stop yelling at me, OK? – πάντα ῥεῖ Jan 29 '22 at 23:39
  • Re the edit: "if I comment out the line that overwrites `memory_address` it retains its original value." Did you mean `static char serial_buffer[30] = "0xABC00000";`? Do you know what `strtoul()` does? – Weather Vane Jan 29 '22 at 23:45
  • @WeatherVane Yes, it will retain the original value and display it as `0xABC00000`. If I enter my own value as `0x00000001` I will get `0x01` (but I want it as `0x00000001`). – t0rxe Jan 29 '22 at 23:47
  • You are converting `serial_buffer`, printing `serial_buffer` but it hasn't been initialised, or had anything written to it since. None of this makes any sense at all. – Weather Vane Jan 29 '22 at 23:48
  • The code isn't fully complete. I have code later on which takes the keyboard input and adds it to the array. `serial_buffer[i++] = key_code; putchar(key_code);` but that isn't important. – t0rxe Jan 29 '22 at 23:50
  • 1
    In that case, please post a [Minimal Reproducible Example](https://stackoverflow.com/help/minimal-reproducible-example) as text, the shortest *complete* code that shows what you have tried. – Weather Vane Jan 29 '22 at 23:51
  • I believe you are getting the correct value but you want the leading 0s, for that just put "0" for (fill with zeros) and "8" for reserving 8 characters _(resulting format "0x%08X")_ – WENDYN Jan 29 '22 at 23:58
  • @WENDYN that's not the issue. See KamilCuk's response below as they said the same thing. – t0rxe Jan 29 '22 at 23:59

2 Answers2

1

why don't you just use sscanf (not to be confused with scanf) it takes input string, format string and output:

int size;
char buffer[30];
/*...*/
sscanf(buffer, "0x%x", &size);

sscanf info here

formatting here

WENDYN
  • 650
  • 7
  • 15
  • I tried using `sscanf` prior to me opening this question, but it did not work. I will try it again however and report my result. – t0rxe Jan 29 '22 at 23:33
  • This code isn't working for me. It is not putting the value into `memory_address`. This is the code I used `sscanf(serial_buffer, "0x%x", &memory_address);`. When I `printf` the value of `memory_address`, I get the default which was put as my global value (in this case, `0xABC00000`. – t0rxe Jan 29 '22 at 23:37
1

I get from the printf a value of 0x1 (but I want it as 0x00000001).

If you want to display a value using 8 digits, specify the minimum field width modifier and specify 0 to pad the field with zeros.

printf("%08X", memory_address);

You may read more at https://en.cppreference.com/w/c/io/fprintf .

Note that 0x1 and 0x00000001 are exactly the same number, represented in different ways.

Note, that: int has at least 16 bits and may be smaller than 32-bits, and strtoul returns a unsigned long. To print unsigned long use %lX format specifier.

KamilCuk
  • 120,984
  • 8
  • 59
  • 111
  • I understand how to properly print results, but please answer me why `0x%X` displays the full value as `0xABC00000` already, however if I modify the variable it drops all leading or proceeding hex values? – t0rxe Jan 29 '22 at 23:56
  • 1
    There is an infinite number of leading zeros in front of any number. `%X` prints the number without any leading zeros. `0x%X` prints `0x` followed by the value of `0xABC00000` represented in hexadecimal. Just like 10$ is 10$, you don't pay with 0000010$. I do not understand the `it drops all leading or proceeding hex values?` - who is "it"? What does it mean "drops"? Leading or proceeding? To which "hex values" are you referring to? (Overall, stackoverflow is bad place to _learn_ programming. Consider hiring a tutor or [some book](https://stackoverflow.com/a/562377/9072753).) – KamilCuk Jan 30 '22 at 00:00
  • Yes, you are right. It will always remove the leading zeros from any decimal or hex value. The only way to save the value with all digits intact is to treat it as a string. Thanks. – t0rxe Jan 30 '22 at 00:14