开发者

how to convert a binary array with hex values into decimal and then to a string

I have a binary array with hex values and I would like to convert it into decimal. Once that is done I would like to display it in a string.

BTW, I am working on an intel process via ubuntu and this will probably be used on a sun many hp unix box.

When I do a straight up assignment to unsigned long it works... as in the decimal part. I still have not made that into a string yet, but I am not sure why it works. Furthermore I read somewhere that when you put it in unsigned long the you will not have to worry about endianess.

Could I get some help on how to take this issue and why the aforementioned works as well?

please note: I am using c++ and not the .net stuff so bitconverter is not available.

EDIT (Copied from answer to own question below)

first of all thanks for the endian link that IBM article really helped. knowing that i was just putting it into a register rather than manually putting the byte values in consecutive memory locations is what takes away the whole endianess issue made a big difference.

here is a piece of code that I wrote which I know can be better but its just something quick...

// use just first 6 bytes
byte truncHmac[6];
memset( truncHmac, 0x00, 6 );
unsigned long sixdigit[6];
for (int i=0; i<=5; i++)
{
    truncHmac[i]=hmacOutputBuffer[i];
    sixdigit[i]=truncHmac[i];
    std::cout<<sixdigit[i]<< std::endl;
}

the output i开发者_StackOverflow社区s

HMAC: 76e061dc7512be8bcca2dce44e0b81608771714b
118
224
97
220
117
18

which makes sense that its take the first six bytes and converting them to decimals.

The only question I have now is how to make this into a string. Someone mentioned using manipulators? Could I get an example?


Indenting your code with four spaces per line makes it show up as code:

// use just first 6 bytes
byte truncHmac[6];
memset( truncHmac, 0x00, 6 );
unsigned long sixdigit[6];

for (int i=0; i<=5; i++)
{
    truncHmac[i]=hmacOutputBuffer[i];
    sixdigit[i]=truncHmac[i];
    std::cout<<sixdigit[i]<< std::endl;
}

Hit the orange ? at the top right of the editing box for markup help.

As for manipulators, use a stringstream instead of cout:

#include <sstream>

stringstream strs;

for (int i = 0; i < 6; ++i)
{
    strs << hex << hmacOutputBuffer[i] << std::endl;
}

strs.str(); // returns a string object.

EDIT: Writing loops as for (int i = 0; i < N; ++i) instead of for (int i = 0; i <= N - 1; ++i) will work better for you when you need to deal with C++ iterators, which define the end as "one past the last valid element".

Also, MAC addresses typically put - or : characters in between each byte. The use of : is deprecated because IPv6 addresses also use :.


hex, decimal and char are just a different way to present the value. it is up to your application how to interpret the value.


You might want to read up on C++ iostream manipulators, especially on hex.


You should read up on Endianness.

IBM has a nice article on endianness in practice.


first of all thanks for the endian link that IBM article really helped. knowing that i was just putting it into a register rather than manually putting the byte values in consecutive memory locations is what takes away the whole endianess issue made a big difference.

here is a pc of code that i wrote which i know can be better but its just something quick...

// use just first 6 bytes
byte truncHmac[6];
memset( truncHmac, 0x00, 6 );
unsigned long sixdigit[6];
for (int i=0; i<=5; i++)
{
    truncHmac[i]=hmacOutputBuffer[i];
    sixdigit[i]=truncHmac[i];
    std::cout<<sixdigit[i]<< std::endl;
}

the output is

HMAC: 76e061dc7512be8bcca2dce44e0b81608771714b
118
224
97
220
117
18

which makes sense that its take the first six bytes and converting them to decimals.

the only question i have now is how to make this into a string..someone mentioned using manipulators? could i get an example?

thanks a bunch guys you all rock!

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