tags:

views:

146

answers:

5

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 unbuntu and this will probably be used on a sun many hp unix box.

so the question i have is that 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 amnot sure why it works. furthermore i read somewhere that when you put it in unsinged 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

thanks

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

+1  A: 

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

Moisei
+1  A: 

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

Kornel Kisielewicz
+1  A: 

You should read up on Endianness.

IBM has a nice article on endianness in practice.

Thorsten79
A: 

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!

khumayun
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.
Mike D.
// use just first 6 bytesbyte 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;}outputHMAC: 76e061dc7512be8bcca2dce44e0b81608771714b1182249722011718
khumayun
+2  A: 

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

Mike D.
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.
luke
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.
Mike D.
@luke: Noted and deleted.
Mike D.