|
|
View previous topic :: View next topic |
Author |
Message |
ritchie
Joined: 13 Sep 2003 Posts: 87
|
integer number to hex |
Posted: Mon Jan 12, 2004 4:19 am |
|
|
Mabuhay!
Anybody had an idea in converting an integer number to hex.
say: 16 ----> 0x16, 20 ---> 0x20
Anyone in the community who can provide help, snippet or any info.
Thank u. |
|
|
Ttelmah Guest
|
Re: integer number to hex |
Posted: Mon Jan 12, 2004 4:56 am |
|
|
ritchie wrote: | Mabuhay!
Anybody had an idea in converting an integer number to hex.
say: 16 ----> 0x16, 20 ---> 0x20
Anyone in the community who can provide help, snippet or any info.
Thank u. |
printf...
If you have a number '16' say, stored in a variable called 'value', then:
printf("0x%02x",value);
will output '0x10' to the current default output stream.
The 'sprintf' version will allow this to be sent to a string instead.
Best Wishes |
|
|
chingB
Joined: 29 Dec 2003 Posts: 81
|
Re: integer number to hex |
Posted: Mon Jan 12, 2004 7:55 am |
|
|
Ttelmah wrote: | ritchie wrote: | Mabuhay!
Anybody had an idea in converting an integer number to hex.
say: 16 ----> 0x16, 20 ---> 0x20
Anyone in the community who can provide help, snippet or any info.
Thank u. |
printf...
If you have a number '16' say, stored in a variable called 'value', then:
printf("0x%02x",value);
will output '0x10' to the current default output stream.
The 'sprintf' version will allow this to be sent to a string instead.
Best Wishes |
printf("0x%02x",value); ---> is it possible to store the result of the printf function to an array (.e.g. result[0] = 0x10)?
comments please...
thank u. |
|
|
Ttelmah Guest
|
Re: integer number to hex |
Posted: Mon Jan 12, 2004 8:44 am |
|
|
chingB wrote: | Ttelmah wrote: | ritchie wrote: | Mabuhay!
Anybody had an idea in converting an integer number to hex.
say: 16 ----> 0x16, 20 ---> 0x20
Anyone in the community who can provide help, snippet or any info.
Thank u. |
printf...
If you have a number '16' say, stored in a variable called 'value', then:
printf("0x%02x",value);
will output '0x10' to the current default output stream.
The 'sprintf' version will allow this to be sent to a string instead.
Best Wishes |
printf("0x%02x",value); ---> is it possible to store the result of the printf function to an array (.e.g. result[0] = 0x10)?
comments please...
thank u. |
The number in a value, is allready in _binary_. Numbers are not stored in decimal on a processor like this. Hence if you store the value '16' (decimal), into a memory cell, it will contain
00010000
Which is also representable as '0x10'.
The numeric 'forms' '16', and '0x10', are just different ways of representing the number. Normally a conversion to decimal has to be performed on the output.
printf, allows you to convert the internal numeric form into ASCII characters (so %2x, with this incoming number, will generate the characters '1' (0x31), and '0' (0x30)). You can store these two seperate values into a string array by using the sprintf version, but you _cannot_ store both these digits into a single integer, since you now need 16bits of storage to hold the two characters.
It'd be better if you described what you actually want to do.
Best Wishes |
|
|
Neutone
Joined: 08 Sep 2003 Posts: 839 Location: Houston
|
Re: integer number to hex |
Posted: Mon Jan 12, 2004 8:50 am |
|
|
chingB wrote: | Ttelmah wrote: | ritchie wrote: | Mabuhay!
Anybody had an idea in converting an integer number to hex.
say: 16 ----> 0x16, 20 ---> 0x20
Anyone in the community who can provide help, snippet or any info.
Thank u. |
printf...
If you have a number '16' say, stored in a variable called 'value', then:
printf("0x%02x",value);
will output '0x10' to the current default output stream.
The 'sprintf' version will allow this to be sent to a string instead.
Best Wishes |
printf("0x%02x",value); ---> is it possible to store the result of the printf function to an array (.e.g. result[0] = 0x10)?
comments please...
thank u. |
If value is an array this is valid.
Int8 value[20];
Then the asci characters can be read from the array one at a time. |
|
|
ritchie
Joined: 13 Sep 2003 Posts: 87
|
|
Posted: Mon Jan 12, 2004 5:53 pm |
|
|
Mabuhay!
I am interested on how can I convert like the figures shown below:
say: 16 ----> 0x16, 20 ---> 0x20
Not: 16 ----> 0x10, 20 ---> 0x14
Would this be possible? any idea or info might do?
Thank u. |
|
|
PCM programmer
Joined: 06 Sep 2003 Posts: 21708
|
|
Posted: Mon Jan 12, 2004 6:02 pm |
|
|
Quote: | I am interested on how can I convert like the figures shown below:
say: 16 ----> 0x16, 20 ---> 0x20 |
At the end of this thread, there is a bin2bcd() function
which hopefully will solve your problem.
http://www.ccsinfo.com/forum/viewtopic.php?t=10393 |
|
|
dyeatman
Joined: 06 Sep 2003 Posts: 1934 Location: Norman, OK
|
Conversion |
Posted: Mon Jan 12, 2004 7:31 pm |
|
|
Seeing the responses above, I for one am at a loss for what you are trying to do.
Apparently you are not trying to convert the number from integer to hex but take a number that you already consider a hex number and turn it into a string representation?
If I am understanding you correctly, you want to simply want to turn the number 16 into a string representation "0x16"? Or am I totally off base?
If this is your objective, one way is to separate the two numbers (ones and tens) and add the ascii char offset to each one ()i.e. 1+48+"1", 6+48="6") to make them the ascii equivalent then add them to a string starting with "0x". You will, of course, have to test for the chars A-F and add 65 to them.
This may be all a pile of crap if I am off base here... Oh well...... |
|
|
chingB
Joined: 29 Dec 2003 Posts: 81
|
Re: Conversion |
Posted: Mon Jan 12, 2004 11:24 pm |
|
|
dyeatman wrote: | Seeing the responses above, I for one am at a loss for what you are trying to do.
Apparently you are not trying to convert the number from integer to hex but take a number that you already consider a hex number and turn it into a string representation?
If I am understanding you correctly, you want to simply want to turn the number 16 into a string representation "0x16"? Or am I totally off base?
If this is your objective, one way is to separate the two numbers (ones and tens) and add the ascii char offset to each one ()i.e. 1+48+"1", 6+48="6") to make them the ascii equivalent then add them to a string starting with "0x". You will, of course, have to test for the chars A-F and add 65 to them.
This may be all a pile of crap if I am off base here... Oh well...... |
What I am trying to is this.... I have a number variable say, numb1 with a value 16. Then the value of numb1 is translated to a hex value which hnumb1 = 0x16 --> this a hex value.
PCM Programmer have already provided me a solution using the bin2bcd() function.
I guess you got my point here...
Thanx |
|
|
chingB
Joined: 29 Dec 2003 Posts: 81
|
Re: Conversion |
Posted: Mon Jan 12, 2004 11:28 pm |
|
|
dyeatman wrote: | Seeing the responses above, I for one am at a loss for what you are trying to do.
Apparently you are not trying to convert the number from integer to hex but take a number that you already consider a hex number and turn it into a string representation?
If I am understanding you correctly, you want to simply want to turn the number 16 into a string representation "0x16"? Or am I totally off base?
If this is your objective, one way is to separate the two numbers (ones and tens) and add the ascii char offset to each one ()i.e. 1+48+"1", 6+48="6") to make them the ascii equivalent then add them to a string starting with "0x". You will, of course, have to test for the chars A-F and add 65 to them.
This may be all a pile of crap if I am off base here... Oh well...... |
What I am trying to is this.... I have a number variable say, numb1 with a value 16. Then the value of numb1 is translated to a hex value which hnumb1 = 0x16 --> this a hex value.
PCM Programmer have already provided me a solution using the bin2bcd() function.
I guess you got my point here...
Thanx |
|
|
|
|
You cannot post new topics in this forum You cannot reply to topics in this forum You cannot edit your posts in this forum You cannot delete your posts in this forum You cannot vote in polls in this forum
|
Powered by phpBB © 2001, 2005 phpBB Group
|