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


ADVERTISEMENT

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++ :: Creating String Representation Of Unsigned Char Bits

Mar 5, 2014

I need to convert the bit representation of an unsigned char into a string.

so like 254 would be "11111111"

I'm having some trouble where no matter what number I try to convert I get 01111111.

string bin2string(unsigned char N) {
string sN(8,'0');
unsigned char X;
unsigned char Y = 0;
for(int i = 0; i < 8; i++){

[Code] ....

View 4 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++ :: Function Works When Passed String Literal But Not A Variable

Jul 10, 2014

I'm making a .json loader for a project that I'm working on, and to simplify things, I decided to make all the root attributes named in a separate file. Here's my problem: my loading function, Add(const char* id), works just fine when I pass it a string literal.

However, when I use a function that iterates through a vector list, even with the exact same definitions as the literal, it returns the error: std::out_of_range at memory location 0x0026fb30

I've stepped through it with the VS2010 debugger about a hundred times, checked against memory locations, and just have done everything I can think of, all to no avail..

The code, with data-checking omitted:

The std::map I'm adding to:

static std::map<const char*, Item*> *s_mItems;
Initialized as std::map<const char*, Item*> *Item::s_mItems;

Add() Function (Works by itself with a literal):

static bool Add(const char* id) {
...
std::string name = node.Get("name").ToString();
std::string desc = node.Get("description").ToString();
int rarity = StrToRarity(node.Get("rarity").ToString());

[Code] ....

AddList() function, where the program always breaks:

static void AddList(std::vector<std::string> list) {
for(std::vector<std::string>::iterator it = list.begin(); it != list.end(); it++) {
Add(it->c_str());
}
}

View 3 Replies View Related

C++ :: Object That Is Passed To Function Is Changed Although No Pointer Is Passed

Mar 22, 2013

I am posting this simplified piece of code that is a bit confusing for me. There are two functions that I call. One shows the expected results but the result of the other one rather puzzles me.

//#define defineVecTyp Vec3f
#define defineVecTyp float
template <typename vecTyp>
vecTyp buildLaplacianPyramid(cv::Mat inputmat) {
vecTyp lapPyr;

[Code].....

Calling the function sum1 does not change the values stored in the variables val1 and val2. The output of the program is as follows:

val1= 1 ## val2= 10 // before the call of function sum1
val1= 1 ## val2= 10 // after the call of function sum1
sumOfVals= 22

This is quite obvious and as expected and I just pasted this piece of code as an example for better clarification.

However, if I call the function buildLaplacianPyramid and apply a function for Gaussian Blurring, this also effects the cv::Mat passed to the function. The line imshow("M1, after buildLaplacianPyramid",M1); therefore shows an image that is blurred. Since I am not passing a pointer to the cv::Mat I do not understand why this should be happening. I was assuming that there would be a copy of the cv::Mat M1 to be used within the function. Therefore I was expecting the cv::Mat M1 to retain its original value. I was expecting that all changes applied to cv::Mat inputmat within the function would not have any influence on the cv::Mat M1. Just like in my other example with the sum.

View 3 Replies View Related

C++ :: Making Function To Read Unsigned Integer Into Variable Of Type Unsigned Short Int

Apr 3, 2014

How can i write a function that will read an "unsigned integer" into a variable of type "unsigned short int"? i can not use cin >> inside the function.. so i am looking for atleast a hint!

View 16 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++ :: 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++ :: 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/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 :: 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

Visual C++ :: Sending Unsigned Char To Listbox

Apr 9, 2015

Got something like the following. A button that read characters from a thrid party tool and sends it to a listbox. But the contant is not readable.

Code:
void dlg::sendtolistbox() {
unsigned char buf[250];
thirdparty.GetData(buf, len);
Sendmessage(hndl, listboxupdate,0 , (LPARAM)buf);
}
void Mydlg::UpdateListBox(wparam a,laparm b) {
m_listbox.AddString((LPCTSTR)b);
}

The characters show up in the list box as short unreadable characters. like it is chopped.

If i change to : Sendmessage(hndl, listboxupdate,0 , (LPARAM)&buf[15]);

Then I can see readable valid strings of up to 50 characters and then empty unreadable characters afterwards. I tried all kinds of things , including using CString, still did not work.

View 8 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++ :: 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 View Related

Visual C++ :: Convert BITMAPINFO Header Into Unsigned Char Pointer?

Jan 10, 2014

I have a bitmap header information in the struct

typedef struct tagBITMAPINFO {
BITMAPINFOHEADER bmiHeader;
RGBQUAD bmiColors[1];
} BITMAPINFO

The total size of this is 1080.

Now i want to convert this into a unsigned char pointer.

unsigned char * pBMPHeaderData;

I already got the raw image data in another unsigned char buffer.

unsigned char* pRawBMPData;

Now i want to make a complete BMP image by adding the header info and raw data into a new unsigned char pointer. For this i need to convert the BITMAPINFO struct into a unsigned char *

So the new buffer will be,

unsigned char * pCompleteBMPIMageData = pBMPHeaderData + pRawBMPData;

how to do this?

View 3 Replies View Related

C/C++ :: Why To Use Return Type For String Function As Char

Oct 6, 2012

If we are using strcpy() for copying the string. As we are passing pointers to it It will copy the string & no need to return the string .This function will finely work with return type as void then why Ritchie has used it as char* strcpy()?

View 4 Replies View Related

C/C++ :: Operate Char String To Function At Programming?

Dec 8, 2013

can I operate a char string to a function at C programming?

View 1 Replies View Related

C++ :: Function To Change Char Array String To Lower Case

Feb 17, 2014

This code ran well until i added in the ToLower function which is supposed to convert the char array string to lower case (based off ascii strategy -32). correct this function so it converts string to lower case and doesn't get errors.

#include <iostream>
#include <string>
using namespace std;
const int MAX = 81; //max char is sting is 80
void ToLower(char s[]);
int main(){
string y_n;

[Code]...

View 1 Replies View Related

C++ :: Demonstrate String Concatenation - Passing Char Array Into A Function

Oct 10, 2013

I have been assigned the following task and I am having difficulty in getting it to compile. The compiler is stopping at line 27 but I don't no what the error is.

The task is as follows:

Write two functions with the following function prototypes:

int my string len(char str[])
void my string cat(char dest[], char src[], int dest size)

Both functions should take zero-terminated strings as input. The first function should return the length of the string to the calling function. The second function should take a source and a destination string and the total size of the array pointed to by dest. It should then concatenated the source string onto the end of the destination string, if and only if the destination string has the capacity to store both strings. If the destination string does not have the capacity it should not alter either, print and error, and return to the calling function. Demonstrate the use both the above functions by using them in a program in which you initialise two character arrays with two strings, print out their length, print out the combined length, and print out the combined string

And this is my code so far:

/* A program to demonstrate string concatenation */

#include <iostream>
#include <string.h>
using namespace std;
int my_string_len(char str[]){ // function to calculate length of a chracter array
int len = 0;

[Code] ....

View 8 Replies View Related

C# :: Get The Actual String Passed By Reference?

Apr 8, 2015

I have a list of Strings that are passed to a method consecutively by reference from a class. I want to get the string value passed to the method at a point in time. The reason is to perform an if statement.

//this is the class that holds that holds the constants.
using System;
using Microsoft.VisualStudio.TestTools.UnitTesting;
namespace xxx.Functional.xyz.Login {
public class Constants {
public static String Username = "paul";
public static String Code = "4";

[code].....

View 2 Replies View Related

C++ :: Convert String IP To Unsigned Int

Nov 10, 2014

I need to convert a string IP to an unsigned int (uint32), but however solutions I've found elsewhere have not worked (such as `atoi`).

When using `(uint32)"54.171.82.217 ";` : [URL] ....

When using `atoi("54.171.82.217");`: [URL] .....

How can I correctly convert the string version of the IP to the uint32 type?

View 1 Replies View Related

C/C++ :: SDL LoadBMP Fails To Load A Passed String Literal

Jan 24, 2014

Background: I'm using SDL and CodeBlocks and trying to make a Graphics class that would simplify some SDL operations such as drawing and loading images.

Issue: The loadImage function in the graphics class fails to load the image correctly and so the program prints out a blank window during run-time. I've tried multiple ways of passing a string literal into the function the surface temp fails to load and so background in Game fails to load. After testing it several times, I'm pretty sure that the issue lies with SDL_LoadBMP not registering the passed variable for whatever reason. I know the image is in the right place as writing SDL_LoadBMP("./Graphics/image.bmp"); brings it up just fine.

Current Code:

Main simply creates a Game object and execute(), so I didn't feel the need to put it on here.

Game.h
#ifndef GAME_H
#define GAME_H
#include "SDL/SDL.h"
#include "SDL/SDL_ttf.h"
#include "Graphics.h"
#include <string>
#include <cstring>
using namespace std;

//GLOBAL CONSTANTS
//game window settings

[Code] ....

Output: A blank window. (It should show the background image but doesn't.)

Note: I originally wrote it as gfx.loadImage(background, "./Graphics/image.bmp") but that gives me a conversion warning and still fails to show the image when the program runs. I've tried looking up examples similar to what I was doing but no one else seems to have this problem.

View 3 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

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







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