473,486 Members | 2,476 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

Calculating in C# with the data type "byte"

Lovro Mirnik
8 New Member
Greetings,

I need some help.
In order to save memory space, I would like to perform calculations with the byte data type, instead of int.

The number will never reach beyond 255, and will never be negative...
The problem is described in the code, so please take a look.
Expand|Select|Wrap|Line Numbers
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5.  
  6. namespace ByteCalculation
  7. {
  8.     class Program
  9.     {
  10.         static void Main(string[] args)
  11.         {
  12.             byte Number_1 = 10; // Declaring the value
  13.             byte Number_2 = 12; // Declaring the value
  14.             Console.WriteLine(Number_1 + Number_2); // Output is 22 - CORRECT!
  15.             // ---------------------------------------------------------------
  16.             // Here it's where it stops.
  17.             // If I wanted to make a basic operation, such as add 1
  18.             // I would have to declare 1 as a byte value.
  19.             // ???????????????????????????????
  20.             Number_1 = Number_1 + 1; // ?????? While, when having an int, this is possible.
  21.             // ???????????????????????????????
  22.             Console.ReadKey();
  23.             // Could there be another solution, which wouldn't require me to declare each value?
  24.         }
  25.     }
  26. }
  27.  
If you perhaps have no time to test this out yourself, this is the Error List that Microsoft Visual C# 2010 Express displays:
Expand|Select|Wrap|Line Numbers
  1. Error    1    Cannot implicitly convert type 'int' to 'byte'. An explicit conversion exists (are you missing a cast?)
  2.  
Any help will be greatly appreciated...

Lovro Mirnik
Attached Files
File Type: txt Lovro_Mirnik_CalculatingWithByteType.txt (891 Bytes, 416 views)
Feb 20 '11 #1
1 3168
Lovro Mirnik
8 New Member
Hello,

I'm here to let you know that I've found a solution.
A full topic covered on bytes can be found here.

Thank you,
Lovro Mirnik
Feb 20 '11 #2

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

Similar topics

3
14473
by: Pablo Gutierrez | last post by:
I have a C# method that reads Binary data (BLOB type) from a database and returns the data an array of bytes (i.e byte outbyte = new byte;). The BLOB column is saved into the database by a C...
5
4370
by: _BNC | last post by:
I've converted " byte" to "byte *" at times, using 'unsafe' and fixed { .... }, but the reverse does not seem to work. In this case, a C++ DLL returns a byte * and a length. What is the best...
2
1371
by: Chris Wood | last post by:
In C#, I am calling a method implemented in Managed C++ that returns an array of booleans. This method in turn calls unto unmanaged C++ code that returns an unsigned byte array, which is...
6
4454
by: Tom Kaminski [MVP] | last post by:
I can do this in ASP, but not sure how to handle in ASP.NET: How To Use the ADODB.Stream Object to Send Binary Files to the Browser through ASP http://support.microsoft.com/?kbid=276488 Do I...
8
8868
by: Manuel | last post by:
Anyone know why for openGL applications must be used GLfloat (and GLint, etc...) instead float, int, etc..? thx, Manuel
3
44704
by: SheldonMopes | last post by:
I sometimes get a pop-up box that reads "Overflow" and the module that is executing pauses. It doesn't get caught by my error trapping, and it seems to be randow. By random, I mean usually in the...
12
43393
by: Zero | last post by:
Hi everybody, i want to write a small program, which shows me the biggest and smallest number in dependance of the data type. For int the command could be: ...
0
5660
by: Ismail Fatih Yıldırım | last post by:
I modified the RSACSPSample from MSDN to try out a simple commutative encryption model using RSA encryption but when i run the progrem the first encryption command works but during the second...
0
1570
by: tarlino | last post by:
Hi! I'm play with the sound generation (c++ and directX). Now I would like to manage same filter on my sample. But now I must to convert my audio buffer "BYTE" (from DirectX) to an arry of "short"...
15
1123
by: David C. Ullrich | last post by:
Luckily I tried it before saying no, that's not how "in" works: True False Is there a reason for the inconsistency? I would have thought "in" would check for elements of a sequence,...
0
6967
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
7132
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
7180
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
1
6846
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
7341
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
1
4870
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
3076
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
1
600
muto222
php
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
266
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence...

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.