Hi,

I want to convert unit32 variable to uint16 variable because I can't read uint16 directly from a file using sscanf. Is that possible?

Thank you!

>I can't read uint16 directly from a file using sscanf
You can't read anything directly from a file using sscanf.

>Is that possible?
I think you need to be more specific. Why do you think you can't read a uint16 value?

Edited 6 Years Ago by Narue: n/a

>I know that I can't read anything directly: http://www.cplusplus.com/reference/c...cstdio/sscanf/
I'm well aware of what sscanf does and how it works. I was making a joke, seeing as how sscanf reads from a string rather than a file.

>I can't read an uint16 from a file for this reason:
So what's the problem? You have an answer and it's easy to do. Though there are nuances that make your code non-portable, not the least of which is the assumption that uint32 resolves to int and uint16 does not. You would probably be better off using long and short instead of uint32 and uint16, respectively. long is guaranteed to be at least 32 bits and short is guaranteed to be at least 16. Then to read "uint16" with sscanf, you can use the %hd specifier as described in the first link you provided.

>Thank you!
I love it when people ignore my help and then thank me. It's even better when they choose a horrible solution in favor of my superior recommendations. :icon_rolleyes:

I love it when people ignore my help and then thank me. It's even better when they choose a horrible solution in favor of my superior recommendations. :icon_rolleyes:

Do I detect some "sarcasm" here? :icon_razz:

Then to read "uint16" with sscanf, you can use the %hd specifier as described in the first link you provided.

I didn't ignore your solution. I'm gonna try it. ;)

This question has already been answered. Start a new discussion instead.