C++ :: Unsigned Char Array - Assigning Values Converted From Double

Aug 3, 2014

I'm having a pretty weird problem. I've created an unsigned char array for an image buffer:

buffer_rgb = new unsigned char[_w * _h * 3];
memset(buffer_rgb, 0x0, sizeof(unsigned char)* _w * _h * 3);

And I add pixel color values to it like so:

buffer_rgb[i] = ((unsigned char)(col[0] * 255));
buffer_rgb[i + 1] = ((unsigned char)(col[1] * 255));
buffer_rgb[i + 2] = ((unsigned char)(col[2] * 255));

Where col is a 'vec4' struct with a double[4] with values between 0 and 1 (this is checked and clamped elsewhere, and the output is safely within bounds). This is basically used to store rgb and intensity values.

Now, when I add a constant integer as a pixel value, i.e.:

buffer_rgb[i] = ((unsigned char)255;

Everything works as it should. However, when I use the above code, where col is different for every sample sent to the buffer, the resulting image becomes skewed in a weird way, as if the buffer writing is becoming offset as it goes.

These two images illustrate the problem:

tomsvilans.com/temp/140803_render_skew.png
tomsvilans.com/temp/140803_render_noskew.png

You can see in the 'noskew' image all pixels are the same value, from just using an unchanging int to set them. It seems to work with any value between 0-255 but fails only when this value is pulled from my changing col array.

Whole function is here:

// adds sample to pixel. coordinates must be between (-1,1)
void Frame::addSample(vec4 col, double contrib, double x, double y) {
if (x < -1 || x >= 1 || y < -_aaspect || y >= _aaspect) {

[Code] .....

View 1 Replies


ADVERTISEMENT

C++ :: Using Isalpha With String Converted To Char Array

Jan 26, 2014

I am new to C++ and I have a two player word guessing game working well. However, I would like to be able to validate whether the word entered by player 1 is a completely alphabetic word using isalpha.

The error I am getting right now is as follows:

"error: array must be initialized with a brace-enclosed initializer
char str[100]=hiddenwordtwo;"

/* isalpha portion of code */
#include <stdio.h>
#include <ctype.h>
#include <iostream>
#include <cstdlib>
#include <ctime>
#include <string>
#include <cstring>
using namespace std;

int main () {
char hiddenwordtwo[100];

[Code] .....

View 2 Replies View Related

C :: Assign Integer Value To Unsigned Char Array But It Is Not Storing Integer Values

Oct 25, 2013

I am trying to assign the integer value to unsigned char array. But it is not storing the integer values. It prints the ascii values. Here the code snippet

Code: uchar uc[100];
for(i=0;i<100;i++)
{
uc[i] = i;
}

The values which are stored in uc[] is ascii values.I need the integer values to be stored in uc[]. I tried to do it with sprintf. but the output is not as expected. if I print the uc[i] it should diplay the value as 0,1,2....99.

View 9 Replies View Related

C++ :: Type Conversion - Float Or Double Variable Into Unsigned Char Variable And Back

May 10, 2013

I would like to convert a float or double variable into unsigned char variable and back.

float number_float = 23.453f;
unsigned char* number_char = (unsigned char*)malloc(sizeof(float));
number_char = reinterpret_cast<unsigned char*> (&number_float);
float* number_float_0 = reinterpret_cast<float*>(&number_char);

I am not getting the same value back.. why?

View 2 Replies View Related

Visual C++ :: Using Wstring Converted From Char In DLL Class

Jun 17, 2013

I figured it out when I built a simple demo project. Problem arose because of trying to access a c-wrapper dll from the app class whereas the wrapper class had not been initialized there but rather in the main dialog class - so naturally it didn't work!!! Anyway, I've attached the demo for any who might be interested, but I regard the problem as resolved. Shows the value of building simple projects to isolate a problem. I failed to organize the order in which such a program initializes - I guess it's always App first, then MainFrame, then Doc and View (I think).

View 5 Replies View Related

C++ :: Copy Unsigned Char Array Into Another

May 7, 2013

I am having some trouble performing this. I am not sure, if my unsigned char arrays are null terminated, but I don't think so. Here is my code: They are supposed to be byte arrays of size 16.

int setkey(unsigned char* ky) {
printf("INSIDE POLY-DEL ... key byte array passed in HEX:
");
int i;
for (i = 0; i < (int)16; i++)

[Code] .....

View 12 Replies View Related

C/C++ :: Incrementing Hex Key Unsigned Char Array

Nov 16, 2014

i wish to generate all possible key combinations ranging:

HEX: "0F FF FF FF FF FF FF FF" TO HEX: FF FF FF FF FF FF FF FF

i also test each key after incrementing by 1, for test i want the key to be a an unsigned char[8]

key start rang and end range can be initialize/declare in any format.

Problem is if :

unsigned char key[] = {0x0F,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF};

then i can not increment this key by +1 , though if i initialize this key in decimal as:

unsigned long long key = 1152921504606846975;

then i can increment the key in for loop by key++ but then i cant convert it back into unsigned char array

i want to achieve something like this :

#include <stdio.h>
#include <string.h>
#include <stdlib.h>
unsigned char key[] = {0x0F,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF};
int main()
{

[code]...

In my programer i also have function that test each key but key has to be unsigned char...

View 2 Replies View Related

C++ :: Assigning Char To Array?

Aug 7, 2014

I want to assign a char to an array inside an if statement after the user has input the grade as an integer, but it has to fill an array with characters, such as:

char grades[5];
int grade;
char A, B, C, D, F;
cout << "Enter da grade" << endl;
cin >> grade;
if (grade < 59) {
grade[0] = F;

[code]....

A, B, C, D, and F won't transfer to the array, thus giving me the uninitialized variable error in microsoft visual studio 2010.

View 4 Replies View Related

C++ :: How Can Numeric Values Stored As Strings Be Converted To Numbers

May 16, 2013

I need to check my understanding from some questions about strings. How can numeric values stored as Strings be converted to numbers?

a)vector of required numeric data type
b)atoi function in cstdlib library
c)cout statement with required numeric data type

I picked b), I am aware of atoi, atol, and atof as methods to convert, but are there other methods?

What is the purpose of strncat function?

Combines n characters from source string into target string

C++ string provides:

a)convenient way to declare and manage character arrays
b)functions to manipulate strings
c)all of the above
d)none of the above

