Page 1 of 1
Writing a print-function that can print variables.
Posted: Wed Mar 23, 2005 10:11 am
by Espen
I'm having a problem writing a print-function that can print variables in C.
Something like:
The only thing i have now i a function that writes literal text
like:
I hope any of you have any good ideas for me
Re:Writing a print-function that can print variables.
Posted: Wed Mar 23, 2005 11:10 am
by Pype.Clicker
get a bit further on page 2 of the board ...
http://www.mega-tokyo.com/forum/index.p ... eadid=7462
should help.
Re:Writing a print-function that can print variables.
Posted: Wed Mar 23, 2005 11:23 am
by Espen
Iv`e now created a neat function, the only problem is that it writes to many zeros in the start
Code: Select all
void printint(unsigned int i)
{
putch((char)(((i / 1000000000) %10) + '0'));
putch((char)(((i / 100000000) %10) + '0'));
putch((char)(((i / 10000000) %10) + '0'));
putch((char)(((i / 1000000) %10) + '0'));
putch((char)(((i / 100000) %10) + '0'));
putch((char)(((i / 10000) %10) + '0'));
putch((char)(((i / 1000) %10) + '0'));
putch((char)(((i / 100) %10) + '0'));
putch((char)(((i / 10) %10) + '0'));
putch((char)(((i / 1) %10) + '0'));
csr_y++;
}
putch prints on char onto the screen.
My problem now is when i is 50, 00000000050 is the output.
Re:Writing a print-function that can print variables.
Posted: Wed Mar 23, 2005 1:16 pm
by Red Shaya
A cool fix to your problem would be something like this pseodo code:
Code: Select all
int divider = 1000000000
int digit = 0
bool printFlag = False
while divider > 0 do
digit = (i/divider) % 10
if digit > 0
PrintFlag = True
endIf
if PrintFlag then
putch((char) digit)
endIf
divider = divider / 10
endWhile
Re:Writing a print-function that can print variables.
Posted: Wed Mar 23, 2005 5:03 pm
by Crazed123
That'll work OK for decimal, but what about hex? Or do the ASCII values for A, B, C, D, E and F come straight after the one for '9'? I can't remember.
Re:Writing a print-function that can print variables.
Posted: Wed Mar 23, 2005 7:21 pm
by Just Another Guest
No they don't (... 8 9 : ; < = > ? ...). Use a conversion array.
Re:Writing a print-function that can print variables.
Posted: Thu Mar 24, 2005 3:01 am
by Pype.Clicker
Crazed123 wrote:
That'll work OK for decimal, but what about hex? Or do the ASCII values for A, B, C, D, E and F come straight after the one for '9'? I can't remember.
For hex display, it's even simpler:
1. because you often want it to be fixed-width, so you don't have to do a first pass counting how many digits there will be (e.g. you usually prefer 0x0102 or 0x00000102 rather than 0x102 for pointers, etc)
2. because division per 16 is only >>4 and remainder is &0xf
The trick here is to have a lookup array char hexdigits[]="0123456789ABCDEF" (something i found in start32 code by Tran/Renaissance somewhere around '97)
Re:Writing a print-function that can print variables.
Posted: Thu Mar 24, 2005 11:59 am
by Red Shaya
Look at the PrintReg16 function in the
following code to get a simple and good example of using a lookup array.
Re:Writing a print-function that can print variables.
Posted: Thu Mar 24, 2005 4:18 pm
by dh
Don't see much of crazybudda now-a-day :'(
Cheers, DH.
Re:Writing a print-function that can print variables.
Posted: Fri Mar 25, 2005 4:04 am
by Candy
Might I suggest +7 or +39?
In ASCII, UTF-8 and Unicode (yes, them all) the numbers go from 0x30 to 0x39 and the characters from 0x41-0x46 or 0x61-0x66, depending on whether you want upper case or lower case (respectively). So,
Code: Select all
char * tohexprint(unsigned int x) {
char buffer[9];
int i;
for (i=0; i<8; i++) {
int asc = ((x & (15 << (4*i))) >> (4*i)) + 0x30;
if (asc > 0x39) asc += 0x7; // or 0x27
buffer[7-i] = asc;
}
return buffer;
}
This is of course excellent for mmx optimization, although I don't consider it worth your trouble. For exercise however, it's good.
Re:Writing a print-function that can print variables.
Posted: Fri Mar 25, 2005 10:35 pm
by Chris Giese
Code: Select all
void print_unsigned_long(unsigned long val, unsigned radix) {
char buf [33], *s = buf + sizeof(buf);
unsigned remainder;
/* store terminating nul character in buf */
s--;
*s = '\0';
/* repeatedly divide 'val' by 'radix' until 'val' == 0 */
do {
/* ...it's the remainder we really want... */
remainder = (unsigned)(val % radix);
val = val / radix;
/* convert binary remainder to ASCII digit */
remainder += '0';
if(remainder > '9')
remainder += ('A' - ('9' + 1));
/* store ASCII digits RIGHT-TO-LEFT in buf */
s--;
*s = remainder;
} while(val != 0);
/* et voil?, display numeric string */
puts(s); }
Some questions to ask yourself about this code:
- What are the legal values for 'radix'?
- Why is 'buf' 33 bytes long? Will this buffer ever overflow?
- What would happen if we used while() { } instead of do { } while() ?
Support for printing signed numbers, fixed field widths,
pad-left-with-zeroes, etc. is left as an exercise...
Re:Writing a print-function that can print variables.
Posted: Sat Mar 26, 2005 3:59 am
by B.E
he is another example of printing a integer in hex string
Code: Select all
char* tohexprint(unsigned int x)
{
char* buffer = new char[8]; //Buffer to put the string
buffer[8] = 0; //Terminate the string
char _Hex[17] = "0123456789ABCDEF";//Hex string
_asm
{
mov ecx,8 //cycle though 8 times
mov edi,[buffer] //edi -> points to buffer
add edi,8 //edi -> points to end of buffer
mov eax,[x] //eax -> integer to convert
lea edx,_Hex //edx -> points to hex string
NextDig:
push eax //save eax
and eax,0Fh //last 4 bits of eax
mov al,byte ptr [edx+eax]//al -> _Hex[eax]
mov byte ptr[edi],al //put char into buffer
pop eax //restore eax
shr eax,4 //get now bits
dec edi //goto next char of buffer
loop NextDig //do it all again 8 times
}
return buffer;
}