cancel
Showing results for 
Search instead for 
Did you mean: 

write decimal data to flash

er3481
Senior
Posted on February 06, 2017 at 15:48

Hi,

I am trying to write a buffer data to flash, but i need to convert the hex data to decimal data,because it write the data as hex format. For example it write '81' as '51' that it is hex value. How can i convert it to decimal?

char_buffer = '0x0A';

/*

i need conversion here

*/

write it to flash.

8 REPLIES 8
Posted on February 06, 2017 at 17:27

How would you normally convert numbers between bases?

If you didn't understand the math you could use sprintf() or sscanf().

int i;

sscanf('0x0A','0x%x',&i);

printf('%d %02X\n', i, i); // Expect 10 0A

See also atoi() and itoa()

The flash stores data as binary bits, it's pretty much agnostic to if you think they are decimal, hexadecimal or ASCII

Tips, Buy me a coffee, or three.. PayPal Venmo
Up vote any posts that you find helpful, it shows what's working..
Posted on February 06, 2017 at 18:02

Hi

I am already using atoi function ( string to integer) ;

int i;

i = atoi(char_buffer);

But it does not convert char_buffer value to decimal. In other words my char_buffer is '81' but it convert it to '51' that is hex value of it. So, i can not write right data to flash.

Posted on February 06, 2017 at 19:54

It's not clear what you are asking.

'81' is a text string containing an '8' a '5' and a null. Is that what you mean?

Are you actually asking how to convert characters into integer equivalents? Convert characters into other characters? Change numbers from base 10 to base 16?

For instance:

uint8_t number = 81;

char ascii[] = '81';

These are two completely different things. 

What are you trying to achieve here? Maybe if we had a bit of background as to what you were doing we could help.

A

Posted on February 06, 2017 at 20:01

>>

In other words my char_buffer is '81' but it convert it to '51' that is hex value of it.

How does it do that?

81d = 0x51 = 01010001b

If I write 81 as a 32-bit integer it would be 0x51,0x00,0x00,0x00 as bytes in memory, but so what? There's multiple ways of representing data, you're expected to use them consistently, or do the math to convert back and forth.

>>

So, i can not write right data to flash.

This honestly makes so sense. The Flash memory doesn't care if the number is in decimal or hexadecimal, or ASCII.

Maybe you just aren't explaining yourself very well. Show a more complete example of what is being written in, and read out, and why you think something different is coming out.

Tips, Buy me a coffee, or three.. PayPal Venmo
Up vote any posts that you find helpful, it shows what's working..
Posted on February 06, 2017 at 20:41

Ok, i have a char array char_buffer[] = '81', the char_buffer[0] is '8' and char_buffer[1] is '1'. I need to write the value of '81' to flash. When i use atoi function, it returns the value of 51 that is hex value of 81. I just need to get the value of '81' and write it to flash. By the way, the atoi function returns '0' for the values of 0x0A, 0x0B.... 0x0F, and i need to write whatever the values of char_buffer.

Posted on February 06, 2017 at 21:51

Better yet, char_buffer[2] is NULL, without this, all is lost.

You think that atoi is returning the value 0x51 because you are viewing it in hexadecimal. Change your debugger to show the value as decimal and, hey presto, you will see that 81 == 0x51. When posing these questions it is important to use the proper notation. 51 is not 0x51, they are completely different. 51 is decimal. 0x51 is hex.

Why would atoi return '0'. '0' is a character string. atoi returns an int.  

http://www.cplusplus.com/reference/cstdlib/atoi/

 

If you are saying that it returns a 0, yes, it should. It converts ascii into integers. 0x0A is a line feed character. That is going to be difficult to convert to a number.

I think you need to go back and read the first few chapters of K&R. I think you have a basic misunderstanding of the data types, number and character representation.

Go here: 

http://embedded.fm/blog/embedded-wednesdays

 

Start at number 8

A

Posted on February 06, 2017 at 22:13

You need to take the 'write it to flash' part of the problem off the table.

You need to solve the issue of how you parse numbers provided to you in ASCII form, and how data is represented in memory.

int i = 81;

print('%d %x\n', i, i); // Expect 81 51, the value in the integer hasn't changed.

If you need to recognize the '0x' part of '0x0F' to determine if you are being provided with a number in hexadecimal format look at the leading characters.

char str[] = '0x51';

int x;

if (sscanf(str, '0x%x', &x) == 1) // decodes successfully 1 parameter in format requested

{

  printf('Found HEX value %02X\n', x);

}

else if (sscanf(str, '%d', &x) == 1) // decodes successfully 1 parameter in format requested

{

  printf('Found DEC value %d\n', x); // try with str[] = '81';

}

Tips, Buy me a coffee, or three.. PayPal Venmo
Up vote any posts that you find helpful, it shows what's working..
er3481
Senior
Posted on February 07, 2017 at 07:54

Turvey.Clive.002

Chichak.Andrei

Hi,

It was fault of me because of the name of topic, I just want to describe what i am doing with string and integers. The data types always bother me, i do not know the reason but it comes to me unnecessary, but sometimes it is required to know them. I just have a rush to get the result because of limited time. Thank you for all your replies...