473,327 Members | 2,007 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,327 software developers and data experts.

Base64 unsigned/signed char

Sam
Hi,
I am using some functions return a base64 encoded string.
The functions write it into an unsigned char buffer. I am a little
confused as to why a base64 encoded string would be using
an unsigned char buffer instead of a signed char buffer?

Also if I want to write this to file. Casting the buffer to
"char *" & then using fprintf seems to work, but this doesn't
seem right to me.

What is the right way of doing this?
Aug 2 '07 #1
4 4274
"Sam" <sa*@sonofsam.comwrites:
I am using some functions return a base64 encoded string.
The functions write it into an unsigned char buffer. I am a little
confused as to why a base64 encoded string would be using
an unsigned char buffer instead of a signed char buffer?
Well, using signed char wouldn't make any sense at all, but using
plain char (which may be either signed or unsigned) would probably be
better.

I don't know why the function uses an unsigned char buffer. Perhaps
you can ask the author.

--
Keith Thompson (The_Other_Keith) ks***@mib.org <http://www.ghoti.net/~kst>
San Diego Supercomputer Center <* <http://users.sdsc.edu/~kst>
"We must do something. This is something. Therefore, we must do this."
-- Antony Jay and Jonathan Lynn, "Yes Minister"
Aug 2 '07 #2
Sam wrote:
Hi,
I am using some functions return a base64 encoded string.
The functions write it into an unsigned char buffer. I am a little
confused as to why a base64 encoded string would be using
an unsigned char buffer instead of a signed char buffer?
The C standard forces unsigned char to support a full 2^8 values (0 to
255), while signed may only support 2^8 - 1 values (-127 to +127 is a
valid implementation). If the base64 string is not wasting 2 bits (for
an 8 bit char) of each element, then signed is potentially lossy.
Also if I want to write this to file. Casting the buffer to
"char *" & then using fprintf seems to work, but this doesn't
seem right to me.

What is the right way of doing this?
If a single base64 number is encoded in a single unsigned char
element, it should be fine to cast (I'm guessing the base64 number
doesn't even use 0 to +127).

If 8 base64 numbers only consume 6 char elements (I assume this is the
case), I would use fwrite() or something else.
Richard

Aug 2 '07 #3
"Richard Urich" <Ri**********@gmail.comwrote in message
news:11**********************@x40g2000prg.googlegr oups.com...
Sam wrote:
>Hi,
I am using some functions return a base64 encoded string.
The functions write it into an unsigned char buffer. I am a little
confused as to why a base64 encoded string would be using
an unsigned char buffer instead of a signed char buffer?

The C standard forces unsigned char to support a full 2^8 values (0 to
255), while signed may only support 2^8 - 1 values (-127 to +127 is a
valid implementation). If the base64 string is not wasting 2 bits (for
an 8 bit char) of each element, then signed is potentially lossy.
Base 64 = 2^6 -- that's 2 bits less then 256 valued 2^8. No loss.

karl m

Aug 2 '07 #4
On Aug 2, 8:24 pm, "Karl Malbrain" <malbr...@yahoo.comwrote:
"Richard Urich" <RichardUr...@gmail.comwrote in message
valid implementation). If the base64 string is not wasting 2 bits (for
an 8 bit char) of each element, then signed is potentially lossy.

Base 64 = 2^6 -- that's 2 bits less then 256 valued 2^8. No loss.
This is why I stated "If the base64 string is not wasting 2 bits (for
an 8 bit char) of each element...". Perhaps you will find the
following less confusing: If the base64 string is a buffer of 6-bit
values concatenated with no "empty" or "wasted" bits in between each 6-
bit value...

Perhaps you may also think of this as a "bit-packed" buffer or
something.

I read the OP to mean a buffer of 6-bit values, and attempted to
outline my assumption that it was a buffer of 6-bit values rather than
a buffer of 6-bit values stored in 8-bit elements, even later stating
I assumed 8 numbers were being stored in only 6 elements.

My experience with base64 encoding involves such buffers, generally
because a very bandwidth-limited I/O device is being sent the buffer.
I also explained there is no risk in casting if the only valid values
for each "unsigned char" he was receiving was within 0 to +127.

I (perhaps incorrectly) felt all of this extra explanation and
background was completely off-topic and useless to post.
Richard

Aug 2 '07 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

0
by: Ramy076 | last post by:
I have a C App which uses openssl for Encoding and decoding which needs to be ported to java. The decoded text does not match between the C App and the java App. The size of the output itself is...
19
by: MiniDisc_2k2 | last post by:
Okay, here's a question about the standard. What does it say about unsigned/signed mismatches in a comparison statement: char a = 3; unsigned char b = 255; if (a<b) Now what's the real...
3
by: Siemel Naran | last post by:
Hi. Is there a way to convert the type signed int to the type unsigned int, char to unsigned char, signed char to unsigned char, and so on for all the fundamental integer types? Something like ...
4
by: John | last post by:
Hi all, I've been going through google and yahoo looking for a certain base64 decoder in C without success. What I'm after is something that you can pass a base64 encoded string into and get back...
9
by: dam_fool_2003 | last post by:
For int data type the default range starts from signed to unsigned. If we don't want negative value we can force an unsigned value. The same goes for long also. But I don't understand why we have...
10
by: tinesan | last post by:
Hello fellow C programmers, I'm just learning to program with C, and I'm wondering what the difference between signed and unsigned char is. To me there seems to be no difference, and the...
13
by: aruna.eies.eng | last post by:
i am currently trying to convert data into binary data.for that i need to know how to achieve it in c language and what are the libraries that we can use. so if any one can send me a sample code or...
3
by: pycraze | last post by:
Hi , I am currently trying to implement base64 encoding and decoding scheme in C . Python has a module , base64 , that will do the encoding and decoding with ease . I am aware of OpenSSL having...
6
by: Kislay | last post by:
Consider the following code snippet unsigned int i=10; int j= - 2; // minus 2 if(i>j) cout<<"i is greater"; else cout<<"j is greater"; Since i is unsigned , j is greater . I know why , but...
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...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
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: 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: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...

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.