Ajuda em Char to String


olá, peço desculpa pela pergunta mas existe alguma forma de converter uma variável char em string? eu tenho um array com vários carateres e queria usar alguns de umas posições para os converter de hex para dec

o que queria fazer era algo como isto:
temphex=hextodec(bufin[9]+bufin[10]);

usar função hextodec para converter uma string (que tem código hexadecimal) num número dec.

o meu problema:  error: invalid conversion 'unsigned int' 'const char*'

podem ajudar-me juntar os dois carateres '5' + 'a' numa única string sem que dê o erro qdo os tento depois converter, o meu objectivo é ter como resultado '90' (o código hextodec está correcto o problema é mesmo o tipo de variável que estou usar e juntar os caracteres).

exemplo que estou fazer:

code: [select]

  char bufin[12];
  string temphex;
 
void setup(){

bufin[9]= '5';
bufin[10]='a';

[b]temphex=hextodec(bufin[9]+bufin[10]);[/b]
}

void loop(){}


// converting hex decimal:
// note: function can handle positive hex value 0 - 65,535 (a 4 digit hex string).
// larger/longer values, change "unsigned int" "long" in both places.

unsigned int hextodec(string hexstring) {
 
 unsigned int decvalue = 0;
 int nextint;
 
 for (int = 0; < hexstring.length(); i++) {
   
   nextint = int(hexstring.charat(i));
   if (nextint >= 48 && nextint <= 57) nextint = map(nextint, 48, 57, 0, 9);
   if (nextint >= 65 && nextint <= 70) nextint = map(nextint, 65, 70, 10, 15);
   if (nextint >= 97 && nextint <= 102) nextint = map(nextint, 97, 102, 10, 15);
   nextint = constrain(nextint, 0, 15);
   
   decvalue = (decvalue * 16) + nextint;
 }
 
 return decvalue;
}

mas tu queres o valor 90 em ascii? ou em formato decimal para usares em calculos?

porque é que estás colocar o 5 e o posicões 9 e 10 vector?


Arduino Forum > International > Portugues > Ajuda em Char to String


arduino

Comments

Popular posts from this blog

Convierte tu Raspberry en un NAS. Firmware fvdw-sl 15.3 - Raspberry Pi Forums

How to format a Get Request

avrdude: verification error, first mismatch at byte 0x0000 0x0c != 0x62