Hi all,
I have CRC32 function that returns a decimal number (I do not want to change it, it works right),
What I need to do next is to convert a decimal number into a String (yes with big S, :o not string, why? I have to) that will hold the HEX representation of the number.
NOTES: I need the String only, to use """Serial.print(123, HEX)""" is not helping me cause I need to work with this String, not to print it.
hope someone can show me how it's done with simple function.
Example:
input of the function : 3573747111 (type - long)
output of the function : D5030DA7 (type String)
String DEC_to_HEX_str_converter(long val){
???
}
What do you mean, decimal number? What data type does the function return? There is no "decimal" data type. The String class has conversion functions but you should not use String on small architectures. You can convert a long type into a C string with strtol(). Be aware that a long type cannot represent positive numbers greater than 0x7FFFFFFF. You may want to use unsigned long, and strtoul().
Hi friend,
I showed everything in my original post, even added an example, and lastly a function signature, what is that you don't understand? :o
I need a function that takes as input a long type, which holds 8~10 digits, the function must take the number, convert to a String which is a HEX representation of the number, it is a should be String of 8 chars (not including the EOS char added by the String Class), I believe the question is very clear, so I don't understand what you're asking.
I was also hoping to do this efficiently as possible.
function signature:
String function(long value);
Peace.
Take your long four bits at a time.
If the value of the four bits is less than 10, add '0', else subtract 10 and add 'A'.
Add the resulting character to your String.
TheMemberFormerlyKnownAsAWOL:
Take your long four bits at a time.
If the value of the four bits is less than 10, add '0', else subtract 10 and add 'A'.
Add the resulting character to your String.
Hi friend,
can you show me in code? I do not sure I understand
aarg:
What do you mean, decimal number? What data type does the function return? There is no "decimal" data type. The String class has conversion functions but you should not use String on small architectures. You can convert a long type into a C string with strtol(). Be aware that a long type cannot represent positive numbers greater than 0x7FFFFFFF. You may want to use unsigned long, and strtoul().
correction to my previous replay:
P.S yes, I am using an unsigned long, you are right about that.
please see the Example from the original question, I think it is very clear, if you do not like the term 'number' I specifically added the data types of input and output.
String function(unsigned long value);
Do you understand the use of the bitwise AND (&) operator to isolate groups of bits?
Do you understand the use of the << and >> shift operators?
TheMemberFormerlyKnownAsAWOL:
Do you understand the use of the bitwise AND (&) operator to isolate groups of bits?
Do you understand the use of the << and >> shift operators?
Oh dear, no, I was trying to avoid those things (bitwise and shifts), is there any other way?
You could use sprintf, and then convert the string to a String
This is the sketch that gives the desired string: (3573747111 is transformed into equivalent HEX: D5030DA7 and then each digit of HEX is saved in its ASCII Code (D as 0x44, 5 aas 0x35,...) in the array mycode[].
//input of the function : 3573747111 (type - long)
//output of the function : D5030DA7 (type String)
char myData[10] = "";
char myCode[10] = "";
int i = 0;
void setup()
{
Serial.begin(9600);
unsigned long x = 3573747119;//1; //0xD5030DA7
do
{
myData[i] = x % 16;
if (myData[i] <= 9)
{
myData[i] = myData[i] + 0x30; //0 - 9 : 0x30 - 0x39
}
else
{
myData[i] = myData[i] + 0x37; //A - F : 0x41 - 0x46
}
x = x / 16;
i++;
}
while (x != 0);
for (int i = 7, j = 0; i >= 0; i--, j++)
{
byte x = myData[i];
myCode[j] = x; //70, 61, 52, 43, 34, 25, 16, 07
}
Serial.print(myCode);//D5030DA7
//--------------------------------
}
void loop()
{
}
Note: There may be a C/C++ function to replace all the codes of the above sketch; but, I don't know.
Note: There may be a C/C++ function to replace all the codes of the above sketch; but, I don't know.
ltoa is supported in avr-libc which is the standard c library for AVR-GCC compiler.
https://www.nongnu.org/avr-libc/user-manual/group__avr__stdlib.html#ga5a3fce5fbd20140584619ba9aed09f75
void setup()
{
Serial.begin(9600);
unsigned long x = 3573747119;
char myHex[10] = "";
ltoa(x,myHex,16); //convert to c string base 16
String myHexString = String(myHex);
Serial.println(myHexString);
}
void loop()
{
}
cattledog:
ltoa is supported in avr-libc which is the standard c library for AVR-GCC compiler.
avr-libc: <stdlib.h>: General utilities
void setup()
{
Serial.begin(9600);
unsigned long x = 3573747119;
char myHex[10] = "";
ltoa(x,myHex,16); //convert to c string base 16
String myHexString = String(myHex);
Serial.println(myHexString);
}
void loop()
{
}
Perfect. +Karma
GolamMostafa:
Perfect. +Karma
THANK YOU SO MUCH FOR THIS!!!
It compiles for STM32F1 generic over here. Did you make a cut and paste error, maybe?
cattledog:
ltoa is supported in avr-libc which is the standard c library for AVR-GCC compiler.
avr-libc: <stdlib.h>: General utilities
void setup()
{
Serial.begin(9600);
unsigned long x = 3573747119;
char myHex[10] = "";
ltoa(x,myHex,16); //convert to c string base 16
String myHexString = String(myHex);
Serial.println(myHexString);
}
void loop()
{
}
thank you! but unfortunately I'm sure if I can use it, I'm programming on STM32 micro-controller and not sure if this library is supported, or maybe I found the wrong #inculde name?
I get error: 'ltoa' was not declared in this scope
aarg:
It compiles for STM32F1 generic over here. Did you make a cut and paste error, maybe?
I just copy pasted, no mistakes friend
Then explain how it works for me... there are no special libraries involved, either...
aarg:
Then explain how it works for me... there are no special libraries involved, either...
It compiles for my UNO R3, but not for STM32F103C 
what else I can do?
Do you have the latest STM Arduino core? Try to upgrade in the boards manager.
aarg:
Do you have the latest STM Arduino core? Try to upgrade in the boards manager.
- everything is up to date
- STM32 cores is installed
still not compiles, try for yourself, choose "generic STM32F103C" board and tell me if it compiles for you
@vicdidwhat
Please dont make reports for nothing.
If English is not your first language see the selection below
if you would like your post moved please use the "report to moderator" option