I picked c)

View 2 Replies View Related

C++ :: How To Extract Positive Integer From Unsigned Char Array

Jan 13, 2015

I have an embedded microcontroller system communicating with a similar system by radio. The api for the radio requires data to be transmitted as an unsigned char array. It will always transmit a positive integer in the range 0 to 255.When I receive the data I am having difficult in extracting this positive integer.

Code:
unsigned char rxData[4]={'1','2','3',''};
int inVal=0;

//want to assign inVal whatever number was transmitted

E.g. 123

I've been at this for a week and have tried at least 10 different approaches including the use of the atoi(), copying the absolute value of each element of rxData into another char array, reinterpret_cast, and others.

View 13 Replies View Related

C/C++ :: Reverse Order Of Bytes Unsigned Char Array?

Dec 1, 2014

I need fastest method to reverse order of bytes in my char array.

For example i have:

unsigned char buf[8];
// consider data stored in buf is 88 77 66 55 44 33 22 11
// how to reverse it to: 11 22 33 44 55 66 77 88
// currently i can do it by equal assignment , i make another buf like:
unsigned char buf_ok[8];

[Code] ....

// This does reverse the bytes as i want but its very slow , i am looking for fast method ..

View 3 Replies View Related

C++ :: Convert Uint8 Array To Unsigned Char Array?

Mar 2, 2012

I have the following code which attempts to assign a u_int8 array of 256 to an unsigned char[256]:

Code:
unsigned char testData[256]=pSample->data;

I get the compilation error:

error C2440: 'initializing' : cannot convert from 'const uint8_t [256]' to 'unsigned char [256]'

What is the safe way to cast or convert here?

View 3 Replies View Related

C++ :: Assigning Values To Two Dimensional Array

Feb 18, 2014

I'm trying to write a very simple program that takes values in through variables, and stores those values in a two dimensional array. The values are already passed into the void function, I need to have those values write to their corresponding locations in the array.

