Ok Any Forums, you know programming?
Write a function in C which takes as argument an integer, and a base, and outputs a string representation of that integer in the given base.
1900, 2 = "11101101100"
1900, 16 = 76C
No stdlib functions.
Ok Any Forums, you know programming?
Other urls found in this thread:
en.wikipedia.org
twitter.com
Any Forums doesn't know maths
This is not complicated
No, I will not do your homework for you
Just pay a pajeet on fiver to do your homework
In python u would just write float.(variable_name) if variable is a string
Btw I literally started learning to program this week hahaha.
see
Not your personal army faggot. Also en.wikipedia.org
Ever heard of % operator. If not learn about it. And don't ask help for your homework Rajesh
do your own homework, faggot
already done you faggots
void ft_putnbr_base(int n, int base)
{
long nb;
nb = n;
if (nb < 0)
{
ft_putchar('-');
nb *= -1;
}
if (nb < base)
{
if (base > 10 && nb > 10)
putchar(nb + 'A' - 10);
else
putchar(nb + '0');
return ;
}
ft_putnbr_base(nb / base, base);
ft_putnbr_base(nb % base, base);
}
What is the maximum base that should be supported? If above 64, what characters should be used that are not currently used in base 64? Additionally, what should be the return value for invalid bases?
>do my homework for me
You should save that for when things actually start getting hard
fn fmt(n: i32, size: usize) -> Option {
match size {
2 => Some(format!("{n:b}")),
6 => Some(format!("{n:x}")),
8 => Some(format!("{n:o}")),
_ => None
}
}
you know that using recursion there is not faster than looping?
recursion is great for trees, is meh for simple shit like that
Do your homework, OP. It's important.
He said in C. He also said no stdlib.
#define STACK_SIZE 10000
unsigned stack[STACK_SIZE];
unsigned* stackp = stack;
#define EMPTY -1
static inline void push(unsigned n){*stackp++ = n;}
static inline int pop(){
return stackp != stack ? *--stackp : EMPTY;
}
void print(char s) {
__asm__(
"push %[in]\n\t"
"mov $1, %%rax \n\t"
"mov $1, %%rdi \n\t"
"mov %%rsp, %%rsi \n\t"
"mov $1, %%rdx \n\t"
"syscall \n\t"
"add $8, %%rsp \n\t;"
:: [in] "m" (s)
);
}
void outpt(unsigned n, unsigned base) {
for(; n > 0; n /= base) {
push(n % base);
}
unsigned c;
while ((c = pop()) != EMPTY) {
if (c < 10) {
print(c + '0');
} else {
print(c + ('A' - 10));
}
}
print('\n');
}
int main(int argc, char** argv) {
outpt(255, 10);
outpt(255, 16);
outpt(255, 2);
return 0;
}
fuck you and your no stdlib functions
putchar is stdlib
const bc = require('base-conversion');
module.exports = function(n, base) {
return bc(10, base)(n);
}
>no stdlib
So the caller has to allocate.
static const char digits[] = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ";
/**
* Write n in base b ( 36) {
return -1;
}
int offset = 0;
int written = 0;
if (n < 0) {
buf[0] = '-';
offset = 1;
written = 1;
n = -n;
}
// Write digits in reverse order into the buffer.
while (n > 0) {
buf[written] = digits[n % b];
n = n / b;
written++;
}
// Reverse order of digits, i.e., put them in the right order.
int i = offset;
int j = written - 1;
while (i < j) {
char tmp = buf[i];
buf[i] = buf[j];
buf[j] = tmp;
i++;
j--;
}
buf[written] = '\0';
return written;
}
That was a nice start for the day. Now it's back to PHP.
Just realized it has a bug: returns empty string when n is 0.
Guess there's some homework left for OP after all.
>falling for a "do my homework" post
Idiots.
Im not doing your homework unless you pay me nigger