How to convert HEX number in DEC

Guys, here im again ahahahaha!

Now i want to know how convert HEX numbers that i received on the serial port by string.

For Example, When i send the request to my module return the music name and nume, this return with this String.

M1+00000008 - MUSIC NUMBER
M2+0000002A - TOTAL NUMBER OF SONGS
MT+000000E4 - MUSIC TIME
MK+00000000
MF+y2mate.com - Daddy Yankee - Gasolina.mp3

This is Hexadecimal numbers.

I already have in my sketch information dividers that separate information by information in specific memory

So I'm resuming my sketch because it's about 5 pages long hahaha



String musicnumber = 0000004F;     /// this is a example number, need be a String cuz i'm working with "String.substring" Functions on this code.


void setup() {
Serial.begin (115200)

}

void loop() {
  Serial.println ()   /// How can i do to print  "79"

}

I Tried use

lcd.print(strtol("0000004F", NULL, 16));

and this worked!

but when i try use the String "musictime"

lcd.print(strtol(musictime, NULL, 16));

The Log Shows:

exit status 1
cannot convert 'String' to 'const char*' for argument '1' to 'long int strtol(const char*, char**, int)'

The function strtoul() can convert ASCII C-string representations in any common number base to binary.

char text[]="00002A";
Serial.println(strtoul(text, NULL, 16));

It does not work with Strings, which you should avoid anyway.

1 Like
1 Like

Exact same question a month ago:

1 Like

Do I need to include some library?

This is the LOG result and the serial monitor is receiving "0" "0" "0" "0" "0" "0" "0" only.

I Will Read About. Thank U

No, this is the whole sketch:

void setup() {
  Serial.begin(115200);
  while (!Serial);
  Serial.println();

  char input[] = "7a2a";
  int x;
  char *endptr;

  x = strtol(input, &endptr, 16);
  Serial.println(x, HEX);
  Serial.println(x, DEC);
}

void loop()
{
}
1 Like

I Have problem for search cuz im brazilian. Sometimes the words in other languages is like a letters, only letters hahahaha. Thanks for showing me.

strtol() and strtoul() DO NOT work with Strings.

Instead use C-strings (zero terminated ASCII character arrays).

1 Like

Okay, I'll adapt my code to work with char

They do work with Strings:

void setup() {
  Serial.begin(115200);
  while (!Serial);
  Serial.println();

  String input = "7a2a";
  int x;
  char *endptr;

  x = strtol(input.c_str(), &endptr, 16);
  Serial.println(x, HEX);
  Serial.println(x, DEC);
}

void loop() {}

It might not be a great idea to make excessive use of Strings on a microcontroller with 2KiB of RAM, but that's a different discussion.

2 Likes

Yes, they work with an awkward subfunction of Strings. NO THANKS.

my code has 5 or 6 pages and about 15 Strings var hahahaha. :sweat_smile:
I know that i need improve this Sketch :confused:

Start by getting rid of the Strings. They are not needed.

However, you can expect someone to come along shortly and tell you how wonderful Strings are.

1 Like

For receive Serial data and work with .Substring, no?
Can i work with others var for text?

For receiving serial data, study this helpful Serial Input Basics tutorial.

1 Like

How can i convert the String to Char?
Im "googling" but i dont match any didatic code

I found a function called String.toCharArray(), How Can i use it?

Did you not see reply #11, which already answered your question?

1 Like

I saw, but I didn't understand that this code already converts to char.

My Project Is Working Perfectly now ! Thank you all!

:star_struck: :partying_face: :boom: :dizzy:

This topic was automatically closed 120 days after the last reply. New replies are no longer allowed.