25. november 2003 - 09:55
Der er
2 kommentarer og 1 løsning
Omskrive C++ til C
Hej eksperter... nu er jeg ikke den skarpeste til C, og har lavet et program i C++ der gerne skulle omskrives til C, håber I kan hjælpe mig :) Here it goes: #include <iostream> #include <cstdlib> #include <string> using namespace std; void main(void) { string s = "45 72 19 51 28 5"; string buf = ""; unsigned char number[20] = {0}; //Removing empty spaces for(int i = 0; i < s.length(); i++) { if(s[i] != ' ') { buf += s[i]; } } //Checking for international prefix if(buf[0] == '+') { number[0] = 1+buf.length()/2; number[1] = 0x91; buf.erase(0,1); //Appending f if length is odd if((buf.length() % 2) == 1) { buf.append("f"); } //Swapping numbers for(int j = 0; j < buf.length(); j = j+2) { number[2+(j/2)] = ((buf[j+1] != 'f' ? buf[j+1] - '0' : 0x0f) << 4) | (buf[j] - '0'); } } else { number[0] = 1+buf.length()/2; number[1] = 0xa1; //Appending f if length is odd if((buf.length() % 2) == 1) { buf.append("f"); } //Swapping numbers for(int j = 0; j < buf.length(); j = j+2) { number[2+(j/2)] = ((buf[j+1] != 'f' ? buf[j+1] - '0' : 0x0f) << 4) | (buf[j] - '0'); } } }
Annonceindlæg fra Deloitte
25. november 2003 - 10:23
#1
Grundliggende skal du vel have konverteret fra STL string til C nul termineret array. char s[] = "45 72 19 51 28 5"; char buf[100]; ... if((strlen(buf) % 2) == 1) { strcat(buf,"f"); } ...
26. november 2003 - 00:05
#2
Følgende compiler: #include <stdio.h> #include <stdlib.h> #include <string.h> int main(void) { char s[] = "45 72 19 51 28 5"; char buf[100] = ""; unsigned char number[20] = {0}; int i,j,ix; //Removing empty spaces ix=0; for(i = 0; i < strlen(s); i++) { if(s[i] != ' ') { buf[ix]= s[i]; ix++; } } buf[ix] = '\0'; //Checking for international prefix if(buf[0] == '+') { number[0] = 1+strlen(buf)/2; number[1] = 0x91; strcpy(buf,buf+1); //Appending f if length is odd if((strlen(buf) % 2) == 1) { strcat(buf,"f"); } //Swapping numbers for(j = 0; j < strlen(buf); j = j+2) { number[2+(j/2)] = ((buf[j+1] != 'f' ? buf[j+1] - '0' : 0x0f) << 4) | (buf[j] - '0'); } } else { number[0] = 1+strlen(buf)/2; number[1] = 0xa1; //Appending f if length is odd if((strlen(buf) % 2) == 1) { strcat(buf,"f"); } //Swapping numbers for(j = 0; j < strlen(buf); j = j+2) { number[2+(j/2)] = ((buf[j+1] != 'f' ? buf[j+1] - '0' : 0x0f) << 4) | (buf[j] - '0'); } } }