Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

strtoull based on the input string

Tags:

c

string

I have a tool called romflasher that takes two string commandline parameters --s-addr0 and --e-addr0

romflasher --s-addr0 0x1c40000 --e-addr0 0x1e40000

Inside my program romflasher.c

 char s_addr_0[256];
 char e_addr_0[256];

 uint32_t start_addr = strtoull(s_addr_0,(char**)NULL,16);
 uint32_t end_addr   = strtoull(e_addr_0,(char**)NULL,16);

This works fine as long as the user specifies the address as hexadecimal.I want to give the user the option to specify it in both decimal as well as hexadecimal.Is there a function that will do the appropriate string conversion based on the presence of '0x' prefix to the correct base?

like image 550
liv2hak Avatar asked May 01 '26 14:05

liv2hak


1 Answers

If you pass 0 as the base argument, strtoull recognizes 0x and 0 prefix to indicate hex and octal, respectively, and defaults to decimal otherwise.

like image 96
Barmar Avatar answered May 03 '26 10:05

Barmar



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!