C++ :: Encoding Byte Array Data?

Apr 23, 2012

I'm trying to parse some binary data in the form of an array of bytes and I've come across something that is confusing me related to the representation of data as chars versus ints. It's a bit of a long story, but the byte array contains a mixture of character data and integer data which I' having trouble unravelling. The problem seems to arise from the issue below:

Code:
const char * ch_arr = "abcd";
const unsigned int * ui_arr = (const unsigned int*)ch_arr;
cout << ui_arr[0] << endl;
unsigned int ui = 'a';
ui = ui << 8;
ui |= 'b';
ui = ui << 8;
ui |= 'c';
ui = ui << 8;
ui |= 'd';
cout << ui << endl;

I expected both the output lines to be the same, since they contain the same bytes (I believe), but I get:

Code:
1684234849
1633837924

View 4 Replies


ADVERTISEMENT

C# :: Filtering Invalid Byte Sequences For UTF8 Encoding For A Postgre?

Jun 25, 2014

Basically, I am inserting data from an ODBC connection into a PostgreSql database using the COPY query, but the COPY query stops and returns this error...

Quote
ERROR: invalid byte sequence for encoding "UTF8": 0x92
CONTEXT: COPY [TableName], line 1: "189572|1-00-1202|1-|00-|1202||AP||1...
STATEMENT: COPY [TableName] FROM STDIN (DELIMITER '|', NULL '')

View 4 Replies View Related

C# :: Create Byte Array From IntPtr Without Copying Data

Apr 18, 2012

let's say I have an IntPtr that points to the raw data of System.Drawing.Bitmap. is there any way to create a byte array from that IntPtr without copying the data? I'm a pretty experienced C++ programmer, so I can call ToPointer() on it and convert to a byte* to work with it as a pointer, which is no big deal for me, but using a pointer and doing pointer arithmetic increases the risk of bugs, so I'd prefer not to do it that way if there's another way.

View 4 Replies View Related

C++ :: Read Byte Of Char Data And Convert It Into Text String Of Binary Data That Represents Hex Value

Dec 26, 2013

I am writing a program where I need to read a byte of char data and convert it into a text string of binary data that represents the hex value...

i.e. The char byte is 0x42 so I need a string that has 01000010 in it. I've written the following subroutine....

