Link to home
Start Free TrialLog in
Avatar of kakali
kakali

asked on

ASCII to Byte Conversion

Hi,
I have a field whose decimal value is 36. I want to convert it to a 8 byte stream which will be represented as "00 00 00 00 00 00 00 24".

Now following is my code:

m_objDeviceDetail.strMerchantID.Format("%016d",_atoi64(m_objDeviceDetail.strMerchantID));

lngTemp=_atoi64(m_objDeviceDetail.strMerchantID);

if (lngTemp==0) return false;

ch=((lngTemp>>56) & 0xff);
strQBext.SetAt(0,ch);

ch=((lngTemp>>48) & 0xff);
strQBext.SetAt(1,ch);

ch=((lngTemp>>40) & 0xff);
strQBext.SetAt(2,ch);

ch=((lngTemp>>32) & 0xff);
strQBext.SetAt(3,ch);

ch=((lngTemp>>24) & 0xff);
strQBext.SetAt(4,ch);

ch=((lngTemp>>16) & 0xff);
strQBext.SetAt(5,ch);

ch=((lngTemp>>8) & 0xff);
strQBext.SetAt(6,ch);

ch=(lngTemp & 0xff);
strQBext.SetAt(7,ch);

But the out put I am getting is "00 00 00 24 00 00 00 24".

Can anyone help me to find out what should I do to get the desired result which is "00 00 00 00 00 00 00 24".

Thanks in advance
ASKER CERTIFIED SOLUTION
Avatar of zhuba
zhuba
Flag of New Zealand image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of kakali
kakali

ASKER

Many thanks. Yes this is the right solution.
Avatar of Infinity08
Let me get this right ... You have a 64 bit value, and want to convert it to a stream of 8 bytes ?

If so, you can simply copy the 64 bit value into your 8 bytes. Don't forget to take into account the endianness (byte ordering).

There's no need for arithmetic operations like shifting - simply copy the bytes.