473,322 Members | 1,538 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,322 software developers and data experts.

About Base 34 or Base 36 string encoding

ashitpro
542 Expert 512MB
I have got a RSA signature which is 512 bytes long.
I was looking for a way to convert this string into base34 or base36 format.

I searched a lot, but everywhere I saw converting number to given base. Here, I am looking for a big string to encode.

So far, I tried following code:

Expand|Select|Wrap|Line Numbers
  1. char const c_base_encoding[] = "0123456789ABCDEFGHJKLMNPQRSTUVWXYZ";
  2. size_t const c_base_encoding_size = (sizeof(c_base_encoding)/sizeof(c_base_encoding[0])) - 1;
  3.  
  4. std::string base_encode( unsigned __int8 * data, size_t size )
  5. {
  6.     std::string encoded;
  7.     unsigned long long value = 0;
  8.  
  9.     size_t tmp  = sizeof(unsigned long long);
  10.  
  11.     for(size_t i = 0; i <= size; i+=tmp)
  12.     {
  13.         memcpy(&value, data+i,tmp);
  14.         while( value > 0 )
  15.         {
  16.             encoded = c_base_encoding[ value % c_base_encoding_size ] + encoded;
  17.             value /= c_base_encoding_size;
  18.         }
  19.     }
  20.  
  21.     return encoded;
  22. }
  23.  
  24.  
Here, I am basically reading 8 bytes from buffer and treating it as a long and then convert it to base34.
The problem is, final output should be 101 bytes, but its getting more than 400 bytes. I am missing something in here. Any idea? or do you suggest any other method? Reading long value out of byte array, is it a valid way of encoding?
Dec 13 '11 #1
1 5767
Rabbit
12,516 Expert Mod 8TB
If you're converting 512-bytes encoded in a Base256 scheme to a Base36 scheme, you should expect an increase in bytes. I don't know why you would expect a decrease in bytes.

It's been a while since I've done any C++ so I can't verify the veracity of the code. However, you're encoding algorithm looks off. You need to encode the full 8 bytes you're bringing in and you're not doing that. You're stopping as soon as the value becomes 0.

Take a look at the following example in Base2
Expand|Select|Wrap|Line Numbers
  1. value = some byte variable
  2. encoded = ""
  3. value = 2
  4. encoded = value mod 2 + ""
  5. encoded = 2 mod 2 + ""
  6. encoded = 0 + ""
  7. encoded = "0"
  8. value = value / 2
  9. value = 0
That's where your algorithm would stop. Your encoded string would be "0". When it should be "00000010". Also, another problem is that you don't account for the fact that a Base36 isn't a Base2 derivative. This means that at the end of your 8 bytes, you will have leftover data that needs to be kept, more data should be appended to the front, and then it needs to continue encoding. Is there a reason you decided to use Base36 other than convenience? Because it's troublesome since it's not a power of two.
Dec 13 '11 #2

Sign in to post your reply or Sign up for a free account.

Similar topics

9
by: Mark | last post by:
I've run a few simple tests looking at how query string encoding/decoding gets handled in asp.net, and it seems like the situation is even messier than it was in asp... Can't say I think much of the...
0
by: Raed Sawalha | last post by:
in the aspx application am calling a component (made by me) that convert a string to base64 as : //In Component Side (Different solution than application) return...
3
by: Diffident | last post by:
ErrorMessage: ------------- Invalid length for a Base-64 string. ErrorSource: ------------- mscorlib ErrorTargetSite: ----------------
2
by: Jon Paal | last post by:
how can I find which character is invalid in base-64 string ?
1
by: adrian.besimi | last post by:
Hi, I got a serious problem regarding the Server.Execute and/or Server.Transfer. Here's the idea of my application: I have 1 web page named Default.aspx where I execute all my other pages.
2
by: =?Utf-8?B?SmFtZXM=?= | last post by:
I'm trying to encrypt a part of a url as follows: test.aspx?id=123&check=abc Basically, I'm using TripleDes encryption to encrypt the id, then include this as check in the url to prevent...
4
by: Ron Garret | last post by:
Is there a way to change the default string encoding used by the string.encode() method? My default environment is utf-8 but I need it to be latin-1 to avoid errors like this: Traceback (most...
0
by: =?Utf-8?B?a2V2aW4=?= | last post by:
.....using VS2005, AJAX 1.0 and Windows Workflow Foundation/.net 3.0 The initial page (with four datagridview controls and 7 UpdatePanels) Originally I got the error message defined in the...
5
by: mvmashraf | last post by:
Hi to all.... I have spent some time browsing for a solution to the following error but have unfortunatly not found any solution yet ,Please any help would be much appreciated.......
5
by: abeda | last post by:
I have a wstring that has a base 64 string. I need to extract a byte array from this base 64 string. How can I do this? I am implementing a COM Server in C++. Please help
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
1
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.