------------- My Subroutine ----------------------------------------------------------------------
void charbytetostring(char input, char *output){
int i, remainder;
char BASE=0x2;
int DIGITS=8;
char digitsArray[3] = "01";

[Code] ....

When I submitted the byte 0x42 to the subroutine, the subroutine returned to the output variable 01000010... Life is good.

The next byte that came in was 0x91. When I submit this to the subroutine I get garbage out.

I am using a debugger and stepped through the subroutine a line at a time. When I feed it 0x42 I get what I expect for all variables at all points in the execution.

When I submit 0x91 When the line remainder = input % BASE; gets executed the remainder variable gets set to 0xFFFF (I expected 1). Also, when the next line gets executed..

input = input / BASE; I get C9 where I expected to get 48.

My question is, are there data limits on what can be used with the mod (%) operator? Or am I doing something more fundamentally incorrect?

View 6 Replies View Related

C++ :: Advanced Data Namespace - Exchanging Byte Sizes

Jul 27, 2013

I have an advanced data namespace in which I hope to be able to read a data variable of any type and transfer it's bytes into another type of a multiple size.

i.e.
char[4] -> int;
int -> short[2];
short -> char[2];
char[2] -> short;

but I'm having some trouble, I get the following errors (because as a template it must compile from start)

template<typename T1, typename T2> void stepBytes(UCHAR size1, UCHAR size2, T1 in, T2& out){
if(size1 == 0 || size2 == 0)
return;
if(size1 % size2 != 0 &&
size2 % size1 != 0)

[Code] ....

So, why's this error appearing?

View 2 Replies View Related

C/C++ :: Separate Byte Of Array Into Nibble For New Array

Oct 1, 2012

How can I separate byte array and put nibbles in a new array?

when I try following:

byte in[]={0xab,0x11,0x22,0x33,0xbb}; 
byte out[10];  
void seperatebyte(byte* input, byte* output) {  
  for(int i = 0;i<sizeof(input);i++)  {   
        output[i*2]  = (input[i] >> 4) & 0xf;
        output[(i*2)+1]  = input[i] & 0xf;
     }  
}  

seperatebyte(in,out); //gives output of  

10 11 1 1 184 1 184 0 0 0

I expect 10 11 1 1 2 2 3 3 11 11

View 2 Replies View Related

C# :: Reading A File Into A Byte Array?

Jun 25, 2014

I basically want to create a save editor application that will enable people to alter various values in the save by clicking on releveant buttons and then also for the editor to auto update the checksum when changes are done.

The save file is in hex so from what I can gather I would need to create a button to open the file using 'open file dialogue' and then read the file into a byte array so that the values can be called at any time when a particular butto is pressed and the application will then seek to the point in the file to make the required changes.

View 3 Replies View Related

C# :: Get Posted File From Byte Array

Oct 7, 2014

I have an application that has its own embedded web server. I am trying to add jQuery/Ajax file upload capabilities to the application however, I am running into issues getting the posted file. The jQuery/Ajax portion is similar to this method here. Due to the way the webserver was written (its in a dll and I do not have access to the source), the posted file comes in as a byte[]. If I try to save the byte array directly to file using:

File.WriteAllBytes("path", ByteArray)

I end up with a corrupt file that I cannot open. I believe this is because the byte array also contains the posted file header info (Content-Disposition, name, filename, etc.). If I view the contents of the byte array using:

System.Text.Encoding.Default.GetString(ByteArray)
the header info can be viewed as:
------WebKitFormBoundaryQfPjgEVpjoWgA5JL
Content-Disposition: form-data; name="0"; filename="someimage.png"
Content-Type: image/png
‰PNG

Based on the selected file size and the size of the byte array, the entire file is in the byte array. How can I go about extracting and saving the posted file from the byte array?

View 2 Replies View Related

C++ :: Byte Array To Double Conversion?

Dec 5, 2014

I'm trying to understand why a conversion from a byte array (unsigned char) to a double works when done one way and not antoher.

In the example code I test by hard coding an unsigned char array of the same bytes that the double consists of.

When I copy the bytes to a long long and cast to double the result is not the original double but if I use a struct the bytes can be set and the conversion happens.

Seems to me that both ways should work. I'd just like to know what is going on with the "struct way" that makes the conversion correct. I see in debugger that the bytes in memory are the same for piAsLong and u.bytes.

My compiler is VS 2012 and a long long and double are both 8 bytes (tested with sizeof). This is learning activity only.

Code:
#include "stdafx.h"
#include <iostream>
#include <iomanip>
using namespace std;
union {
double d;

[code]....

View 6 Replies View Related

C++ :: Convert 2-byte Array To Short Int?

Nov 18, 2012

I'm having trouble reading a block of bytes into a vector of short ints. My code is as follows:

Code:
FileStream.seekg(2821);
vector<short> g_Data;
int iter = 0;
g_Data.reserve(maxNumOfInts);

[Code] ....

The relevant block of data starts at offset 2821 in the file. Every two bytes are a signed short integer. What's odd is that it's giving me the correct results only part of the time. At offset 1052421 and 1052422 there are two bytes 40 and 1F that are correctly read in as 8000, but at offset 1052415 and 1052416 bytes 88 and 13 are read in as -120 instead of 5000.

I don't see anything wrong with my code, though, unless I'm misunderstanding completely how to convert an array of two bytes into a single float. Is my method correct? Better still, is there some way to just convert en mass an array of bytes into a vector of signed short ints?

View 5 Replies View Related

C# :: Store Each Char Of A String Into Byte-array?

Apr 25, 2014

I am trying to store each char of a string(string a ="1100") into a byteArray ( byte[] byteArray = new byte[4]. its not showing any error but its storing like below:

byteArray[0] = 49
byteArray[1] = 49
byteArray[2] = 48
byteArray[3] = 48

and what i want is

byteArray[0] = 1
byteArray[1] = 1
byteArray[2] = 0
byteArray[3] = 0

I don't know why but its replacing 1 with 49 and 0 with 48.what am I doing wrong or how to do this?

my code is as below

byte[] byteArray = new byte[4];)/>
int binArrayAdd = 0;
string a ="1100";
foreach (char Character in a)
{
byteArray [binArrayAdd] = Convert.ToByte(Character);
binArrayAdd++;
}

View 4 Replies View Related

C Sharp :: Byte Array To Char Conversion

Jul 26, 2012

- API Function

TestSend(ref char data, ref int len, char slot);
---------------------------------------------------
 Byte[] IccSelect = new Byte[7]{
                              0x00,    
                              0xA4,    // INS 
                              0x04,    //1                              0x00,    //2                              0x0E,    
                              0x31,    
                              0x50,    
                              };  
Int32 len = 20;

TestSend(ref ??, ref len, '0');//byte array to char conversion

View 1 Replies View Related

C Sharp :: How To Extract A String From Byte Array

Feb 11, 2014

i need to search for a keyword in a binary(.raw)file and have to extract the characters(until a '&' character is found)that immediately following the keyword .

View 1 Replies View Related

C Sharp :: Write Byte Array To Textboxes?

Jan 22, 2015

In my application there is a structure that holds 200 parameters for 200 tests. These structure is converted to byte array . I want to write this byte array to a file in save button and when I click open button, this file must open and write these bytes to corresponding text boxes. How it possible.?

View 1 Replies View Related

Visual C++ :: How To Convert PNG Image Into Byte Array

Mar 12, 2013

I m loading a png image by using the ATLImage library.

CImage Image;
Image.Load (L"D:ImagesPNG_ImagesImage7.png");

how to retrieve the byte array value of this png image. I tried retrieving it as

byte *png = reinterpret_cast<BYTE *>(Image.GetBits());

but when i access the data , der is a loss of data while converting it in the above manner.

I found a snippet in net for decoding the raw data . but der is a line is the code which is unknown to me. The code is as follows :

CImage atlImage;
HMODULE hMod = GetModuleHandle(NULL);
atlImage.Load(bstr);
void* pPixel = atlImage.GetBits();
intpitch = atlImage.GetPitch();
intdepth = atlImage.GetBPP();

[Code] ....

How do I get the byte * value form the png image loaded?

View 3 Replies View Related

Visual C++ :: Draw Image Using 2D Byte Array?

Feb 8, 2013

I like to Draw an Image Using two dimensional byte array[512][256].

I used SetPixelV method to plot the image.

But Its very slow & hidden some buttons(Controls) at run time in the same dialog.

For reference i send some code,

Code:
for(row = 0; row <512; row ++)
{
for(col = 0; col < 256; col++)
{
Data[row][col] = y;
SetPixelV(d, 10+row, 10+col, RGB(Data[row][col],Data[row][col],Data[row][col]));
}
}

View 14 Replies View Related

C++ :: Variable To Byte Array Converter Class?

Aug 18, 2012

I've been working on making a class that makes turning any variable or object into a byte array and vice-versa, quick with minimal pointer interface.

As well as a function for turning any variable or object into a binary text string.

Code:
#include <iostream>
#include <bitset>
#include <sstream>
// Convert a Variable to a Byte Array
template <class var>
unsigned char* VarToBytes(var &data) {

[code].....

View 2 Replies View Related

C++ :: Storing A String In Byte-array Form?

Jun 6, 2012

I am trying to use C# with C++, two different applications that work together.

In C# it is easy to get a byte array out of a string, by just using Encoding.Default.GetBytes(of-this-string);

I can pass bytes to my C++ program by just writing in the embedded resources. But this won't allow strings, as far as I know it can only be a byte array. C++ reads the embedded resources a LPBYTE.

So I try to send the string or message in byteform.

However the problem in C++ is that there is no Encoding.Default.GetString(xxx)

Would there be any other ways to send a message/sentence in bytearrayform and request it in C++ back to the original string?

View 14 Replies View Related

C/C++ :: How To Convert Byte Array With Hex Number To Equivalent Decimal Value

Sep 18, 2012

I tried to convert byte array with hex to equivalent decimal value as follows but it gives me unexpected results:

byte hex_arr[] = { 0x00, 0x01, 0xab, 0x90};
unsigned long i=0;
i = hex_arr[3] + (hex_arr[2] << 8) + (hex_arr[1] << 16) + (hex_arr[0] << 24);
the output is 4294945680
Correct output should be 109456

but when I try with byte hex_arr[]={0x00,0x00,0x0f,0xff};

it gives correct out put 4095

the correct output works until the hex values is {0x00,0x00,0x7f,0xff}

View 8 Replies View Related

C++ :: How To Pass Constant BYTE Array Declared In Function As Parameter

Apr 20, 2013

I have this code:

const BYTE original[2][4] = {
{0x00, 0x00, 0x00, 0x00},
{0xFF, 0xFF, 0xFF, 0xFF}
};
void function(const BYTE** values){

[Code] ....

You might notice that the above code doesn't compile, this is the error:

cannot convert parameter 2 from 'BYTE [2][4]' to 'BYTE *'
1>
Types pointed to are unrelated; conversion requires reinterpret_cast, C-style cast or function-style cast

Even after some search I couldn't really find an answer to my problem, how do I pass the const BYTE array which I declared above in the function as a parameter (or what structure do I need to set for the function as a parameter)?

View 10 Replies View Related

Visual C++ :: Processing Time For PVOID Into Byte Array Conversion

Mar 12, 2013

In my project I'm using PVOID pointer to store real time data. After getting this data I will convert the data into byte array, like below

Code:
byte *bPoint = NULL;
PVOID pvData;
byte TempArr[1024];
bPoint = (byte*) pvData;
for(int i=0;i<1024;i++)
TempArr[i] = (byte) (*bPoint + i);

Processing time for the above code takes 9500 to 9900 microseconds (Used QueryPerformanceCounter).

Code:
TempArr[0] = ((BYTE*) pvData) [0];

This code takes 1100 to 1200 microseconds. My doubt is, The processing time of PVOID data into byte array conversion takes time like above? Or any other easy way(PVOID data into byte array conversion) to reduce the processing time?

View 14 Replies View Related

C :: Mcrypt Lib And String Encoding

Feb 11, 2015

I've got an application here which connects over the internet to a webserver and sends some json strings. This is all working already.

Now I want to encode one string via mcrypt (because it seemed the easiest library of all) AES and send it over to the other server where it should get decrypted again and checked for validity.

I'll be using this sample code as "starting base". i've found it on the internet:

Code:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

/*
* MCrypt API available online:

[Code] ....

The code works as it is, it encodes my string into a ciphered text and displays the text via the display function to stdout.

From previous projects I know I usually used Base64 for transporting strings via json, but in this example the string is encoded into "%d" - so decimals.. It works too, and I dont care if I send a base64 encoded string or these decimals but how would I calculate back the %d encoded string ? So how would a "undisplay()" class look alike ?

Or maybe there is an easier way to transport the string and re-decode it ?

I'd expect something like:

base64string = base64encode(ciphertext);
...
send base64string to host2
...
at host2:
ciphertext = base64decode(base64string)
...
and then mcrypt_decode that ciphertext...

View 1 Replies View Related

C :: How To Write In DOS Encoding Using Class FILE

Dec 12, 2013

How to write in DOS encoding using class FILE ?

View 5 Replies View Related

C/C++ :: Zip And Unzip Files Using Huffman Encoding

Apr 14, 2015

I am writing a program to zip and unzip files using Huffman Encoding. I have successfully Built my Huffman Tree but I am unable to traverse through it to retrieve code the code for each character in sample files using the GetCode() method.

Here is my zip.cpp file:

#include <iostream>
#include <fstream>
#include <algorithm>
#include <cctype>
#include "huffman.h"
using namespace std;
void CountLetters(int frequency[]);
int main(int argc, char *argv[]) {

[Code] ....

Specifically I am having trouble with the following segment in zip.cpp where I want to go through my built tree and print out each character and it's associated ascii value using the GetCode() method in huffman.cpp

cout << " Here are the codes: "<< endl;
for(char ch = char(0); ch <= char(255); ch++)
//for(int i= 1; i < 256; i++ )
{ //int x = (char) i;
if(mytree.inTree('*'))

[Code] ....

View 8 Replies View Related

C/C++ :: Encoding And Decoding Text From TXT Files

Dec 4, 2014

I'm new to C++ and involving encoding and decoding text from text files. When I enter a file in the program just stalls.

#include <iostream >
#include <iomanip>
#include <string>
#include <cstring>
#include <fstream>
using namespace std;
char getmenuselection (char m);

{Code] .....

View 3 Replies View Related

C/C++ :: Writing A Program Which Includes Encoding And Decoding A Message?

Oct 31, 2014

I am writing a porgram which includes encoding and decoding a message.Now I am doing the encoding part.

The goal is to encode a message using similar approach as Caesar Cipher. But instead of alphabets, I am going to use the entire ASCII code of total 128 codes to be the base.

And it is not a simple shifting, it is a rotation. This means if you want to shift 4 units from char <DEL>, it will come back to the first code in ASCII Code which is at dec 0 char <NUL> and starts the shifting again. It won't jump if the limit of ASCII Code is reached.

Here we've got 7 different malls' names and I am going to encode them with a shift n. I think n is free to set, so here I just set it as 5. This is also called the key.

I have written something and I think it should work, but after all it doesn't. And I don't get what is wrong.

Here is my program:

#include <iostream>
using namespace std;
const int NUMBER_OF_MALLS = 7;
const int MALL_NAME_LENGTH = 13;
const int NAME_SIZE = MALL_NAME_LENGTH + 1;
void encode(int key, char plain[], int length, char encoded[]) {
for (int i = 0; i < MALL_NAME_LENGTH; i++)

[code].....

Note that I am not going to use other libraries, just <iostream>.

View 13 Replies View Related







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