I am reading a value sent over RS485 which is the value of an encoder I first check if it has returned an E character (the encoder is reporting an error) and if not then do the following
*position = atoi( buffer );
// Also tried *position = (s32) strtol(buffer,NULL,10);
The value in the buffer is 4033536 and position gets set to 33536 this does not happen every time in this function probably 1 in 1000 times maybe although I am not counting. Setting the program counter back and doing the line again if has failed returns the same result but starting the debugger again causes the value to convert correctly.
I am using keil uvision 4, its a custom board using an stm32f103vet6 and the stm32f10 library V2.0.1 This one has really got me stumped never come across something like this before any help would be much appreciated.
Thanks
As no one knows I will just post what I ended up doing which was to write my own function for convertion not ideal but it worked.
bool cdec2s32(char* text, s32 *destination)
{
s32 tempResult = 0;
char currentChar;
u8 numDigits = 0;
bool negative = FALSE;
bool warning = FALSE;
if(*text == '-')
{
negative = TRUE;
text++;
}
while(*text != 0x00 && *text != '\r') //while current character not null or carridge return
{
numDigits++;
if(*text >= '0' && *text <= '9')
{
currentChar = *text;
currentChar -= '0';
if((warning && ((currentChar > 7 && !negative) || currentChar > 8 && negative )) || numDigits > 10) // Check number not too large
{
tempResult = 2147483647;
if(negative)
tempResult *= -1;
*destination = tempResult;
return FALSE;
}
tempResult *= 10;
tempResult += currentChar;
text++;
if(numDigits >= 9)
{
if(tempResult >= 214748364)
{
warning = TRUE; //Need to check next digit as close to limit
}
}
}
else if(*text == '.' || *text == ',')
{
break;
}
else
return FALSE;
}
if(negative)
tempResult *= -1;
*destination = tempResult;
return TRUE;
}
Related
can anybody help me to build the function mentioned above I am using dart in the flutter and want this function
make a function which calculate the number of same nearby character in flutter like aabcddaabb => 2abc2d2a2b
Same
void main() {
var input = 'aabcddaabb';
print(getret(input));
}
String getret(String input) {
var ret = '';
var cc = '';
var co = 0;
cut(){
var c = input[0];
input = input.substring(1);
return c;
}
write(){
if(co == 1) ret = '$ret$cc';
if(co > 1) ret = '$ret$co$cc';
}
while(input.isNotEmpty){
final c = cut();
if(c != cc){
write();
cc = c;
co = 1;
}else{
co ++;
}
}
write();
return ret; // 2abc2d2a2b
}
There's probably a smarter and shorter way to do it, but here's a possible solution:
String string = 'aaabcddaabb';
String result = '';
String lastMatch = '';
int count = 0;
while (string.isNotEmpty) {
if (string[0] != lastMatch) {
result += '${count > 1 ? count : ''}$lastMatch';
lastMatch = string[0];
count = 0;
}
count++;
string = string.substring(1);
}
result += '${count > 1 ? count : ''}$lastMatch';
print(result); //3abc2d2a2b
I also came up with this smarter solution. Even though it's nice that it's a single expression it's maybe not very readable:
String string = 'aaabcddaabb';
String result = string.split('').fold<String>(
'',
(r, e) => r.isNotEmpty && e == r[r.length - 1]
? r.length > 1 &&
int.tryParse(r.substring(r.length - 2, r.length - 1)) != null
? '${r.substring(0, r.length - 2)}${int.parse(r.substring(r.length - 2, r.length - 1)) + 1}$e'
: '${r.substring(0, r.length - 1)}2$e'
: '$r$e');
print(result); //3abc2d2a2b
I want to input three numbers into TextFormFields. The controllers are numOneController and numTwoController for putting the percent and a totalAmountController for putting the the amount. I then want to check if the percents are in total 100 so i can display they are indeed so. When totalAmountController is empty(null) then its no problem to display that its an invalid input but when the numOneController and numTwoController are empty i get the error Invalid number (at character 1). I know its the fact that handler can't add an empty number with another. But how can i handle this so i can check if its null then i want to assign a number 0 to add that automaticly.
sumHandler() {
var totalPrecent = int.parse(numOneController.text) +
int.parse(numTwoController.text) ;
var totalAmount = totalAmountController;
if (totalPrecent == 100 && totalAmount.text.isEmpty == false) {
debugPrint(totalPrecent.toString());
debugPrint(totalAmount.text.toString());
} else {
debugPrint("invalid input");
}
}
Using the tryParse method and a default value would help...
sumHandler() {
var totalPrecent = int.tryParse(numOneController.text) ?? 0 +
int.tryParse(numTwoController.text) ?? 0;
var totalAmount = totalAmountController;
if (totalPrecent == 100 && totalAmount.text.isEmpty == false) {
debugPrint(totalPrecent.toString());
debugPrint(totalAmount.text.toString());
} else {
debugPrint("invalid input");
}
}
So i found the solution for this on: Another exception was thrown: FormatException: Invalid number (at character 1)
I had to use try catch block:
try {
firstNumber = int.parse(numOneController.text);
} on FormatException {
firstNumber = 0;
}
This way i can handle the exception and on FormatException i can assign the number 0.
Create a temp variable and store it with 0 and later if the textfield has values assign values to it. Like the following
sumHandler() {
int firstNumber = 0;
int secondNumber = 0;
if(numOneController.text != "")
{
firstNumber = int.parse(numOneController.text);
}
if(numTwoController.text != "")
{
secondNumber = int.parse(numTwoController.text);
}
var totalPresent = firstNumber + secondNumber ;
var totalAmount = totalAmountController;
if (totalPresent == 100 && totalAmount.text.isEmpty == false) {
debugPrint(totalPresent.toString());
debugPrint(totalAmount.text.toString());
} else {
debugPrint("invalid input");
}
}
I had a some Issue of font rendering with NotosansCJK(Google Opensource font) and freetype 2
My freetype version is 2.8.1 and here is a part of my code when I make bitmap from Ft to create opengl texture.
[Rendering alphabet with freetype library]
bool bRender = false;
FT_Error fError;
uint uCharIndex;
if(m_tFTFace == KD_NULL)
{
return bRender;
}
if( m_nCharFontSize != nCharFontSize )
{
if(SetCharSize(nCharFontSize) == KD_FALSE)
{
return bRender;
}
}
uCharIndex = FT_Get_Char_Index(m_tFTFace,(FT_ULong)uCharUnicode);
if(uCharIndex == 0)
{
return bRender;
}
fError = FT_Load_Glyph(m_tFTFace,uCharIndex, FT_LOAD_DEFAULT | FT_LOAD_NO_BITMAP );
if(fError)
{
return bRender;
}
if(m_nExtraBoldthick > 0)
{
FT_Outline_Embolden(&m_tFTFace->glyph->outline, (m_tFTFace->size->metrics.x_ppem*m_nExtraBoldthick/100)*64);
}
fError = FT_Render_Glyph(m_tFTFace->glyph,FT_RENDER_MODE_NORMAL);
if(fError)
{
return bRender;
}
else
{
bRender = KD_TRUE;
}
if( bRender == KD_TRUE )
{
m_uRenderedUnicode = uCharUnicode;
}
return bRender;
[Make an alpha bitmap]
FT_Face pFace = m_oTtfFont.GetFontFace();
int nBitmapWidth,nBitmapHeight;
nBitmapWidth = pFace->glyph->bitmap.width;
nBitmapHeight = pFace->glyph->bitmap.rows;
kdMemset( pBitmap->m_aBitmap, 0, sizeof(KDubyte)*32*32);
int nTX = pFace->glyph->bitmap_left + 32/2 - (pFace->glyph->advance.x>>6) ;
int nTY = 0;
int y = nBitmapHeight;
while( y-- > 0 )
{
nTX = pFace->glyph->bitmap_left;
if(nTX < 0)
{
nBitmapWidth = pFace->glyph->bitmap.width + nTX;
nTX = 0;
}
else
{
nBitmapWidth = pFace->glyph->bitmap.width;
}
for (int x = 0; x < nBitmapWidth; x++)
{
pBitmap->m_aBitmap[nTY][nTX] = pFace->glyph->bitmap.buffer[(y*nBitmapWidth)+x];
nTX++;
}
nTY++;
}
Almost all of alphabets are rendering correctly but some of results are abnormal especially English if I use a character size more than 15.
all of notosancjk font has similar problem when I test it
this is the result using NotoSansCJKkr-Bold.otf and the character size is 20
[result - normal] - alphabet O
[result - abnormal] - alphabet V
I think it's a problem of freetype library or NotosanCJK font because There has no problem when I use another fonts but I'm not sure it is the real reasons.
Do anyone guess the reason of this Issue?
Thanks
I believe this can be achieved by counting the instances for each character in that string. Even if a single character in that string is repeated at least twice, we can declare that string as a palindrome.
For example: bbcccc can be rewritten as bccccb or ccbbcc.
edified can be rewritten as deified.
Some book mentioned we should be using hash table. I think we can just use a list and check for the character count.
Do you think the logic is correct?
Yes, the main idea is to count the times of each char existing in the string. And it will be true if the string has at most one char occurs odd times and all others even times.
For example:
aabbcc => acbbca
aabcc => acbca
aabbb => abbba
No. You don't have to use a hash map (as some of the other answers suggest). But the efficiency of the solution will be determined by the algorithm you use.
Here is a solution that only tracks odd characters. If we get 2 odds, we know it can't be a scrambled palindrome. I use an array to track the odd count. I reuse the array index 0 over and over until I find an odd. Then I use array index 1. If I find 2 odds, return false!
Solution without a hash map in javascript:
function isScrambledPalindrome(input) {
// TODO: Add error handling code.
var a = input.split("").sort();
var char, nextChar = "";
var charCount = [ 0 ];
var charIdx = 0;
for ( var i = 0; i < a.length; ++i) {
char = a[i];
nextChar = a[i + 1] || "";
charCount[charIdx]++;
if (char !== nextChar) {
if (charCount[charIdx] % 2 === 1) {
if (charCount.length > 1) {
// A scrambled palindrome can only have 1 odd char count.
return false;
}
charIdx = 1;
charCount.push(0);
} else if (charCount[charIdx] % 2 === 0) {
charCount[charIdx] = 0;
}
}
}
return true;
}
console.log("abc: " + isScrambledPalindrome("abc")); // false
console.log("aabbcd: " + isScrambledPalindrome("aabbcd")); // false
console.log("aabbb: " + isScrambledPalindrome("aabbb")); // true
console.log("a: " + isScrambledPalindrome("a")); // true
Using a hash map, I found a cool way to only track the odd character counts and still determine the answer.
Fun javascript hash map solution:
function isScrambledPalindrome( input ) {
var chars = {};
input.split("").forEach(function(char) {
if (chars[char]) {
delete chars[char]
} else {
chars[char] = "odd" }
});
return (Object.keys(chars).length <= 1);
}
isScrambledPalindrome("aba"); // true
isScrambledPalindrome("abba"); // true
isScrambledPalindrome("abca"); // false
Any string can be palindrome only if at most one character occur odd no. of times and all other characters must occur even number of times.
The following program can be used to check whether a palindrome can be string or not.
vector<int> vec(256,0); //Vector for all ASCII characters present.
for(int i=0;i<s.length();++i)
{
vec[s[i]-'a']++;
}
int odd_count=0,flag=0;
for(int i=0;i<vec.size();++i)
{
if(vec[i]%2!=0)
odd_count++;
if(odd_count>1)
{
flag=1;
cout<<"Can't be palindrome"<<endl;
break;
}
}
if(flag==0)
cout<<"Yes can be palindrome"<<endl;
My code check if can it is palindrome or can be manipulated to Palindrome
#include <stdio.h>
#include <string.h>
#include <stdbool.h>
//Tested on windows 64 bit arhc by using cygwin64 and GCC
bool isPalindrome (char *text);
int main()
{
char text[100]; // it could be N with defining N
bool isPal,isPosPal = false;
printf("Give me a string to test if it is Anagram of Palindrome\n");
gets(text);
isPal = isPalindrome(text);
isPosPal = isAnagramOfPalindrome(text);
if(isPal == false)
{
printf("Not a palindrome.\n");
}
else
{
printf("Palindrome.\n");
}
if(isPosPal == false)
{
printf("Not Anagram of Palindrome\n");
}
else
{
printf("Anagram of Palindrome\n");
}
return 0;
}
bool isPalindrome (char *text) {
int begin, middle, end, length = 0;
length = getLength(text);
end = length - 1;
middle = length/2;
for (begin = 0; begin < middle; begin++)
{
if (text[begin] != text[end])
{
return false;
}
end--;
}
if (begin == middle)
return true;
}
int getLength (char *text) {
int length = 0;
while (text[length] != '\0')
length++;
printf("length: %d\n",length);
return length;
}
int isAnagramOfPalindrome (char *text) {
int length = getLength(text);
int i = 0,j=0;
bool arr[26] = {false};
int counter = 0;
//char string[100]="neveroddoreven";
int a;
for (i = 0; i < length; i++)
{
a = text[i];
a = a-97;
if(arr[a])
{
arr[a] = false;
}
else
{
arr[a] = true;
}
}
for(j = 0; j < 27 ; j++)
{
if (arr[a] == true)
{
counter++;
}
}
printf("counter: %d\n",counter);
if(counter > 1)
{
return false;
}
else if(counter == 1)
{
if(length % 2 == 0)
return false;
else
return true;
}
else if(counter == 0)
{
return true;
}
}
as others have posted, the idea is to have each character occur an even number of times for an even length string, and one character an odd number of times for an odd length string.
The reason the books suggest using a hash table is due to execution time. It is an O(1) operation to insert into / retrieve from a hash map. Yes a list can be used but the execution time will be slightly slower as the sorting of the list will be O(N log N) time.
Pseudo code for a list implementation would be:
sortedList = unsortedList.sort;
bool oddCharFound = false;
//if language does not permit nullable char then initialise
//current char to first element, initialise count to 1 and loop from i=1
currentChar = null;
currentCharCount = 0;
for (int i=0; i <= sortedList.Length; i++) //start from first element go one past end of list
{
if(i == sortedList.Length
|| sortedList[i] != currentChar)
{
if(currentCharCount % 2 = 1)
{
//check if breaks rule
if((sortedList.Length % 2 = 1 && oddCharFound)
|| oddCharFound)
{
return false;
}
else
{
oddCharFound = true;
}
}
if(i!= sortedList.Length)
{
currentCharCount = 1;
currentChar = sortedList[i];
}
}
else
{
currentCharCount++;
}
}
return true;
Here is a simple solution using an array; no sort needed
#include <stdio.h>
#include <stdlib.h>
int main()
{
int a[256] = { 0 };
unsigned char i[] = {"aaBcBccc"};
unsigned char *p = &i[0];
int c = 0;
int j;
int flag = 0;
while (*p != 0)
{
a[*p]++;
p++;
}
for(j=0; j<256; j++)
{
if(a[j] & 1)
{
c++;
if(c > 1)
{
flag = 1;
break;
}
}
}
if(flag)
printf("Nope\n");
else
printf("yup\n");
return 0;
}
C#:
bool ok = s.GroupBy(c => c).Select(g => g.Count()).Where(c => c == 1).Count() < 2;
This solution, however, does use hashing.
Assuming all input characters are lower case letters.
#include<stdio.h>
int main(){
char *str;
char arr[27];
int j;
int a;
j = 0;
printf("Enter the string : ");
scanf("%s", str);
while (*str != '\0'){
a = *str;
a = a%27;
if(arr[a] == *str){
arr[a]=0;
j--;
}else{
arr[a] = *str;
j++;
}
*str++;
}
if(j==0 || j== -1 || j==1){
printf ("\nThe string can be a palindrome\n");
}
}
I am facing an issue in reading char values.
See my program below. I want to evaluate an infix expression.
As you can see I want to read '10' , '*', '20' and then use them...but if I use string indexer s[0] will be '1' and not '10' and hence I am not able to get the expected result.
Can you guys suggest me something? Code is in c#
class Program
{
static void Main(string[] args)
{
string infix = "10*2+20-20+3";
float result = EvaluateInfix(infix);
Console.WriteLine(result);
Console.ReadKey();
}
public static float EvaluateInfix(string s)
{
Stack<float> operand = new Stack<float>();
Stack<char> operator1 = new Stack<char>();
int len = s.Length;
for (int i = 0; i < len; i++)
{
if (isOperator(s[i])) // I am having an issue here as s[i] gives each character and I want the number 10
operator1.Push(s[i]);
else
{
operand.Push(s[i]);
if (operand.Count == 2)
Compute(operand, operator1);
}
}
return operand.Pop();
}
public static void Compute(Stack<float> operand, Stack<char> operator1)
{
float operand1 = operand.Pop();
float operand2 = operand.Pop();
char op = operator1.Pop();
if (op == '+')
operand.Push(operand1 + operand2);
else
if(op=='-')
operand.Push(operand1 - operand2);
else
if(op=='*')
operand.Push(operand1 * operand2);
else
if(op=='/')
operand.Push(operand1 / operand2);
}
public static bool isOperator(char c)
{
bool result = false;
if (c == '+' || c == '-' || c == '*' || c == '/')
result = true;
return result;
}
}
}
You'll need to split the string - which means working out exactly how you want to split the string. I suspect you'll find Regex.Split to be the most appropriate splitting tool in this case, as you're dealing with patterns. Alternatively, you may want to write your own splitting routine.
Do you only need to deal with integers and operators? How about whitespace? Brackets? Leading negative numbers? Multiplication by negative numbers (e.g. "3*-5")?
Store the numerical value in a variable, and push that when you encounter an operator or the end of the string:
int num = 0;
foreach (char c in s) {
if (isOperator(c)) {
if (num != 0) {
operand.Push(num);
num = 0;
}
operator1.Push(c);
if (operand.Count == 2) {
Compute(operand, operator1);
}
} else {
num = num * 10 + (int)(c - '0');
}
}
if (num != 0) {
operand.Push(num);
}