Hello, Got another silly C newbie question. If I want to convert a hex value to decimal, would this work?
value = ff /* Hex value */
sscanf(value, %d, &value)
Is the variable "value" now 255??
Thanks - Joe
On Wed, Jul 04, 2007 at 09:22:54AM -0400, Joseph Smith wrote:
Hello, Got another silly C newbie question. If I want to convert a hex value to decimal, would this work?
value = ff /* Hex value */
sscanf(value, %d, &value)
Is the variable "value" now 255??
Hexadecimal, decimal and octal are different ways for us humans to express numbers to computers, but no matter what we use, they are always stored in binary form in the machine.
Thus, there is no difference between a number in hexadecimal or decimal. We do however have to tell the computer which formatting we want when the computer should show us the numbers.
unsigned char value;
value=0xff; /* hexadecimal */
/* %d means print number in decimal */ printf("value in decimal is now %d\n",value);
value=135; /* decimal */
/* %x means print in hex */ printf("value in hexadecimal is now %x\n",value);
value=0254; /* octal */
if(0xac==0254) printf("C knows that 0xac == 0254 because they are both == %d\n",value);
Have a look at the printf man page for your nearest C library to learn about all the good stuff you can put into formatting strings besides just %d and %x.
//Peter
On Wed, Jul 04, 2007 at 05:29:56PM +0200, Peter Stuge wrote:
On Wed, Jul 04, 2007 at 09:22:54AM -0400, Joseph Smith wrote:
Hello, Got another silly C newbie question. If I want to convert a hex value to decimal, would this work?
value = ff /* Hex value */
sscanf(value, %d, &value)
Is the variable "value" now 255??
Hexadecimal, decimal and octal are different ways for us humans to express numbers to computers, but no matter what we use, they are always stored in binary form in the machine.
Thus, there is no difference between a number in hexadecimal or decimal. We do however have to tell the computer which formatting we want when the computer should show us the numbers.
unsigned char value;
value=0xff; /* hexadecimal */
/* %d means print number in decimal */ printf("value in decimal is now %d\n",value);
value=135; /* decimal */
/* %x means print in hex */ printf("value in hexadecimal is now %x\n",value);
value=0254; /* octal */
if(0xac==0254) printf("C knows that 0xac == 0254 because they are both == %d\n",value);
Have a look at the printf man page for your nearest C library to learn about all the good stuff you can put into formatting strings besides just %d and %x.
I use some aliases in my .bashrc which are pretty handy for converting HEX or BINARY or DECIMAL:
alias HEX="ruby -e 'printf("0x%X\n", ARGV[0])'" alias DEC="ruby -e 'printf("%d\n", ARGV[0])'" alias BIN="ruby -e 'printf("%bb\n", ARGV[0])'"
(You can replace 'ruby' with 'perl' if you want)
Usage:
$ HEX 78 0x4E $ BIN 0x6a 1101010b $ DEC 0x72 114
HTH, Uwe.
On Wed, Jul 04, 2007 at 07:47:11PM +0200, Uwe Hermann wrote:
I use some aliases in my .bashrc which are pretty handy for converting HEX or BINARY or DECIMAL:
alias HEX="ruby -e 'printf("0x%X\n", ARGV[0])'" alias DEC="ruby -e 'printf("%d\n", ARGV[0])'" alias BIN="ruby -e 'printf("%bb\n", ARGV[0])'"
(You can replace 'ruby' with 'perl' if you want)
bash has printf too; try printf 0x%X\n 128
//Peter
2007/7/4, Peter Stuge peter@stuge.se:
On Wed, Jul 04, 2007 at 07:47:11PM +0200, Uwe Hermann wrote:
I use some aliases in my .bashrc which are pretty handy for converting HEX or BINARY or DECIMAL:
alias HEX="ruby -e 'printf("0x%X\n", ARGV[0])'" alias DEC="ruby -e 'printf("%d\n", ARGV[0])'" alias BIN="ruby -e 'printf("%bb\n", ARGV[0])'"
(You can replace 'ruby' with 'perl' if you want)
bash has printf too; try printf 0x%X\n 128
Yes, printf at shell is very useful, I use this command to reset BIOS CMOS:
# printf "\x2E" | dd bs=1 seek=112 of=/dev/port # printf "\xFF" | dd bs=1 seek=113 of=/dev/port
It sends 0x2E to port 0x70 and 0xFF to port 0x71.
//Peter
Alan
* Uwe Hermann uwe@hermann-uwe.de [070704 19:47]:
I use some aliases in my .bashrc which are pretty handy for converting HEX or BINARY or DECIMAL:
alias HEX="ruby -e 'printf("0x%X\n", ARGV[0])'" alias DEC="ruby -e 'printf("%d\n", ARGV[0])'" alias BIN="ruby -e 'printf("%bb\n", ARGV[0])'"
Usage:
$ HEX 78 0x4E $ BIN 0x6a 1101010b $ DEC 0x72 114
cool stuff. Can it easily be fixed to convert binary into hex or dec, too?
On Thu, Jul 05, 2007 at 05:58:18AM +0200, Stefan Reinauer wrote:
- Uwe Hermann uwe@hermann-uwe.de [070704 19:47]:
I use some aliases in my .bashrc which are pretty handy for converting HEX or BINARY or DECIMAL:
alias HEX="ruby -e 'printf("0x%X\n", ARGV[0])'" alias DEC="ruby -e 'printf("%d\n", ARGV[0])'" alias BIN="ruby -e 'printf("%bb\n", ARGV[0])'"
Usage:
$ HEX 78 0x4E $ BIN 0x6a 1101010b $ DEC 0x72 114
cool stuff. Can it easily be fixed to convert binary into hex or dec, too?
Yes, that works already (the syntax is a bit strange, though):
$ HEX 0b10101010 0xAA $ DEC 0b10000011 131
Uwe.
Quoting Uwe Hermann uwe@hermann-uwe.de:
On Thu, Jul 05, 2007 at 05:58:18AM +0200, Stefan Reinauer wrote:
- Uwe Hermann uwe@hermann-uwe.de [070704 19:47]:
I use some aliases in my .bashrc which are pretty handy for converting HEX or BINARY or DECIMAL:
alias HEX="ruby -e 'printf("0x%X\n", ARGV[0])'" alias DEC="ruby -e 'printf("%d\n", ARGV[0])'" alias BIN="ruby -e 'printf("%bb\n", ARGV[0])'"
Usage:
$ HEX 78 0x4E $ BIN 0x6a 1101010b $ DEC 0x72 114
cool stuff. Can it easily be fixed to convert binary into hex or dec, too?
Yes, that works already (the syntax is a bit strange, though):
$ HEX 0b10101010 0xAA $ DEC 0b10000011 131
Uwe.
Ok so how would I go about doing this is print_debug than? Something like:
value=0xff; /* hexadecimal */
/* %d means print number in decimal */ print_debug("value in decimal is now %d\n",value);
Thanks - Joe
On Wed, Jul 04, 2007 at 04:36:57PM -0400, Joseph Smith wrote:
printf("value in hexadecimal is now %x\n",value);
Ok so how would I go about doing this is print_debug than?
Sorry.
print_debug() in v2 doesn't support variable number of arguments (varargs) but maybe you could backport printk() from v3 lib/console.c ?
//Peter
Quoting Peter Stuge peter@stuge.se:
On Wed, Jul 04, 2007 at 04:36:57PM -0400, Joseph Smith wrote:
printf("value in hexadecimal is now %x\n",value);
Ok so how would I go about doing this is print_debug than?
Sorry.
print_debug() in v2 doesn't support variable number of arguments (varargs) but maybe you could backport printk() from v3 lib/console.c ?
//Peter
Maybe V3 at a later date, I'm still working on porting my board to V2.
Ok, if the computer sees everthing in binary, than maybe I can try something like this?
#define PRINT_DEBUG_DEC(d)
value=0xff; /* hexadecimal */
print_debug("value in decimal is now "); PRINT_DEBUG_DEC(value); print_debug(" Is this working?\r\n");
What do you think?
Thanks - Joe
* Joseph Smith joe@smittys.pointclark.net [070706 17:41]:
#define PRINT_DEBUG_DEC(d)
value=0xff; /* hexadecimal */
print_debug("value in decimal is now "); PRINT_DEBUG_DEC(value); print_debug(" Is this working?\r\n");
Since it seems to be for debug only, you can also use print_debug_hex8 print_debug_hex16 print_debug_hex32
Stefan
Quoting Stefan Reinauer stepan@coresystems.de:
- Joseph Smith joe@smittys.pointclark.net [070706 17:41]:
#define PRINT_DEBUG_DEC(d)
value=0xff; /* hexadecimal */
print_debug("value in decimal is now "); PRINT_DEBUG_DEC(value); print_debug(" Is this working?\r\n");
Since it seems to be for debug only, you can also use print_debug_hex8 print_debug_hex16 print_debug_hex32
Stefan
But that is the whole problem. I already says "print_debug_hex8". I want it to display decimal not hex.....
Thanks - Joe
* Joseph Smith joe@smittys.pointclark.net [070707 00:59]:
But that is the whole problem. I already says "print_debug_hex8". I want it to display decimal not hex.....
You could look at print_debug_hex8 and implement the _dec8 functions.
I don't think it's worth the effort though. What's wrong with hex?
It's all debug anyways.
Quoting Stefan Reinauer stepan@coresystems.de:
- Joseph Smith joe@smittys.pointclark.net [070707 00:59]:
But that is the whole problem. I already says "print_debug_hex8". I want it to display decimal not hex.....
You could look at print_debug_hex8 and implement the _dec8 functions.
I don't think it's worth the effort though. What's wrong with hex?
It's all debug anyways.
I kow it is just for aesthetics but, If it is an easy implimentation, I will check it out. What file is print_debug_hex8 in anyways?
Thanks Again - Joe
joe, print in hex is trivial.
In the limit, it's this simple: putc(hexarray((a>>4)&0xf); putc(hexarray(a&0xf));
decimal is harder, and, in romcc, not worth the effort.
I think this whole decimal escapade is not a good use of time.
ron
Ühel kenal päeval (laupäev 07 juuli 2007 1:59 am) kirjutas Joseph Smith:
Quoting Stefan Reinauer stepan@coresystems.de:
- Joseph Smith joe@smittys.pointclark.net [070706 17:41]:
#define PRINT_DEBUG_DEC(d)
value=0xff; /* hexadecimal */
print_debug("value in decimal is now "); PRINT_DEBUG_DEC(value); print_debug(" Is this working?\r\n");
Since it seems to be for debug only, you can also use print_debug_hex8 print_debug_hex16 print_debug_hex32
Stefan
But that is the whole problem. I already says "print_debug_hex8". I want it to display decimal not hex.....
/* Print CPU clock */
p3 = cpu_clk / 100; p2 = (cpu_clk - p3 * 100)/10; p1 = cpu_clk - p3 * 100 - p2*10;
print_info("CPU "); print_info_char(48+p3); print_info_char(48+p2); print_info_char(48+p1); print_info(" MHz\r\n");
:)
cheers, Indrek
inline void print_info_dec(unsigned long value,unsigned long res) { for(;res;res/=10) print_info_char('0'+value/res%10); }
On Mon, Jul 09, 2007 at 09:43:49AM +0300, Indrek Kruusa wrote:
print_info("CPU ");
print_info_dec(cpu_clk,100);
print_info(" MHz\r\n");
Note this only prints the modulus while Indrek's code indicates that the value does not fit in 3 positions by printing a non-digit hundreds character.
Multiply the resoluton by 10 to get more digits. 1000=4, 10000=5 etc.
Omitting leading 0:s would need some state to be kept and cost one more register.
//Peter
Quoting Peter Stuge peter@stuge.se:
inline void print_info_dec(unsigned long value,unsigned long res) { for(;res;res/=10) print_info_char('0'+value/res%10); }
On Mon, Jul 09, 2007 at 09:43:49AM +0300, Indrek Kruusa wrote:
print_info("CPU ");
print_info_dec(cpu_clk,100);
print_info(" MHz\r\n");
Note this only prints the modulus while Indrek's code indicates that the value does not fit in 3 positions by printing a non-digit hundreds character.
Multiply the resoluton by 10 to get more digits. 1000=4, 10000=5 etc.
Omitting leading 0:s would need some state to be kept and cost one more register.
//Peter
Actually, I found this in one of the scripts:
printk_debug("Setting RAM size to %d\n", tomk);
Thanks - Joe
* Joseph Smith joe@smittys.pointclark.net [070709 23:04]:
Actually, I found this in one of the scripts:
printk_debug("Setting RAM size to %d\n", tomk);
print_debug only works in preram stage
printk_debug only works in ram stage
with some exceptions. :-)
Stefan
* Peter Stuge peter@stuge.se [070706 16:28]:
On Wed, Jul 04, 2007 at 04:36:57PM -0400, Joseph Smith wrote:
printf("value in hexadecimal is now %x\n",value);
Ok so how would I go about doing this is print_debug than?
Sorry.
print_debug() in v2 doesn't support variable number of arguments (varargs) but maybe you could backport printk() from v3 lib/console.c ?
There are already 6 printk/f variants in v2. So one could assume this has been tried 5 times already ;)
Quoting Peter Stuge peter@stuge.se:
On Wed, Jul 04, 2007 at 09:22:54AM -0400, Joseph Smith wrote:
Hello, Got another silly C newbie question. If I want to convert a hex value to decimal, would this work?
value = ff /* Hex value */
sscanf(value, %d, &value)
Is the variable "value" now 255??
Hexadecimal, decimal and octal are different ways for us humans to express numbers to computers, but no matter what we use, they are always stored in binary form in the machine.
Thus, there is no difference between a number in hexadecimal or decimal. We do however have to tell the computer which formatting we want when the computer should show us the numbers.
unsigned char value;
value=0xff; /* hexadecimal */
/* %d means print number in decimal */ printf("value in decimal is now %d\n",value);
value=135; /* decimal */
/* %x means print in hex */ printf("value in hexadecimal is now %x\n",value);
value=0254; /* octal */
if(0xac==0254) printf("C knows that 0xac == 0254 because they are both == %d\n",value);
Have a look at the printf man page for your nearest C library to learn about all the good stuff you can put into formatting strings besides just %d and %x.
//Peter
-- linuxbios mailing list linuxbios@linuxbios.org http://www.linuxbios.org/mailman/listinfo/linuxbios
Ok so how would I go about doing this is print_debug than? Something like:
value=0xff; /* hexadecimal */
/* %d means print number in decimal */ print_debug("value in decimal is now %d\n",value);
Thanks - Joe