itoa

Base Conversion Problem

末鹿安然 提交于 2019-12-06 04:34:49
I'm trying to convert an integer to a string right now, and I'm having a problem. I've gotten the code written and working for the most part, but it has a small flaw when carrying to the next place. It's hard to describe, so I'll give you an example. Using base 26 with a character set consisting of the lowercase alphabet: 0 = "a" 1 = "b" 2 = "c" ... 25 = "z" 26 = "ba" (This should equal "aa") It seems to skip the character at the zero place in the character set in certain situations. The thing that's confusing me is I see nothing wrong with my code. I've been working on this for too long now,

itoa、ltoa

无人久伴 提交于 2019-12-06 02:55:27
#include <stdlib.h> /*整形转字符型*/ char * itoa(int value, char *string, int radix) { char tmp[33]; char *tp = tmp; int i; unsigned v; int sign; char *sp; if (radix > 36 || radix <= 1) { return 0; } sign = (radix == 10 && value < 0); if (sign) v = -value; else v = (unsigned)value; while (v || tp == tmp) { i = v % radix; v = v / radix; if (i < 10) *tp++ = i + '0'; else *tp++ = i + 'a' - 10; } if (string == 0) string = (char *)malloc((tp - tmp) + sign + 1); sp = string; if (sign) *sp++ = '-'; while (tp > tmp) *sp++ = *--tp; *sp = 0; return string; } /*长整形转字符型*/ char *ltoa(long value, char *string,

ANSI C, integer to string without variadic functions

强颜欢笑 提交于 2019-12-05 17:41:04
I'm currently working with a PLC that supports ANSI C, but uses its own flavour of the GNU compiler, which doesn't compile any variadic functions and things like itoa. So using sprintf & co. isn't an option for converting integers to strings. Can anyone guide me to a site where a robust, sprintf- free implementation of itoa is listed or post a suitable algorithm here? Thanks in advance. This is from K&R: void itoa(int n, char s[]) { int i, sign; if ((sign = n) < 0) /* record sign */ n = -n; /* make n positive */ i = 0; do { /* generate digits in reverse order */ s[i++] = n % 10 + '0'; /* get

第二次博客作业

南楼画角 提交于 2019-12-04 04:43:01
一.运行截图 (1) 这是输入正确时的转换 (2) 当输入的选项不在选项范围时会提示无效选项且不会退出,而是可以继续选择。 (3)当输入的数字不是所需进制数时会提示错误,并且可以重新输入正确的数字。 (由于其他操作均相同,就不一一列出所有进制转换及错误提示) 二.函数介绍 1.函数声明 #include<stdio.h> #include<string.h> #include<math.h> #include<stdlib.h> void Er(); //将二进制数转换为其他三种进制数 void Ba(); //将八进制数转换为其他三种进制数 void Shi(); //将十进制数转换为其他三种进制数 void Shiliu();//将十六进制数转换为其他三种进制数 2.各个进制转换的函数 void Er(){//将二进制数转换为其他三种进制数 char str[128]; int b,i,j,length,sum=0; input: printf("请输入您需要转换的二进制数:\n"); scanf("%s",str); length=strlen(str); //length为字符长度 for(i=length-1;i>=0;i--){ if(str[i]!='0' && str[i]!='1'){ printf("Error\n"); printf("请输入正确的二进制数\n

What is the difference between _itoa and itoa?

扶醉桌前 提交于 2019-12-03 17:19:56
问题 Visual Studio is yelling at me about using itoa() saying to use _itoa() instead? It looks to me like they are the same function. What gives? 回答1: A C run time library implementation is not supposed to introduce names that aren't in the standard unless they follow a certain naming convention (like starting with an underscore). The earlier versions of Microsoft's compiler didn't follow this rule particularly closely, but over time, Microsoft has been moving more toward making their

What is the difference between _itoa and itoa?

前提是你 提交于 2019-12-03 07:14:57
Visual Studio is yelling at me about using itoa() saying to use _itoa() instead? It looks to me like they are the same function. What gives? A C run time library implementation is not supposed to introduce names that aren't in the standard unless they follow a certain naming convention (like starting with an underscore). The earlier versions of Microsoft's compiler didn't follow this rule particularly closely, but over time, Microsoft has been moving more toward making their implementation more standards compliant. So functions they used to supply that would intrude on the user's namespace

C Error: undefined reference to '_itoa'

女生的网名这么多〃 提交于 2019-12-03 06:36:43
I'm trying to convert an integer to a character to write to a file, using this line: fputc(itoa(size, tempBuffer, 10), saveFile); and I receive this warning and message: warning: implicit declaration of 'itoa' undefined reference to '_itoa' I've already included stdlib.h, and am compiling with: gcc -Wall -pedantic -ansi Any help would be appreciated, thank you. Brian Roach itoa is not part of the standard. I suspect either -ansi is preventing you from using it, or it's not available at all. I would suggest using sprintf() If you go with the c99 standard, you can use snprintf() which is of

converting number to string in lisp

自闭症网瘾萝莉.ら 提交于 2019-12-03 05:47:11
问题 I tried to find a lisp function to convert between numbers and strings and after a little googling I fond a function with the same name. when I entered (itoa 1) SLIME printed: Undefined function ITOA called with arguments (1) . How can I do the conversion? 回答1: From number to string: (write-to-string 5) "5" you may transform a string to any numerical notation: (write-to-string 341 :base 10) "341" From string to number: (parse-integer "5") 5 with some trash (parse-integer " 5 something not a

implicit declaration of function itoa is invalid in c99

匿名 (未验证) 提交于 2019-12-03 02:15:02
可以将文章内容翻译成中文,广告屏蔽插件可能会导致该功能失效(如失效,请关闭广告屏蔽插件后再试): 问题: When I try use the function itoa() , I get the warning: implicit declaration of function is invalid in c99. I have included stdlib.h on my header. I'm trying to call this function inside a function, I'm not sure if this is allowed. 回答1: The problem is that itoa() isn't a standard function. You should take a look at this link which gives you some alternative implementations 回答2: An alternative that is commonly used in place of itoa is sprintf / snprintf . These are part of stdio.h . 文章来源: implicit declaration of function itoa is invalid in

Optimal Base-10 only itoa() function? [closed]

匿名 (未验证) 提交于 2019-12-03 01:12:01
可以将文章内容翻译成中文,广告屏蔽插件可能会导致该功能失效(如失效,请关闭广告屏蔽插件后再试): 问题: In 20+ years programming in C I've used a base other than 10 once, so when I found my trusty MSVC's _itoa() missing in another environment, I set out to write one that only does base 10, and puts the destination buffer argument, pointing to the storage returned by the function, on the left, instead of on the right, like all of the string functions in the C Standard Library. I believe this code is also thread-safe. Is there a faster way to do this? I was also going to ask about correctness, but I believe the included test code proves it works