# Basic c++ help with MemoryBlock

**URL:** <https://forum.juce.com/t/basic-c-help-with-memoryblock/5695>\
**Category:** General JUCE discussion\
**Created:** [September 24, 2010, 9:22am UTC](https://forum.juce.com/t/basic-c-help-with-memoryblock/5695 "2010-09-24T09:22:41Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![cosmo](https://avatars.discourse-cdn.com/v4/letter/c/46a35a/32.png) [@cosmo](https://forum.juce.com/u/cosmo)\
**Post date:** [September 24, 2010, 9:22am UTC](https://forum.juce.com/t/basic-c-help-with-memoryblock/5695/1 "2010-09-24T09:22:41Z")

</div>

Hello

I’m trying to get the encoding function from [http://www.adp-gmbh.ch/cpp/common/base64.html](http://www.adp-gmbh.ch/cpp/common/base64.html) to encode a File, so I though the best way would be to get it to work with a MemoryBlock and i don’t quite understand why I’m getting messed up results, because I’m a noob.

The original function:

[code]static const std::string base64\_chars =  
“ABCDEFGHIJKLMNOPQRSTUVWXYZ”  
“abcdefghijklmnopqrstuvwxyz”  
“0123456789+/”;

std::string base64\_encode(unsigned char const\* bytes\_to\_encode, unsigned int in\_len) {  
std::string ret;  
int i = 0;  
int j = 0;  
unsigned char char\_array\_3[3];  
unsigned char char\_array\_4[4];

while (in\_len–) {  
char\_array\_3[i++] = \*(bytes\_to\_encode++);  
if (i == 3) {  
char\_array\_4[0] = (char\_array\_3[0] & 0xfc) \>\> 2;  
char\_array\_4[1] = ((char\_array\_3[0] & 0x03) \<\< 4) + ((char\_array\_3[1] & 0xf0) \>\> 4);  
char\_array\_4[2] = ((char\_array\_3[1] & 0x0f) \<\< 2) + ((char\_array\_3[2] & 0xc0) \>\> 6);  
char\_array\_4[3] = char\_array\_3[2] & 0x3f;

```
  for(i = 0; (i <4) ; i++)
    ret += base64_chars[char_array_4[i]];
  i = 0;
}

```

}

if (i)  
{  
for(j = i; j \< 3; j++)  
char\_array\_3[j] = ‘\0’;

```
char_array_4[0] = (char_array_3[0] & 0xfc) >> 2;
char_array_4[1] = ((char_array_3[0] & 0x03) << 4) + ((char_array_3[1] & 0xf0) >> 4);
char_array_4[2] = ((char_array_3[1] & 0x0f) << 2) + ((char_array_3[2] & 0xc0) >> 6);
char_array_4[3] = char_array_3[2] & 0x3f;

for (j = 0; (j < i + 1); j++)
  ret += base64_chars[char_array_4[j]];

while((i++ < 3))
  ret += '=';

```

}

return ret;

}[/code]

My version:

[code]String Base64::base64\_encode(MemoryBlock& mb) {  
static const String base64\_chars =  
“ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/”;  
int memoryBlockPosition = 0;  
int numBytes = mb.getSize();  
String ret;

```
int i = 0;
int j = 0;
unsigned char char_array_3[3];
unsigned char char_array_4[4];

while (memoryBlockPosition < numBytes)
{
	char_array_3[i++] = mb[memoryBlockPosition++];

	..... the rest is the same as above...

```

}[/code]

thanks for any hints! :roll:

---

<div class="post-metadata">

**Author:** ![jules](https://avatars.discourse-cdn.com/v4/letter/j/41988e/32.png) [@jules](https://forum.juce.com/u/jules)\
**Post date:** [September 24, 2010, 10:30am UTC](https://forum.juce.com/t/basic-c-help-with-memoryblock/5695/2 "2010-09-24T10:30:49Z")

</div>

Can’t see anything obvious - you probably need to spend some quality time with your debugger. Also have a look at the MemoryBlock::toBase64Encoding() function, which uses a different format, but might be informative.

---

<div class="post-metadata">

**Author:** ![atom](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.juce.com/atom/32/113_2.png) [@atom](https://forum.juce.com/u/atom)\
**Post date:** [September 24, 2010, 10:40am UTC](https://forum.juce.com/t/basic-c-help-with-memoryblock/5695/3 "2010-09-24T10:40:47Z")

</div>

i had the same exact problem, but i got it to work with the standard base64 encoding used on the net: [http://code.google.com/p/ctrlr/source/browse/#svn/trunk/EdoController/Primitives](http://code.google.com/p/ctrlr/source/browse/#svn/trunk/EdoController/Primitives) (Base64.cpp and Base64.h)

---

<div class="post-metadata">

**Author:** ![cosmo](https://avatars.discourse-cdn.com/v4/letter/c/46a35a/32.png) [@cosmo](https://forum.juce.com/u/cosmo)\
**Post date:** [September 24, 2010, 12:00pm UTC](https://forum.juce.com/t/basic-c-help-with-memoryblock/5695/4 "2010-09-24T12:00:04Z")

</div>

thanks for the links atom, i got it working now.
