Vikram Ranabhatt
Vikram Ranabhatt

Reputation: 7620

Data and Time format in c++

I need to convert Date time in 17 byte format.

The format is

MMDDYYYY HH:MM:SS

in ASCII.

Example date and time: 01212009 09:38:57

Hexadecimal format

0x30    0x31    0x32    0x31    0x32    0x30    0x30    0x39
0x20    0x30    0x39    0x3A    0x33    0x38    0x3A    0x35    0x37

the problem is how to convert this 01212009 09:38:57 in to hex format.I need to send this date timeover network.

Upvotes: 0

Views: 271

Answers (3)

Lightness Races in Orbit
Lightness Races in Orbit

Reputation: 385204

You're confusing form with representation.

Just because we conventionally represent "raw bytes" in hexadecimal format doesn't mean that they "exist" that way.

Whether you display the data as the ASCII string "01212009 09:38:57" or as a list of broken-down individual byte values, it's still the same data, and it's still 17 bytes.

Consequently, there is no conversion to be done here.

Upvotes: 0

Anton Semenov
Anton Semenov

Reputation: 6347

Im actually didnt know on which platform you are working. I think itoa function should exists on all platforms as part of standard C++ library. Use it to conver char value to hex. Just set radix parameter to 16

Upvotes: 1

filipe
filipe

Reputation: 3380

The hexadecimal values that you posted are exactly the ascii values for the string that you posted, 01212009 09:38:57.
There's really no conversion done from that string to the hex values in your example.

so if you have this:

const char* dateStr = "01212009 09:38:57";

and you print it like this:

for (int i=0; i < strlen(dateStr); ++i)
{
    printf("%x\t", dateStr[i]);
}

(or something like that, anyway)
you'll get basically those values.
you're just displaying them as their hexadecimal values instead of their character representation.

Upvotes: 1

Related Questions