3

Sorry to ask this but I have searched for this small thing and found somthing related on this below link but was unable get any idea

  1. How to store floats in array to be used later?

  2. storing in unsigned char array

How to store a float value suppose 0.00895 to an unsigned char which is later used to store in the memory buffer.

And later I need to read the array back and want to read it back from the memory.

Thanks

2
  • Why do you need (want) to store float value in unsigned char array? You can use float value. In every case it is holded in memory as chunk of bytes. Commented Nov 12, 2012 at 22:19
  • because my buffer function for storing data into memory takes its parameter as an unsigned char.I didnt get you abt float value ? Commented Nov 12, 2012 at 23:08

2 Answers 2

6

You don't need memcpy for this... That would be useful if you were copying a float array into a buffer elsewhere in memory. All you really need is to use a different pointer type to look into the character array.

const size_t BUFSIZE = 4096;
char buffer[BUFSIZE];
float *f_buf = (float*)buffer;

If all you wanted was to stick a single float into the start of the buffer, either of the following two lines is okay:

*f_buf = 0.00895;

f_buf[0] = 0.00895;

All it's really doing is letting you see the buffer as an array of floats.

int i;

/* Put a bunch of floats into the buffer */
for( i = 0; i < 10; i++ ) {
    f_buf[i] = i / 2;
}

/* Display contents of buffer in hex */
for( i = 0; i < 10 * sizeof(float); i++ ) {
    printf( "%02x", (int)buffer[i] );
}
printf( "\n" );

/* Sanity-test the buffer contents */
for( i = 0; i < 10; i++ ) {
    printf( "%d: %f\n", i, f_buf[i] );
}
Sign up to request clarification or add additional context in comments.

6 Comments

yes this looks to be what I'm searching for but I want to store the float value in an unsigned char.
You mean an array of unsigned char, right? In that case use unsigned char instead of char in my example. It makes no difference. If you mean a single unsigned char, you are dreaming. A float occupies four bytes, while a char occupies only one.
when Im trying to test buffer contents like this *f_buf = 0.00895; check =buffer[0]; sprintf(value_current,"%f",check); position =0x40; Lcd_move(position); Lcd_puts (value_current); the output is 152 but the buffer actually contains 0.00895.what is this value?
I think this is not wrkng const size_t BUFSIZE = 4096; char buffer[BUFSIZE]; float *f_buf = (float*)buffer;
In the hex dump, I would expect to see 80 characters (2 hex digits for each byte), but instead I see a lot more. Eg here's what I get when i run it on my machine (where a float is 4 bytes) Any idea why?:00000000000000000000ffffff803f0000ffffff803f000000400000004000004040000040400000ffffff80400000ffffff8040
|
3

you can copy the memory of the float into the char buffer.

float a;
char buffer[sizeof(float)];
memcpy(buffer,&a,sizeof(float));

1 Comment

Can I use sprintf(value_current,"%d",buffer[0]); to see my output in the buffer 0th element.Can I be able to see the data of the buffer.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.