Inputting numbers of a fixed length

R

Roncohl

So I'm trying to read some data from a file, which includes hex numbers
of a known length (In my specific case, 6). I know there are more
indirect ways to do this, but I was hoping I could do it more directly.
For example, I want to read in the first 6 characters in this string as
a hex number:
0000FF123456
I was hoping I could do something along the lines of

int x;
ifstream file;
// Open the file, etc.
file >> setw(6) >> x;

The problem is, it ignores the 0s - x will have the value 0xFF1234
(Instead of 0x0000FF, like I want).
Is there a way around this, or am I going to have to find a less direct
way?

Thanks.
 
V

Victor Bazarov

So I'm trying to read some data from a file, which includes hex
numbers of a known length (In my specific case, 6). I know there are
more indirect ways to do this, but I was hoping I could do it more
directly. For example, I want to read in the first 6 characters in
this string as a hex number:
0000FF123456
I was hoping I could do something along the lines of

int x;
ifstream file;
// Open the file, etc.
file >> setw(6) >> x;

The problem is, it ignores the 0s - x will have the value 0xFF1234
(Instead of 0x0000FF, like I want).
Is there a way around this, or am I going to have to find a less
direct way?

Use scanf. Unfortunately currently streams ignore the width setting
when in input mode.

V
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,770
Messages
2,569,584
Members
45,075
Latest member
MakersCBDBloodSupport

Latest Threads

Top