1

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 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?

1
  • 4
    Post some code that illustrates your problem. Commented Jan 7, 2010 at 13:26

5 Answers 5

2

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 :.

Sign up to request clarification or add additional context in comments.

2 Comments

I'd be very careful with strs.str().c_str(). stringstream::str(), I believe, is allowed to return a temporary copy, and a .c_str() on top of that is getting a pointer to temporary memory. This issue has bitten me before.
Whoever tried edit this: I was editing it a bunch at the time and don't know how to merge the edits. Please recheck my work. Sorry.
1

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

Comments

1

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

Comments

1

You should read up on Endianness.

IBM has a nice article on endianness in practice.

Comments

0

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!

2 Comments

Okay, you just replied in the form of an answer. Next time, put this in a comment under the answer you're referring to (that "add comment" button below an answer) or edit your question to add the new information or question.
// 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; } output HMAC: 76e061dc7512be8bcca2dce44e0b81608771714b 118 224 97 220 117 18

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.