Click to See Complete Forum and Search --> : converting data to hexadecimal


Brett
01-17-2001, 08:45 PM
Any idea how to convert data read in from a file to output in hexadecimal.
Any ideas would be greatly appreciated. Thanks.

Danny Kalev
01-17-2001, 10:03 PM
int num;
// ...assign a value to num
cout<<hex<<num; // display in hex format

Danny

Brett wrote:
>
> Any idea how to convert data read in from a file to output in hexadecimal.
> Any ideas would be greatly appreciated. Thanks.

BRett
01-18-2001, 03:39 PM
Ok, say for example i have the letter A, i convert that to hex its 41, how
can i get it to display it in binary form of 0100 0001, i need to be able
to do this for everything i need to read in from a file. take for example
"Age", i need to do it for each letter.

Thanks


Danny Kalev <dannykk@inter.net.il> wrote:
>int num;
>// ...assign a value to num
>cout<<hex<<num; // display in hex format
>
>Danny
>
>Brett wrote:
>>
>> Any idea how to convert data read in from a file to output in hexadecimal.
>> Any ideas would be greatly appreciated. Thanks.

jonnin
01-18-2001, 03:57 PM
once you have it in hex, to binary is easy.
for a byte, the binary is grouped by 4s. 12h is 1 in binary and 2 in binary,
= 0001 0010. ABh is 1011 1100. Easy to code. There is probably a built
in way to do binary output, look it up...

to convert the hex to ascii hex use sprintf. then use the chars to do the
above.

alternately, use and masks and bit shifting to crank it out.
x and 10000000.
x and 01000000.
x and 00100000. ...

and write the bit in the 1 position each time.

(shift it right to ones position and write the int. it will be one or zero).





"BRett" <brettritch@yahoo.com> wrote:
>
>Ok, say for example i have the letter A, i convert that to hex its 41, how
>can i get it to display it in binary form of 0100 0001, i need to be able
>to do this for everything i need to read in from a file. take for example
>"Age", i need to do it for each letter.
>
>Thanks
>
>
>Danny Kalev <dannykk@inter.net.il> wrote:
>>int num;
>>// ...assign a value to num
>>cout<<hex<<num; // display in hex format
>>
>>Danny
>>
>>Brett wrote:
>>>
>>> Any idea how to convert data read in from a file to output in hexadecimal.
>>> Any ideas would be greatly appreciated. Thanks.
>

W R Collymore
01-22-2001, 06:36 AM
"BRett" <brettritch@yahoo.com> wrote:
>
>Ok, say for example i have the letter A, i convert that to hex its 41, how
>can i get it to display it in binary form of 0100 0001, i need to be able
>to do this for everything i need to read in from a file. take for example
>"Age", i need to do it for each letter.
>
>Thanks
>
>
>Danny Kalev <dannykk@inter.net.il> wrote:
>>int num;
>>// ...assign a value to num
>>cout<<hex<<num; // display in hex format
>>
>>Danny
>>
>>Brett wrote:
>>>
>>> Any idea how to convert data read in from a file to output in hexadecimal.
>>> Any ideas would be greatly appreciated. Thanks.
>

Its sounds like your converting ASCII bytes to their HEX equivalent to be
displayed in BINARY form. Try the following:

#include<iostream.h>
int main()
{


int index=0;// Initialise string index to point to individual
// characters in string
char string1[30];// This string to hold characters from file
// u will need to redirect the input form file to
//string first

while(string1[index]!='\0')// continue while not end of string
{
cout<<bin<<string1[index]<<endl;// display each byte in binary
};

return 0;

}

Try and Reply

W R Collymore
01-22-2001, 12:53 PM
"W R Collymore" <a2ias.jones@mailexcite.com> wrote:
>
>"BRett" <brettritch@yahoo.com> wrote:
>>
>>Ok, say for example i have the letter A, i convert that to hex its 41,
how
>>can i get it to display it in binary form of 0100 0001, i need to be able
>>to do this for everything i need to read in from a file. take for example
>>"Age", i need to do it for each letter.
>>
>>Thanks
>>
>>
>>Danny Kalev <dannykk@inter.net.il> wrote:
>>>int num;
>>>// ...assign a value to num
>>>cout<<hex<<num; // display in hex format
>>>
>>>Danny
>>>
>>>Brett wrote:
>>>>
>>>> Any idea how to convert data read in from a file to output in hexadecimal.
>>>> Any ideas would be greatly appreciated. Thanks.
>>
>Hi
>Its sounds like your converting ASCII bytes to their HEX equivalent to
be
>displayed in BINARY form. Try the following:
>
>#include<iostream.h>// for cout
#include<conio.h> // for clrscr
#include<stdio.h>// for getch
long int bin(char octet1);// function to convert ASCII to bin

>int main()
>{
>clrscr();

>
>int index=0;// Initialise string index to point to individual
>// characters in string
>char string1[30];// This string to hold characters from file
>// u will need to redirect the input from file to the
> //string first
>
>while(string1[index]!='\0')// continue while not end of string
>{
>cout<<bin(string1[index])<<endl<<string1[index]<<endl;// display each byte
in binary
// and ASCII
index++;// point to next character
>};
>
>return 0;
>
>}


long int bin(char octet1)
{


char octet2=octet1&0xFF; // mask to byte
long int num=0;

for(int x=1;x<9;x++) // shift 8 times
{
if(octet2&0x80)
{
num=(num*10)+1;
}
else
{
num=num*10;
};

octet2= octet2<<1;
};

return num;// return long int
};

>
>Try and Reply
>
>
>