0

I encountered this problem on a task given to me. Here's the situation:

enter image description here

If the user clicks on "Vat First" then the value is 0. Else, the value is 1.

Then, in this Textbox,

enter image description here

The range of the value permitted is 0 to 15.

Then the example values are saved like this:

"01" = since it's vat first and 1mm Z-axis Up height.

"11" = Platform First, 1mm Z-axis up

"015" = vat first, 15mm

"115" = platform first, 15mm

The reason why it is saved like this because it is the file protocol given to me, and I can't do anything about it. It says there that the value is "Platform Moving order and Z Axis Up height" in 1 byte value.

My problem now is reading the result afterwards for reloading it to the application. How do I know if it just uses 3 or 2 digits since the return of int disregards the 0 in the beginning. "015" = "15" so my application might treat it as Platform First, 5 mm rather than Vat first, 15mm.

Edit: So I just realized thanks to @BenVoigt, that it is saved into a byte and it has 8 bits. According to the protocol given to me, the first 4-bits is the moving order (0 or 1) and the rest is for the Z-Axis Up Height (0-15). For the real question, how do I separate the byte into 4 bits each and get the value?

Community
  • 1
  • 1
TerribleDog
  • 1,237
  • 1
  • 8
  • 31
  • Why????? Can't you just do JSON or at least comma separated list like 0,15? – Alexei Levenkov Mar 13 '19 at 02:33
  • Ow. I forgot to tell you that it is a file protocol given to me. I can't do anything about it @AlexeiLevenkov – TerribleDog Mar 13 '19 at 02:36
  • But it is *text* file protocol? How do you get "015" and "15" strings to be the same then? – Alexei Levenkov Mar 13 '19 at 02:37
  • Please refer to the code. @AlexeiLevenkov. I used ToString("D3") to treat them as 3 digit number, before assessing the values. And that is the problem if I saved the values as 2 digits only. – TerribleDog Mar 13 '19 at 02:39
  • With your clarification, the task is impossible. Think you'd better make the "platform first" worth +100, instead of prepending a `'1'` digit. It also could work in the other order, with `value = 10 * zShift + (platformFirst? 1: 0)` – Ben Voigt Mar 13 '19 at 02:40
  • 1
    @TerribleDog you mean some #$@# made `PlatformVatMovingOrder` of type `int` when they read non-int values from text stream ? (Side note please re-read [MCVE] guidance - it is very hard to see what exactly happening in your case ) – Alexei Levenkov Mar 13 '19 at 02:42
  • @BenVoigt This is what I'm thinking to. Impossible unless I find a way to tell me if I saved it for 3 digits or 2 digits. – TerribleDog Mar 13 '19 at 02:43
  • @TerribleDog: If it's been stuffed into a byte, then it hasn't been saved with either 2 digits or 3 digits. It's been saved with 8 bits. – Ben Voigt Mar 13 '19 at 02:44
  • @BenVoigt Yes, the first 4 bits is for the Moving Order and the rest is for the Height. Is it possible to get the two apart? – TerribleDog Mar 13 '19 at 02:45
  • @TerribleDog: Well that's an entirely different thing, and it is easy. `Order = Combined >> 4; Height = Combined & 0x0F;` And saving: `Combined = (Order << 4) | Height;` The `<<` and `>>` are the bitshift operators. – Ben Voigt Mar 13 '19 at 02:46
  • @BenVoigt Maybe you can help me for that. I'll change my question. – TerribleDog Mar 13 '19 at 02:47
  • 1
    This question could have benefited a lot without all the business logic, it was exceedingly difficult to understand – TheGeneral Mar 13 '19 at 02:52
  • @MichaelRandall I understand, I'm sorry. – TerribleDog Mar 13 '19 at 03:11

2 Answers2

3

the first 4-bits is the moving order (0 or 1) and the rest is for the Z-Axis Up Height (0-15).

This is a straightforward bit-shifting task.

Order = Combined >> 4;
Height = Combined & 0x0F;

And saving:

Combined = (Order << 4) | Height;

The << and >> are the bitshift operators.

Ben Voigt
  • 277,958
  • 43
  • 419
  • 720
  • If you go for just bit-shifting :) - `Height = ((byte)(Combined << 4)) >>4;` (but this all really answered many times already)... – Alexei Levenkov Mar 13 '19 at 02:57
-1

Can’t you just make both results strings and just append them together for your result? It seems that the integer type is your issue.

Joe2112
  • 1
  • 1
  • Nah, his issue is not showing the real definition in the file protocol document. Apparently (from comments) there's 4 bits for each item, non-overlapping, no problem. – Ben Voigt Mar 13 '19 at 02:47