void planeSeats(int seats[13][6], int ticket, int ticketRow, int ticketColumn) {
if (ticket = 1) {
if (ticketRow >= 1 && ticketRow <= 2) {

[Code] ....

For example, lets say that ticketrow is 2 and ticket Column is 4 .

What I need is for ticketRow and ticketColumn to assign that data to seat[ticketRow][ticketColumn], turning it into seat[2][4]. How to do that.

View 2 Replies View Related

C++ :: Writing In Binary Format Array Of Unsigned Int Values

Aug 5, 2013

I am trying to write down in binary format an array of unsigned int values but i get the following compilation error :

: In function ‘int CIndex(std::fstream&, std::fstream&, std::fstream&, std::fstream&)’:
./src/IndexBuilder/index.cpp:23:26: error: no matching function for call to ‘std::basic_fstream<char>::write(int*, long unsigned int)’
./src/IndexBuilder/index.cpp:23:26: note: candidate is:
/usr/include/c++/4.6/bits/ostream.tcc:184:5: note: std::basic_ostream<_CharT, _Traits>& std::basic_ostream<_CharT, _Traits>::write(const _CharT*, std::streamsize) [with _CharT = char, _Traits = std::char_traits<char>, std::streamsize = long int]

This is the part the is not working:

Code:
// uia is : unsigned int * uia;
// then I have allocated the space for it
// load it with unsigned int's
// k is the number of variables in my array

o.write(uia,sizeof(unsigned int)*k); But thsi should be so simple and strait forward.... in c i do it as :

Code:
fwrite(uia, sizeof(unsigned int), k , fp); but since i would need to convert fstream to FILE* i decided to do it c++ way.

and this is how i opened the file :

Code:
o.open (fileName.c_str(),std::ios::out|std::ios::binary);

View 5 Replies View Related

C++ :: Copying Two Unsigned Char Into Unsigned Char

Mar 30, 2014

unsigned char key[32];
139 unsigned char rfseed[32];
173 f = fopen("/dev/urandom","rb");
174 fread(key,1,32,f);
175 fread(rfseed,1,32,f);

I am having problems copying outputs of the above code into other unsigned char other[32]. I need to keep the output of dev/urandom for backup. But, when I try to assign the values by memcpy(other, key, 32), the values do not match. The same problem happens by assigning values index by index in a loop.

View 2 Replies View Related

C++ ::  accessing / Assigning Values Of A 5 Dimensional Array

Feb 8, 2014

I'm okay with 2-dimensional arrays, but when I get to 3 or more, I can't seem to wrap my head around how to assign and/or pull values from specific parts.

To give an example, let's take the following example:

We know that a player can take up to 5 total quests, and each quest can have a max of 5 tasks.

Let's assume I have the following multi-dimensional array holding all of a players quest data:

quests[0] = 78;// Store the questID
quests[0][0] = 3945;// Store the 1st creature ID
quests[0][1] = 2230;// Store the 2nd creature ID
quests[0][2] = 3045;// Store the 3rd creature ID
quests[0][0][0] = 2;// Store how many needed of the 1st creature
quests[0][1][0] = 5;// Store how many needed of the 2nd creature
quests[0][2][0] = 13;// Store how many needed of the 3rd creature

As we know, the above code can't be done. How do I assign certain values to each specific dimension?

View 6 Replies View Related

C :: Reading Values From File To First 2 Columns Of 2D Array / Assigning 0 To 3rd Column

Apr 4, 2014

I am attempting to read values from a file into a 2d array temp[31][2] (31 rows, 3 columns).I only want the values from the file to be read into the first two columns.I believe I am accomplishing that but when I go to print the array, I expect the first two columns to have the file data and the third column to have all zeros. The third column, however is printing such that the value is the next row/first column.

I'm not sure for instance why on the bottom loop for line 1 it doesn't print:

temp[0][0] temp[0][1] temp[0][2] 20 49 0 It instead prints: 20 49 1

Code:

#include <stdio.h> Code: #include <math.h>
FILE *inptr;
int main() {
int temp[31][2] = {0}, tempavg[31][2] = {0};
int i, j, k, sum;
inptr = fopen("ProgrammingProject14.txt", "r");

[code]....

View 8 Replies View Related

C :: Convert Char Array To Double

Mar 6, 2015

How to convert char array into double?,i.e. store char array values into a single double variable. Below is the code that i'm working. Im extracting the value "2255.1682" from char array gpsdata1. I used while loop, extracted the value and stored in "myChar", but i want to store it in double variable "lat1".

Code:
#include <stdio.h>
#include <stdlib.h>
int main(){
unsigned char gpsdata1[]="$GPGGA,091310.000,2255.1682,N,11356.3605,E,1,4,1.62,164";
char myChar;
double lat1;

[Code] .....

View 5 Replies View Related

C++ :: How To Print Unsigned Char

Apr 23, 2013

How do I print an unsigned char in c++? E.g.

unsigned char a[100] = "acdef";
wprintf(L"a is %u
", a);
wcout << "a is " << a << endl;

gives

a is 2880488
a is 002BF3E8

and not

a is acdef
a is acdef

??

what is the difference between unsigned char and char?

View 6 Replies View Related

C :: How To Properly Put Char Values Into Array

Oct 24, 2014

I'm trying to fill the array "g" with letters from the array "letras" given a certain condition. Everything is working fine, except I couldn't do it... Strange characters appear when I run the code. What am i doing wrong?

Note: This is a part of a function. "vetor" is a parameter that was passed to this function.

Code:

int i;
int j = 0;
char g[20];
char letras[5] = {'a', 'b', 'c', 'd', 'n'};
while(j < g)
{
for(i = 0; i < 80; i = i + 4)

[Code]...

View 3 Replies View Related

C++ :: How To Convert Unsigned Char To String

Oct 4, 2014

How do I convert a variable of type unsigned char to string.

View 9 Replies View Related

C++ :: Using Char Instead Of Unsigned To Calculate Numbers?

Mar 10, 2014

How do you use char instead of unsigned to calculate numbers? This is using char only and nothing else.

Step 1: I ask the user to enter a number.
Step 2: User enters a number.
Step 3: Number user entered is going to be that number squared or cubed or w/e.

For example;
"Enter a number: " 3
" Number you entered multiplied four times: " 81 (Since (3)*(3)*(3)*(3) = 81)

Another example;
"Enter a number: " 5
" Number you entered multiplied four times: " 625 (Since (5)*(5)*(5)*(5) = 625)

Code:
Char num;
cout << "Enter a number";
cin >> num;
cout << "Number you entered multiplied four times: " << (num)*(num)*(num)*(num) << endl;

View 4 Replies View Related

C :: Assigning Value To Char Pointer

Feb 3, 2013

I thought we needed to allocate memory before assigning a value to a char* and also that we needed to use functions like strcpy() to copy something into it. Then how come this works and does not crash?

Code:
#include <iostream>
using namespace std;
int main()
{
char * buf;
buf = "Hello";
cout << buf << endl;
buf = "World!!!!!!!!";
cout << buf << endl;
return 0;
}

View 3 Replies View Related

C :: How To Assign Values To Pointer Char Array

Aug 10, 2013

I can assign values to pointer character array like this...

Code:

char *array[4]={"abc","xyz","dgf","sdt"} ;

but the case is i don't know how to assign strings through key board ???? with using gets ,getchar or other suitable function

View 2 Replies View Related

C :: Unsigned Char - Pointer Type Casting

Dec 2, 2013

I came across some code and it's not clear why it is casting an unsigned char * to another pointer type only to free it right after. Here are the relevant structures:

Code:
struct _Edje_Message {
Edje *edje;
Edje_Queue queue;
Edje_Message_Type type;
int id;
unsigned char *msg;

[Code] .....

As you can see, _Edge_Message has a *msg field, but in the function below, they cast it to the other two structure types inside the case blocks of the switch statement only to free it. What is the point or advantage of doing this?

Code:
void
_edje_message_free(Edje_Message *em) {
if (em->msg) {
int i;
switch (em->type) {

[Code] ......

View 14 Replies View Related

C++ :: Unsigned Char To String And Being Passed Into Function

Mar 24, 2015

I have the following code, but it crashes on the "data = " line...

Code:
void Test(string& data) {
unsigned char* msg = (unsigned char*)malloc(MAX_LENGTH));
...
data = string(reinterpret_cast<const char*>(msg), MAX_LENGTH);
}

I know I could just return string, but what is wrong with this code that is making it crash?

View 6 Replies View Related







Copyrights 2005-15 www.BigResource.com, All rights reserved