Question byte addition operation

patrick

Well-known member
Joined
Dec 5, 2021
Messages
249
Programming Experience
1-3
byte[] TEST1 = 65 48 48 48 48 ==> ASCII Char expression ===> A 0 0 0 0

byte[] TEST2 = 48 65 48 48 48 ==> ASCII Char expression ===> 0 A 0 0 0

I want byte addition.
So the result I want is ====> TEST1 ( 65 48 48 48 48 ) + TEST2 ( 48 65 48 48 48 ) = 65 65 48 48 48 or ASCII Char expression A A 0 0 0

How to code in C#?

Please help me ㅠㅠ
 
Last edited by a moderator:
You don't want byte addition at all because, if you did, you would simply add the bytes. You obviously want something else, but exactly what that is is not obvious.

Part of writing a good question is understanding that code needs to implement the general case, so providing a single, basic example is and nothing else is simply not enough. There are all sorts of ways that we could write ode to achieve your aim but most of them likely wouldn't work for other data. You need to describe the principle you're trying to implement, i.e. the rules for the general case. Your single example might help if we already had that but it's ambiguous at best on its own. My guess is that you're bytes actually represent hexadecimal digits and it's those digits that you want to add. That's consistent with the example you posted but it's certainly not a given that that's what you mean. You need to actually tell us what you mean.
 
For example, what is the expect result of TEST2 + TEST2?

What is the expected result of TEST1 + TEST1?

As mentioned above you have to articulate clearly what you what to do. Describe the process as if you did not have a computer and had to do things manually.
 
For example, what is the expect result of TEST2 + TEST2?

What is the expected result of TEST1 + TEST1?

As mentioned above you have to articulate clearly what you what to do. Describe the process as if you did not have a computer and had to do things manually.
This is the result of byte addition I want.
 

Attachments

  • ts.jpg
    ts.jpg
    77.1 KB · Views: 11
Yes, that screenshot just reiterates what you showed in post #1 for TEST1 + TEST2. I was asking what happens when you do TEST2 + TEST2 or TEST1 + TEST1:
C#:
  48 65 48 48 48   0 A 0 0 0
+ 48 65 48 48 48   0 A 0 0 0

or

C#:
  65 48 48 48 48   A 0 0 0 0
+ 65 48 48 48 48   A 0 0 0 0
 
Yes, that screenshot just reiterates what you showed in post #1 for TEST1 + TEST2. I was asking what happens when you do TEST2 + TEST2 or TEST1 + TEST1:
C#:
  48 65 48 48 48   0 A 0 0 0
+ 48 65 48 48 48   0 A 0 0 0

or

C#:
  65 48 48 48 48   A 0 0 0 0
+ 65 48 48 48 48   A 0 0 0 0


Only in this case. TEST1 + TEST2

Please help me

In the attached file, the range marked with a blue box is input from the user.


The range corresponding to the ASCII code is the parameter to be used for addition.

71 48 48 48 81 = G 0 0 0 Q
+48 87 48 85 48 = 0 W 0 U 0
====================
71 87 48 85 81 = G W 0 U Q
 

Attachments

  • ts.jpg
    ts.jpg
    76.7 KB · Views: 14
  • ascii-chars-table-landscape.jpg
    ascii-chars-table-landscape.jpg
    149.1 KB · Views: 14
Last edited by a moderator:
Loop and at each index pick highest value between the two arrays?
 
The point of my questions was too see what would happen if there was supposed to be some kind of carry operation, or how to deal with overflow. Even with your latest example in post #7, you have carefully set up the addends where there is always one 48 in each column. What happens if in a column you had both a 65 and a 71?
C#:
  71 48 48 48 81 = G 0 0 0 Q
+ 65 87 48 85 48 = A W 0 U 0
  ====================
  ?? 87 48 85 81 = ? W 0 U Q
 
Last edited:
I'm going to take a best guess at what it is you want and provide an example of how I would explain it.
I have two byte arrays of the same length. At any particular index, at least one array will contain the value 48, which is the ASCII code for digit 0. The other array may also contain 48 or it may contain another value. I want to create a third array of the same length that, at any particular index, contains 48 if both input arrays contain 48 or the other value if only one array contains 48.
If that's what you actually want, why couldn't you say so? If that's not what you want then that simply demonstrates why you need to provide a proper explanation. If we only have part of the story then we have to guess. Maybe we guess correctly but, if we don't, we waste our time and yours. If you just explain the problem in the first place then we don't have to guess, so we can't guess incorrectly.
 
Assuming the guess in post #11 is correct:
C#:
var result = test1.Zip(test2, (l, r) => r == 48 ? l : r).ToArray();
 
Back
Top Bottom