RxAndroidBle,值应为十六进制

时间:2018-08-06 09:23:17

标签: arrays hex rxandroidble

通知中的值应为十六进制,但我得到了十进制和负值(在另一个应用中,我得到了很好的格式):

123 0 0 72 98 0  
124 0 0 39 97 0   
125 0 0 -2 95 0 
126 0 0 -50 94 0 
127 0 0 -105 93 0  
-128 0 0 88 92 0  
-127 0 0 18 91 0  
-126 0 0 -59 89 0 
-125 0 0 113 88 0 
-124 0 0 22 87 0 
-123 0 0 -76 85 0  
-122 0 0 76 84 0  
-121 0 0 -35 82 0  
-120 0 0 103 81 0

你知道我怎么能以十六进制形式得到它?

最诚挚的问候

2 个答案:

答案 0 :(得分:0)

RxAndroidBle库在收到通知时发出byte[]。单个byte值可以用不同的方式表示。

您提供的日志只是根据Java规范打印原始值。 Java没有无符号值的概念,每个以1位开头的值都被视为负数。即0xA2等效于-94(0xA2 == -94) == true

将值显示为十六进制的一种简单方法是使用String.format(),即String.format("0x%2xh", byteValue)

您最有可能想问一个问题:How to convert a byte array to a hex String in Java

答案 1 :(得分:0)

我写了一个小方法将byte []转换成类似于import React, { Component } from "react"; import Form from "./Form"; class App extends Component { constructor(props) { super(props); this.state = { people: [] }; this.addPerson = this.addPerson.bind(this); this.deletePerson = this.deletePerson.bind(this); } addPerson(name, email) { this.setState(prevState => ({ people: [...prevState.people, { name, email }] })); } componentDidMount() { this.getPeople(); } getPeople() { fetch("https://jsonplaceholder.typicode.com/users") .then(response => response.json()) .then(response => this.setState({ people: response })) .catch(error => console.log(error)); } deletePerson(email) { return () => { this.setState(prevState => ({ people: prevState.people.filter(person => person.email !== email) })); }; } render() { console.log(this.state); return ( <div className="App"> <Form addPerson={this.addPerson} /> <table> <thead> <tr> <th>LP</th> <th>USER</th> <th>EMAIL</th> <th>Actions</th> </tr> </thead> <tbody> {this.state.people.map((person, index) => { return ( <tr key={person.email}> <th>{index + 1}</th> <td>{person.name}</td> <td>{person.email}</td> <td> <button onClick={this.deletePerson(person.email)}> Delete </button> </td> </tr> ); })} </tbody> </table> </div> ); } } 等的字符串:

import React, { Component } from "react";

class Form extends Component {
  constructor() {
    super();
    this.formSubmit = this.formSubmit.bind(this);
  }

  formSubmit(event) {
    event.preventDefault();
    const form = event.target;
    const email = form.elements["email"].value;
    const name = form.elements["name"].value;
    this.props.addPerson(name, email);
    form.reset();
  }

  render() {
    return (
      <form onSubmit={this.formSubmit}>
        <input
          id="name"
          type="text"
          defaultValue=""
          placeholder="Name..."
        />
        <input
          id="email"
          type="text"
          defaultValue=""
          placeholder="Email..."
        />
        <input type="submit" value="submit" />
      </form>
    );
  }
}

export default Form;